2012-04-27 59 views
5

我使用下面顯示的java代碼在連接到我的計算機的HP DeskJet1000 USB打印機上打印文本文件。每當我運行此代碼時,都會發送打印作業,但打印機不會打印任何內容。狀態顯示打印機正在打印,但它甚至沒有進入頁面。請幫忙!我的代碼如下:java打印代碼不工作

package printing; 

import java.io.FileInputStream; 
import javax.print.*; 
import javax.print.attribute.HashPrintRequestAttributeSet; 
import javax.print.attribute.PrintRequestAttributeSet; 

/** @author Majid */ 
public class Printing { 
    public static void main (String [] args) { 
     // TODO code application logic here 
     DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; 
     PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 
     /* locate a print service that can handle it */ 
     PrintService [] pservices = PrintServiceLookup.lookupPrintServices (flavor, aset); 
     /* create a print job for the chosen service */ 
     int printnbr = 0; 
     DocPrintJob pj = pservices [printnbr].createPrintJob(); 
     try { 
      FileInputStream fis = new FileInputStream ("e:/fypdatabase/test.txt"); 
      Doc doc = new SimpleDoc (fis, flavor, null); 
      //PrintJobWatcher pjDone = new PrintJobWatcher (pj); 
      /* print the doc as specified */ 
      pj.print (doc, aset); 
     } 
     catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 
+0

其他該打印機工作應用程序(如Word或記事本)? – Jeffrey 2012-04-27 22:41:03

+0

是的,它可以完美地打印所有其他應用程序。 – 2012-04-27 22:44:41

+1

你不檢查有多少打印服務返回,你只是硬編碼使用索引0? (只是想知道是否安裝了僞打印機) – Benj 2012-04-27 22:49:29

回答

0

您的代碼實際上正在工作。但可能你要打印到錯誤的打印機...

試試這個:

package printing; 

import java.io.FileInputStream; 
import javax.print.*; 
import javax.print.attribute.HashPrintRequestAttributeSet; 
import javax.print.attribute.PrintRequestAttributeSet; 

/** @author Majid */ 
public class Printing { 

    public static void main (String [] args) { 
     // TODO code application logic here 
     DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; 
     PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 
     /* locate a print service that can handle it */ 
     PrintService [] pservices = PrintServiceLookup.lookupPrintServices (flavor, aset); 

     try { 
      int printer = getPrinter(pservices); 
      if(printer == -1) { 
       throw new Exception("No network printer found"); 
      } 
      DocPrintJob pj = pservices[2].createPrintJob(); 
      FileInputStream fis = new FileInputStream ("c:/Temp/test.txt"); 
      Doc doc = new SimpleDoc (fis, flavor, null); 
      pj.print (doc, aset); 
     } 
     catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

    private int getPrinter(PrintService[] pservices) { 
     int printer = -1; 
     for(int i = 0; i<pservices.size(); i++) { 
      if(pservices[i].getName().contains("\\\\")) { 
       System.out.println("network printer: " + pservices[i].toString()); 
       printer = i; 
       break; 
      }   
     } 
     return printer; 
    } 
} 
1

@ moskiteau爲什麼你硬編碼號碼在

DocPrintJob pj = pservices[2].createPrintJob(); 

[2],而不是得到的打印機作爲pservices'索引的值?

DocPrintJob pj = pservices[printer].createPrintJob(); 

(IM對不起,如果這個心不是來澄清這個問題的正確的地方,但是這是我的第一個問題在這裏,並沒有找到如何以其他任何方式問這個)