如何使用 Java 打开 HTML 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20517434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:29:25  来源:igfitidea点击:

How to Open HTML file using Java?

java

提问by Wanna Coffee

I try to open HTML file from local (In my system) by Java program. I tried some of the program got through stack overflow but its not working as much.

我尝试通过 Java 程序从本地(在我的系统中)打开 HTML 文件。我尝试了一些程序通过堆栈溢出但它没有那么多工作。

For E.G.:I have this small HTML file.

对于 EG:我有这个小的 HTML 文件。

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

My Java code:

我的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();

Any solution or tips appreciated.

任何解决方案或提示表示赞赏。

采纳答案by RamonBoza

I would prefer to use default browser

我更喜欢使用默认浏览器

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

回答by Troyseph

Here is the code for a method that fails gracefully.

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

Note that the string can be the location of an htmlfile.

请注意,字符串可以是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);
        }
}

回答by omkar sirra

URI oURL = new URI(url);
Desktop.getDesktop().browse(oURL);

Apart from that, make sure that file is already opened in your desired browser. Check the icon on the file, If it is showing like a text file, you might have already opened with Text file. So change the default program to the desired program.

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