我正在从事在网络打印机上用java实现打印的项目,打印机未配置。我遇到问题,而打印机服务查找总是返回 null。
在Java应用程序中,是否可以在未配置的打印机上打印文件?
下面的代码无法帮助检测打印机。
public int printDoc(InputStream is) throws PrintException, FileNotFoundException {
PrintService printService=PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob job = printService.createPrintJob();
job.addPrintJobListener(new PrintJobAdapter() {
public void printDataTransferCompleted(PrintJobEvent event){
System.out.println("transfer complete");
}
public void printJobNoMoreEvents(PrintJobEvent event){
System.out.println("received no more events");
}
});
Doc doc=new SimpleDoc(is, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
// Doc doc=new SimpleDoc(fis, DocFlavor.INPUT_STREAM.JPEG, null);
PrintRequestAttributeSet attrib=new HashPrintRequestAttributeSet();
attrib.add(new Copies(1));
String printeraddr= "ipp://blrprt01.blr.network18.com/23Flr_Printer1";
PrinterName prName =new PrinterName(printeraddr, null);
attrib.add(prName);
job.print(doc, attrib);
return 0;
}
最佳答案
您需要先配置打印机。
第一行位于 https://docs.oracle.com/javase/tutorial/2d/printing/printable.html 说选定的打印机
请使用PrinterJob而不是PrintServiceLookup。
关于java - 如何在java中直接在网络打印机上打印文件。应用程序运行的系统中未配置打印机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48341071/