java 调用浏览器学习

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());
				        }


猜你喜欢

转载自blog.csdn.net/u011984172/article/details/72724219
今日推荐