在java中打印HTML文件

我正在尝试打印我的程序生成的HTML文件,但它不起作用。 在Ubuntu上,“。isSupported(Desktop.Action.PRINT)”返回false,即使我安装了gnome库,并且在Windows 7上,java抛出以下exception:

java.io.IOException: Failed to print file:/C:/Users/user/Documents/document.html. Error message: Unspecified error 

然后是堆栈跟踪。 下面是代码,我正在使用java.awt.Desktop。

 File doc = DocumentComposer.writeDocument(new File(System.getProperty("user.dir") + File.separator + "docs" + File.separator + docName + ".html"), case, data); if (Desktop.isDesktopSupported()) { Desktop desktop = Desktop.getDesktop(); if (desktop.isSupported(Desktop.Action.PRINT)) { desktop.print(doc); } else printError(); } else printError(); 

任何forms的帮助将非常感激:)。

我最终没有使用java.awt.Desktop,它根本行不通。 相反,我遵循了这个IBM教程中的说明, http://www.ibm.com/developerworks/java/library/j-mer0322/ 。 确切地说,我现在使用的代码如下(它在Linux和Windows上运行完美!):

 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(doc); DocAttributeSet das = new HashDocAttributeSet(); Doc document = new SimpleDoc(fis, flavor, das); job.print(document, pras); }