java 打印机 乱码
import java.applet.Applet;
import java.awt.Frame;
import java.awt.PrintJob;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.swing.JFileChooser;
import com.sun.corba.se.impl.encoding.CodeSetConversion.BTCConverter;
import sun.nio.cs.US_ASCII;
/**
* * 建立与打印机的连接
* @author Administrator
*
*/
public class Print1 {
public void print(){
JFileChooser fileChooser = new JFileChooser(); //创建打印作业
int state = fileChooser.showOpenDialog(fileChooser);
if(state == fileChooser.APPROVE_OPTION){
File file = fileChooser.getSelectedFile();
// File file=new File(file2, "utf-8");
//获取选择的文件
//构建打印请求属性集
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//设置打印格式,因为未确定类型,所以选择autosense
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
//查找所有的可用的打印服务
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
System.out.println(pras+";"+printService.length+";"+flavor);
//定位默认的打印服务
// PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
//System.out.println(defaultService.getName());
PrintService getService=null;
for(int i=0;i<printService.length;i++)
{
//System.out.println(printService[i].getName());
if(printService[i].getName().equals("Tray1"))
{
System.out.println(printService[i].getName());
getService=printService[i];
}
}
//显示打印对话框
// PrintService service = ServiceUI.printDialog(null, 200, 200, printService,
// defaultService, flavor, pras);
if( getService!= null){
//System.out.println(getService.getName());
try {
DocPrintJob job = getService.createPrintJob(); //创建打印作业
FileInputStream fis = new FileInputStream(file); //构造待打印的文件流
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
System.out.println(pras+";"+doc.getPrintData().toString());
job.print(doc, pras);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
PrintTestThreeNextOne printTestThree=new PrintTestThreeNextOne();
printTestThree.print();
}
}
打印机打印出现中文乱码
打印机 java 乱码 --------------------编程问答-------------------- 设置编码。。。。 --------------------编程问答-------------------- 怎么设置 啊 字节流里面设置还是其他
--------------------编程问答-------------------- 当然是流了,看看api --------------------编程问答-------------------- 楼主这个问题解决了没?我遇到打印PDF出现乱码啊,求解决方法……
补充:Java , Java SE