繁体   English   中英

Java是否有平台无关的代码来调用默认应用程序以查看诸如PDF,DOCX,JPEG等文档类型?

[英]Is there platform-agnostic code in Java to invoke the default app to view document types like PDF, DOCX, JPEG etc.?

我想编写一个Java类(或方法),其职责是在任何平台上调用标准/默认应用程序,以打开和处理特定格式的文件,例如PDF,DOCX,XLSX,JPG等,并且还要知道是否提供了适合该文件格式的应用程序。

是否有一种不错的跨平台方法?

老实说...答案并不是什么大秘密,毕竟自Java 1.6以来,所有的Desktop Class都已存在。

要使用文件的关联应用程序(例如:带有MS WORD的 .docx或系统与该文件关联的任何应用程序)打开文件,则可以使用Desktop.getDesktop()。open()方法。 此方法应与Desktop.isDesktopSupported()方法相结合,以确保当前平台支持Desktop类。 这是一个演示上述桌面类方法的用法的小方法:

public void runFile (String filePath) throws IOException {
    File myFile = new File(filePath);
    //Test whether the Desktop class is supported on the current platform.
    if (Desktop.isDesktopSupported()) {
        // Open the file in its associated application:
        Desktop.getDesktop().open(myFile);
    }
    else {
        // Desktop Not Supported...
        System.err.println("runFile() method error! The Desktop Class " +
                           "is not supported on this platform!");
    }
}

暂无
暂无

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

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