2013-10-29 65 views
0
fis = new FileInputStream(file); 
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; // FILE IS .txt TYPE 
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 
//pras.add(MediaSizeName.ISO_A4); 
//pras.add(new Copies(1)); 

PrintService printService[] =PrintServiceLookup.lookupPrintServices(flavor, pras); 
System.out.println("Print Service:"+printService); 

PrintService defaultService =PrintServiceLookup.lookupDefaultPrintService(); 
System.out.println("Default Service:"+defaultService); 
PrintService service = ServiceUI.printDialog(null, 200, 200,printService, defaultService, flavor, pras); 

if (service != null) 
{ 
    System.out.println("Selected Service"+service); 
    DocPrintJob job = service.createPrintJob(); 
    job.addPrintJobListener(new MyPrintJobListener()); 

    System.out.println("JOB:"+job); 
    DocAttributeSet das = new HashDocAttributeSet(); 
    Doc doc = new SimpleDoc(fis, flavor, das); 

    System.out.println("Start of Print"); 
    job.print(doc, pras); 
    System.out.println("End of Print"); 
    i=1; 
} 
else 
{ 
    i=0; 
} 

我正在處理一個Web應用程序,用戶希望打印保存在包含來自數據庫的詳細信息的文件中的數據,因爲他的查詢。我將上面的代碼放入一種方法,當它被調用時,它給出用戶的打印對話框,他可以從連接到他的機器的打印機列表中選擇打印機。 如果我嘗試打印到本地打印機,則文件將被髮送到C:\ WINDOWS \ system32 \ spool文件夾並進行打印。 但它不會在網絡打印機的情況下發生 我在網絡上的打印機是佳能MP280系列打印機 我能看到它在打印機列表中,但無法打印我的文件用java打印一個文件

回答

0

打印機網絡是共享還是不共享? 如果打印機是共享的,並在您的電腦/筆記本電腦已安裝。 你必須選擇打印服務名稱的列表陣列中的一個

PrintService defaultPrintservice = printServices[0]; 

如果打印機沒有在您的PC /筆記本電腦安裝,必須設置位置的網絡打印機的路徑

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

我希望它能解決你的問題:)對不起,我的英語:D