我正在使用下面显示的 java 代码在连接到我的计算机的 HP DeskJet1000 USB 打印机上打印文本文件。每当我运行此代码时,都会发送打印作业,但打印机不打印任何内容。状态显示打印机正在打印,但它甚至不占用一页。请帮忙!我的代码如下:
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 ();
}
}
}