简体   繁体   English

如何在Java中直接在网络打印机上打印文件。在运行应用程序的系统中未配置打印机?

[英]How to print a file directly on network printer in java.. Printer is not configured in system where application is running?

I am working on project to implementing print in java on network printer, printer is not configured. 我正在研究在网络打印机上用Java实现打印的项目,未配置打印机。 I am having problem while printer service lookup always returning null. 我在打印机服务查找始终返回null时遇到问题。

in Java application, is there anyway to print a file on printer which not configured? 在Java应用程序中,是否仍可以在未配置的打印机上打印文件?

code below is not helping to detect printer. 以下代码无法帮助检测打印机。

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;
}

You need to configure the printer first. 您需要先配置打印机。

first line at https://docs.oracle.com/javase/tutorial/2d/printing/printable.html says selected printer https://docs.oracle.com/javase/tutorial/2d/printing/printable.html的第一行表示已选择打印机

And please use PrinterJob instead of PrintServiceLookup . 并且请使用PrinterJob而不是PrintServiceLookup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM