温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java打开本地html文件的方法

发布时间:2020-04-30 10:59:08 来源:亿速云 阅读:525 作者:小新 栏目:编程语言

今天小编给大家分享的是java打开本地html文件的方法,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧。一定会有所收获的哦。

第一种:Object获取项目中的properties

InputStream in = Object. class .getResourceAsStream( "/com/demo/conf.properties" );

第二种:直接获得本地配置文件properties

FileInputStream in = new FileInputStream("D:\\work\\demo\\conf.properties"); 
//加载properties文件
Properties prop =  new  Properties();
prop.load(in);
//从配置文件中获取 页面的位置,此处url为网页的绝对路径并解决乱码,如:d:/demo/index.html
 String url = new String(prop.getProperty( "csv_url" ).trim().getBytes("ISO-8859-1"),"gbk");
//根据url打开网页
private static void browse(String url) throws ClassNotFoundException, IllegalAccessException,
IllegalArgumentException, InterruptedException, InvocationTargetException, IOException,NoSuchMethodException {
  String osName = System.getProperty("os.name", "");
    if (osName.startsWith("Windows")) {
        Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
          } else if (osName.startsWith("Mac OS")) {
              Class fileMgr = Class.forName("com.apple.eio.FileManager");
               Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });
                   openURL.invoke(null, new Object[] { url });
                     } else {
                      //  Unix or Linux
                          String[] browsers = { "firefox", "opera", "konqueror", "epiphany", 
                          "mozilla", "netscape" };
    String browser = null;
        for (int count = 0; count < browsers.length && browser == null; count++)
        if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0)
        browser = browsers[count];
        if (browser == null)
        throw new NoSuchMethodException("Could not find web browser");
        else
              Runtime.getRuntime().exec(new String[] { browser, url });
                }}

关于java打开本地html文件的方法就分享到这里了,希望以上内容可以对大家有一定的参考价值,可以学以致用。如果喜欢本篇文章,不妨把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI