2012-02-01 192 views
0

我正在研究一個示例應用程序以在網絡打印機上打印文件。但我無法取得成功。夥計們請幫我解決這個問題。 要在網絡打印機上打印的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(); 
} 

非常感謝。

+3

歡迎StackOverflow上。事實上,您發佈的格式不完整,代碼不完整,沒有詳細說明它如何失敗。不幸的是,除了猜測之外,沒有什麼人可以提供。 – 2012-02-01 15:55:01

+1

夥計們在他身上很容易,對於想要在網絡上打印的人有用 – 2015-10-18 06:10:21

回答

0

該代碼將無法編譯,因爲您在打印機名稱有無效escape sequences

new PrinterName("ipp:\\witnw21va\ipp\ITDepartment-HP4050", null) 

Java編譯器會認爲你只是試圖寫特殊字符,如換行符\n,由\w困惑,\i等字符串,這是不合法的。

你需要逃避每個反斜線使其合法:

new PrinterName("ipp:\\\\witnw21va\\ipp\\ITDepartment-HP4050", null) 

或改變它,如果它其實應該是正斜槓

相關問題