我想使用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);
}
}
有人可以帮助我吗?