我在使用 java 类打印时遇到问题。每次找到我的文件(路径正确)时,它都会成功发送到我的打印机队列,但片刻后它会消失,没有错误,也没有打印。这是我的代码,在此先感谢。
private void print(String path){
FileInputStream psStream = null;
try {
psStream = new FileInputStream(path);
} catch (FileNotFoundException ffne) {
ffne.printStackTrace();
}
if (psStream == null) {
return;
}
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc myDoc = new SimpleDoc(psStream, psInFormat, null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset);
PrintService myPrinter = null;
for (int i = 0; i < services.length; i++){
String svcName = services[i].toString();
System.out.println("service found: "+svcName);
if (svcName.contains(PrintServiceLookup.lookupDefaultPrintService().getName())){
myPrinter = services[i];
System.out.println("my printer found: "+svcName);
break;
}
}
if (myPrinter != null) {
DocPrintJob job = myPrinter.createPrintJob();
try {
job.print(myDoc, aset);
} catch (Exception pe) {
pe.getMessage();}
} else {
System.out.println("no printer services found");
}
}