java 调用浏览器学习

来源:互联网 发布:管家婆软件好用吗 编辑:程序博客网 时间:2024/06/06 01:07

1、org.eclipse.swt.browser.Browser 方式调用


Shell shell = new Shell(SWT.APPLICATION_MODAL | SWT.CLOSE);shell.setLayout(new FillLayout());shell.setMaximized(true);Browser browser = new Browser(shell, SWT.NONE);browser.setBounds(0, 0, 950, 600);browser.setUrl(sb.toString());browser.setVisible(true);shell.open();

2、启用 cmd 方式打开

String str = "cmd /c start iexplore " + url;          try {              Runtime.getRuntime().exec(str);          } catch (IOException e1) {              e1.printStackTrace();          }  

3、调用默认浏览器方式打开

//启用系统默认浏览器来打开网址。          try {              URI uri = new URI(sb.toString());              Desktop.getDesktop().browse(uri);          } catch (URISyntaxException e2) {              e2.printStackTrace();          } catch (IOException e3) {              e3.printStackTrace();          }  


4、指定IE路径方式来打开


// 指定IE路径方式来打开网址String iepath1 = "C:\\Program Files\\Internet Explorer\\iexplore.exe";        String iepath2 = "C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe";        final Runtime runtime = Runtime.getRuntime();      Process process = null;          File file = new File(iepath1);        if(file.exists()) {        process = runtime.exec(iepath1+" " + sb.toString());        } else {        File file2 = new File(iepath2);        process = runtime.exec(iepath2+" " + sb.toString());        }