Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 打印可以在Linux上工作,但不能在Windows上工作_Java_Windows_Tomcat_Printing - Fatal编程技术网

Java 打印可以在Linux上工作,但不能在Windows上工作

Java 打印可以在Linux上工作,但不能在Windows上工作,java,windows,tomcat,printing,Java,Windows,Tomcat,Printing,我正在开发一个web应用程序,用于打印pdf文件(来自ByteArrayOutputStream)。 代码如下: PrintService service = null; for (String printer : listPrinters){ for( PrintService printService : printServices ) { Attribute[] attrs = printService.getAttributes().toArray();

我正在开发一个web应用程序,用于打印pdf文件(来自ByteArrayOutputStream)。
代码如下:

PrintService service = null;
for (String printer : listPrinters){
    for( PrintService printService : printServices ) {
        Attribute[] attrs = printService.getAttributes().toArray();
        for (int j=0; j<attrs.length; j++) {
            String attrName = attrs[j].getName();
            String attrValue = attrs[j].toString();
            if (attrName.equals("printer-name")){
                if (attrValue.equals(printer)){
                    service = printService;
                }
            }
        }
    }
}
try {
    InputStream fi = new ByteArrayInputStream(baos.toByteArray());

    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    DocPrintJob printJob = service.createPrintJob();
    Doc doc = new SimpleDoc(fi, flavor, null);
    printJob.print(doc, null);

} 
catch (Exception e1) {
    log.debug(e1.getMessage());
}

尝试调用
baos.flush()
baos.close()
,然后从
ByteArrayOutputStream
的字节创建
InputStream
,它也不起作用。我尝试过FileInputStream fis=newFileInputStream(“C:/mypdf.pdf”);,但同样的问题。您可以尝试通过将
DocFlavor
设置为
DocFlavor.INPUT\u STREAM.PDF
来避免自动感知。不过,我认为这意味着打印机必须具有对PDF打印的本机支持。您也可以尝试
DocFlavor.BYTE\u ARRAY.PDF
避免
InputStream
创建。。。您的驱动程序必须支持PDF打印,才能实现上述所有功能。在Linux中,更常见的是驱动程序支持它。请确保您的Windows驱动程序支持PDF打印。如果较新版本支持,可能会更新驱动程序。这一讨论揭示了一些问题:其他Docflavor也存在同样的问题。简单的文本文件也会出现问题。
FileInputStream fis = new FileInputStream("C:/mytxt.txt");
Doc pdfDoc = new SimpleDoc(fis, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
DocPrintJob printJob = service.createPrintJob();
printJob.print(pdfDoc, new HashPrintRequestAttributeSet());
fis.close();