简体   繁体   English

在java中使用默认浏览器打开PDF文件

[英]Open PDF file with default browser in java

Snippet of the code代码片段

    public class AnyPlatformAppPDF {

        public static void main(String[] args) {

          try {

            File pdfFile = new File("c:\\Users\\ADMIN\\Desktop\\css\\Praveen_Profile.pdf");
            if (pdfFile.exists()) {

                if (Desktop.isDesktopSupported()) {
                    Desktop.getDesktop().open(pdfFile);
                } else {
                    System.out.println("Awt Desktop is not supported!");
                }

            } else {
                System.out.println("File is not exists!");
            }

            System.out.println("Done");

          } catch (Exception ex) {
            ex.printStackTrace();
          }

        }

        public static void openWebpage(java.net.URI uri) {
            Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
            if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
                try {
                    desktop.browse(uri);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

I tried this code to open pdf file in browser but it doesn't open the pdf file.我试过这段代码在浏览器中打开pdf文件,但它没有打开pdf文件。 I am using Java to do so.我正在使用 Java 这样做。 How can I fix this?我怎样才能解决这个问题?

Using process builder may solve your problem;使用流程构建器可能会解决您的问题;

//Windows //视窗

ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/C", "explorer c:\\Users\\ADMIN\\Desktop\\css\\Praveen_Profile.pdf");

//Linux //Linux

ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash", "-c", "sensible-browser c:\\Users\\ADMIN\\Desktop\\css\\Praveen_Profile.pdf");

// //

processBuilder.start();

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

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