您的位置:首页 > 运维架构 > 网站架构

Windows Linux下Java编程使用默认浏览器打开指定网页 网站 URL

2011-10-20 15:27 656 查看
Windows 和 Linux系统下,Java使用默认浏览器打开指定的网页或者网站。

经测试Linux下需要在URL上添加“http://”而Windows下则可以省略

源代码如下:

//Windows platform goto url
	private void gotoUrlWindows(String url){
		String cmd = "rundll32 url.dll,FileProtocolHandler " + url;
		try {
			Runtime.getRuntime().exec(cmd);
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	
	//Linux platform goto url(need "http://")
	private void gotoUrlLinux(String url){		
		try {
			URI uri = new URI(url);
			java.awt.Desktop.getDesktop().browse(uri);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//goto url
	public void gotoUrl(String url){
		if((System.getProperty("os.name").toUpperCase()).indexOf("WINDOWS")!=-1){	//is Windows platform
			this.gotoUrlWindows(url);
		}
		else if((System.getProperty("os.name").toUpperCase()).indexOf("LINUX")!=-1){	//is Linux Platform 
			this.gotoUrlLinux(url);
		}
	}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: