Java应用程序在网络打印机上打印
我正在研究一个示例应用程序来打印网络打印机上的文件。 但我无法取得成功。 伙计们请帮我解决这个问题。
FileInputStream fis = new FileInputStream(file); if (fis == null) { System.out.print("No File"); return; } DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); PrintService service = PrintServiceLookup.lookupDefaultPrintService(); aset.add(new PrinterName("ipp:\\witnw21va\ipp\ITDepartment-HP4050", null)); //PrintServiceAttributeSet aset = HashPrintAttributeSet(); PrintService[] pservices = PrintServiceLookup.lookupPrintServices(flavor, aset); if (service != null){ System.out.println("Default Printer: " + service.getName()); // Creating DocPrintJob DocPrintJob job = service.createPrintJob(); try{ Doc doc = new SimpleDoc(fis,flavor,null); PrintJobWatcher pjDone = new PrintJobWatcher(job); job.print(doc,aset); // Wait for the print job to be done pjDone.waitForDone(); fis.close(); }
非常感谢。
该代码将无法编译,因为您在打印机名称中有无效的转义序列 :
new PrinterName("ipp:\\witnw21va\ipp\ITDepartment-HP4050", null)
Java编译器认为你正在尝试编写像newline \n
这样的特殊字符,并且在该字符串中被\w
, \i
等混淆,这是不合法的。
您需要转义每个反斜杠以使其合法:
new PrinterName("ipp:\\\\witnw21va\\ipp\\ITDepartment-HP4050", null)
如果它实际上应该是正斜线,则更改它