在浏览器Java中关闭或修改当前选项卡URL

时间:2018-09-26 09:52:24

标签: java file web browser

我想使用Java程序在浏览器中关闭或修改当前的标签URL。 这是在浏览器中打开文件的代码(我希望它是跨平台的):

public static void openInWebBrowser(String url) {

    File tmpfile = null;
    try {
        tmpfile = File.createTempFile("tempBrwsr", ".html");
        System.out.println(tmpfile.getAbsolutePath());
        tmpfile.deleteOnExit();
        BufferedWriter bw = new BufferedWriter(new FileWriter(tmpfile));
        bw.write("<html><head><meta http-equiv=\"refresh\" content=\"0;url=file:///" + url + "\" /></head></html>");
        bw.close();
    } catch (IOException e) {
        System.err.println("[ err. ] " + e);
    }

    String os = System.getProperty("os.name").toLowerCase();
    Runtime rt = Runtime.getRuntime();
    if (os.indexOf("win") >= 0) {
        try {
            rt.exec("rundll32 URL.dll,FileProtocolHandler \"" + tmpfile.toURI().toString() + "\"");
        } catch (IOException e) {
            System.err.println("[ err. ] " + e);
        }
    } else if (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0) {
        try {
            rt.exec("xdg-open " + url);
        } catch (IOException e) {
            System.err.println("[ err. ] " + e);
        }
    } else {
        JOptionPane.showMessageDialog(null, "Not supported operating system : " + os, "Error",
                JOptionPane.ERROR_MESSAGE);
    }

}

有人可以帮助我吗?

0 个答案:

没有答案