【问题标题】:How to Open HTML file using Java?如何使用 Java 打开 HTML 文件?
【发布时间】:2013-12-29 07:48:41
【问题描述】:

我尝试通过 Java 程序从本地(在我的系统中)打开 HTML 文件。我尝试了一些程序通过堆栈溢出,但它的工作量不大。

例如:我有这个小的 HTML 文件。

<html>
  <head> 
    Test Application
  </head>
  <body>
     This is test application
  </body>
</html>

我的 Java 代码:

Runtime rTime = Runtime.getRuntime();
String url = "D:/hi.html";
String browser = "C:/Program Files/Internet Explorer/iexplore.exe ";
Process pc = rTime.exec(browser + url);
pc.waitFor();

感谢任何解决方案或提示。

【问题讨论】:

  • “如何在 chrome 中打开 HTML 文件..?” 为什么选择“Chrome”而不是用户的默认浏览器?

标签: java


【解决方案1】:
URI oURL = new URI(url);
Desktop.getDesktop().browse(oURL);

除此之外,请确保该文件已在您所需的浏览器中打开。 检查文件上的图标,如果它像文本文件一样显示,您可能已经用文本文件打开了。 所以将默认程序更改为所需的程序。

【讨论】:

    【解决方案2】:

    这是一个正常失败的方法的代码。

    请注意,该字符串可以是html 文件的位置。

    /**
    * If possible this method opens the default browser to the specified web page.
    * If not it notifies the user of webpage's url so that they may access it
    * manually.
    * 
    * @param url
    *            - this can be in the form of a web address (http://www.mywebsite.com)
    *            or a path to an html file or SVG image file e.t.c 
    */
    public static void openInBrowser(String url)
    {
        try
            {
                URI uri = new URL(url).toURI();
                Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
                if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE))
                    desktop.browse(uri);
            }
        catch (Exception e)
            {
                /*
                 *  I know this is bad practice 
                 *  but we don't want to do anything clever for a specific error
                 */
                e.printStackTrace();
    
                // Copy URL to the clipboard so the user can paste it into their browser
                StringSelection stringSelection = new StringSelection(url);
                Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
                clpbrd.setContents(stringSelection, null);
                // Notify the user of the failure
                WindowTools.informationWindow("This program just tried to open a webpage." + "\n"
                    + "The URL has been copied to your clipboard, simply paste into your browser to access.",
                        "Webpage: " + url);
            }
    }
    

    【讨论】:

      【解决方案3】:

      我更喜欢使用默认浏览器

      File htmlFile = new File(url);
      Desktop.getDesktop().browse(htmlFile.toURI());
      

      【讨论】:

      • 我没有否定你的问题,也不知道为什么有人这样做,你的问题100%有效,所以我投票赞成xD
      • 如果用户已经为“html”之类的文件扩展名分配了自定义“打开方式”操作,那么这将不会打开浏览器,但用户已将其链接到的程序......这个根本不是解决方案!
      • URI uri = new URL(url).toURI(); Desktop.getDesktop().browse(uri);使用 File 对我不起作用,使用 url 似乎有效
      • @thesaint 这怎么不是解决方案?从未指定过这样的要求。
      • mac中打开方式是什么
      猜你喜欢
      • 1970-01-01
      • 2015-01-30
      • 2017-01-11
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      • 2016-08-29
      • 2014-11-27
      相关资源
      最近更新 更多