2012-04-27 56 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对不起,如果这个心不是来澄清这个问题的正确的地方,但是这是我的第一个问题在这里,并没有找到如何以其他任何方式问这个)