Java调用浏览器打开指定页面的5种方法(最全)

  1. package com.xbz;

  2.  
  3. import java.awt.*;

  4. import java.lang.reflect.Method;

  5. import java.net.URI;

  6. import java.util.Map;

  7.  
  8. /**

  9. * @title java调用浏览器打开指定页面

  10. * @author Xingbz

  11. */

  12. public class XDemo {

  13.  
  14. private static void browse1(String url) throws Exception {

  15. String osName = System.getProperty("os.name", "");// 获取操作系统的名字

  16.  
  17. if (osName.startsWith("Windows")) {// windows

  18. Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);

  19. } else if (osName.startsWith("Mac OS")) {// Mac

  20. Class fileMgr = Class.forName("com.apple.eio.FileManager");

  21. Method openURL = fileMgr.getDeclaredMethod("openURL", String.class);

  22. openURL.invoke(null, url);

  23. } else {// Unix or Linux

  24. String[] browsers = {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape"};

  25. String browser = null;

  26. for (int count = 0; count < browsers.length && browser == null; count++) { // 执行代码,在brower有值后跳出,

  27. // 这里是如果进程创建成功了,==0是表示正常结束。

  28. if (Runtime.getRuntime().exec(new String[]{"which", browsers[count]}).waitFor() == 0) {

  29. browser = browsers[count];

  30. }

  31. }

  32.  
  33. if (browser == null) {

  34. throw new RuntimeException("未找到任何可用的浏览器");

  35. } else {// 这个值在上面已经成功的得到了一个进程。

  36. Runtime.getRuntime().exec(new String[]{browser, url});

  37. }

  38. }

  39. }

  40.  
  41. /**

  42. * @title 使用默认浏览器打开

  43. * @author Xingbz

  44. */

  45. private static void browse2(String url) throws Exception {

  46. Desktop desktop = Desktop.getDesktop();

  47. if (Desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE)) {

  48. URI uri = new URI(url);

  49. desktop.browse(uri);

  50. }

  51. }

  52.  
  53. /**

  54. * @title 通过获取环境变量的浏览器路径, 然后启动浏览器

  55. * @author Xingbz

  56. */

  57. private static void browse3(String url) throws Exception {

  58. String firefox = "C:\\Program Files\\Mozilla Firefox\\firefox.exe";

  59. Map map = System.getenv();

  60. for (Object key : map.keySet()) {

  61. String value = (String) map.get(key);

  62. if (value.contains("firefox.exe")) {

  63. firefox = value;

  64. break;

  65. }

  66. }

  67. Runtime.getRuntime().exec(new String[]{firefox, url});

  68. }

  69.  
  70. /**

  71. * @title 启用cmd运行IE的方式来打开网址

  72. * @author Xingbz

  73. */

  74. private static void browse4(String url) throws Exception {

  75. Runtime.getRuntime().exec("cmd /c start " + url);//启用cmd运行默认浏览器

  76. // Runtime.getRuntime().exec("cmd /c start iexplore " + url);//启用cmd运行IE

  77. }

  78.  
  79. /**

  80. * @title 利用java.lang.ProcessBuilder类创建系统进程的能力,通过浏览器地址启动浏览器,并将网址作为参数传送给浏览器。

  81. * ProcessBuilder类是J2SE1.5在java.lang中新添加的一个新类,此类用于创建操作系统进程,它提供一种启动和管理进程(也就是应用程序)的方法。

  82. * @author Xingbz

  83. */

  84. private static void browse5(String url) throws Exception {

  85. ProcessBuilder proc = new ProcessBuilder("C:\\Program Files\\Mozilla Firefox\\firefox.exe",

  86. url);

  87. proc.start();

  88. }

  89.  
  90.  
  91. public static void main(String[] args) throws Exception {

  92. // browse1("https://blog.csdn.net/xingbaozhen1210");

  93. // browse2("https://blog.csdn.net/xingbaozhen1210");

  94. // browse3("https://blog.csdn.net/xingbaozhen1210");

  95. // browse4("https://blog.csdn.net/xingbaozhen1210");

  96. browse5("https://blog.csdn.net/xingbaozhen1210");

  97.  
  98. }

  99. }

猜你喜欢

转载自blog.csdn.net/dqswuyundong/article/details/82632846