我正在研究在网络打印机上用Java实现打印的项目,但未配置打印机。我在打印机服务查找始终返回null时遇到问题。

在Java应用程序中,是否仍可以在未配置的打印机上打印文件?

以下代码无助于检测打印机。

public int printDoc(InputStream is) throws PrintException, FileNotFoundException {
    PrintService printService=PrintServiceLookup.lookupDefaultPrintService();
    DocPrintJob job = printService.createPrintJob();
    job.addPrintJobListener(new PrintJobAdapter() {
        public void printDataTransferCompleted(PrintJobEvent event){
            System.out.println("transfer complete");
        }
        public void printJobNoMoreEvents(PrintJobEvent event){
            System.out.println("received no more events");
        }
    });

      Doc doc=new SimpleDoc(is, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
      // Doc doc=new SimpleDoc(fis, DocFlavor.INPUT_STREAM.JPEG, null);
      PrintRequestAttributeSet attrib=new HashPrintRequestAttributeSet();
      attrib.add(new Copies(1));
      String printeraddr= "ipp://blrprt01.blr.network18.com/23Flr_Printer1";
      PrinterName prName =new PrinterName(printeraddr, null);

      attrib.add(prName);
      job.print(doc, attrib);
    return 0;
}

最佳答案

您需要先配置打印机。

第一行
https://docs.oracle.com/javase/tutorial/2d/printing/printable.html
说选择的打印机

并且请使用PrinterJob而不是PrintServiceLookup。

07-26 00:05
查看更多