Java-打印HTML文件不打印

Java-打印HTML文件不打印,java,Java,我正在使用中提到的方法,这看起来很有希望,答案的作者还说它在Windows和Linux上都适用 代码如下: PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor

我正在使用中提到的方法,这看起来很有希望,答案的作者还说它在Windows和Linux上都适用

代码如下:

PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
PrintService service = ServiceUI.printDialog(
        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(),
        200, 200, printService, defaultService, flavor, pras);
if (service != null) {
    DocPrintJob job = service.createPrintJob();
    FileInputStream fis = new FileInputStream(file);
    DocAttributeSet das = new HashDocAttributeSet();
    Doc doc = new SimpleDoc(fis, flavor, das);
    job.print(doc, pras);
    fis.close();
}
每当我想打印一个HTML文件时,该文件就会被添加到打印机的队列中,并立即被删除(没有任何错误/警告等)。这在Windows 10和8.1上都会发生,并且打印机是HP激光打印机

这个代码适用于任何人吗? API中是否有任何更改导致代码不再正常工作?这些问题是否可能与打印机和/或操作系统有关

我还尝试用这段代码打印非HTML文件,但它也不会打印任何文件(例如,PDF)

编辑:我可以通过使用
Desktop.getDesktop().print()
并将Internet Explorer设置为打开.html文件的标准程序来解决问题。当用户将Google Chrome或Firefox作为html文件的标准程序时,它不起作用