最近项目中需要做用程序打开浏览器,并且传递相应的url地址到浏览器,即打开浏览器并且访问指定url的网页。根据网上整理如下几个办法。
下面列举了几个例子,是我测试过,可以使用的,比如:
/*******************************************************************************
* @project: OpenExplorer
* @package: com.burns.test
* @file: OpenExplorer.java
* @author: Administrator
* @created: 2017-9-8
* @purpose:
*
* @version: 1.0
*
* Revision History at the end of file.
*
* Copyright 2017 AcconSys All rights reserved.
******************************************************************************/
package com.burns.test;
/**
* 利用java.lang.ProcessBuilder类创建系统进程的能力,通过浏览器地址启动浏览器,并将网址作为参数传送给浏览器。
* ProcessBuilder类是J2SE
* 1.5在java.lang中新添加的一个新类,此类用于创建操作系统进程,它提供一种启动和管理进程(也就是应用程序)的方法。
*
* @author Administrator
*
*/
public class OpenExplorerTest1 {
public static void main(String args[]) {
try {
ProcessBuilder proc = new ProcessBuilder(
"C:\\Program Files\\Internet Explorer\\iexplore.exe",
"http://blog.csdn.net/");
proc.start();
} catch (Exception e) {
System.out.println("Error executing progarm.");
}
}
}
/*******************************************************************************
* Revision History
* [type 'revision' and press Alt + / to insert revision block]
*
*
*
* Copyright 2017 AcconSys All rights reserved.
******************************************************************************/
/*******************************************************************************
* @project: OpenExplorer
* @package: com.burns.test
* @file: OpenExplorer.java
* @author: Administrator
* @created: 2017-9-8
* @purpose:
*
* @version: 1.0
*
* Revision History at the end of file.
*
* Copyright 2017 AcconSys All rights reserved.
******************************************************************************/
package com.burns.test;
/**
* java调用系统默认浏览器打开链接
* @author Administrator
*
*/
public class OpenExplorerTest2 {
public static void main(String args[]) {
if (java.awt.Desktop.isDesktopSupported()) {
try {
// 创建一个URI实例
java.net.URI uri = java.net.URI.create("http://www.cnblogs.com/lsgwr/");
// 获取当前系统桌面扩展
java.awt.Desktop dp = java.awt.Desktop.getDesktop();
// 判断系统桌面是否支持要执行的功能
if (dp.isSupported(java.awt.Desktop.Action.BROWSE)) {
// 获取系统默认浏览器打开链接
dp.browse(uri);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/*******************************************************************************
* Revision History
* [type 'revision' and press Alt + / to insert revision block]
*
*
*
* Copyright 2017 AcconSys All rights reserved.
******************************************************************************/
/*******************************************************************************
* @project: OpenExplorer
* @package: com.burns.test
* @file: OpenExplorer.java
* @author: Administrator
* @created: 2017-9-8
* @purpose:
*
* @version: 1.0
*
* Revision History at the end of file.
*
* Copyright 2017 AcconSys All rights reserved.
******************************************************************************/
package com.burns.test;
import java.io.IOException;
/**
* java调用系统默认浏览器打开链接
*
* @author Administrator
*
*/
public class OpenExplorerTest3 {
public static void main(String args[]) {
try {
Runtime.getRuntime().exec(
"cmd /c start http://bbs.csdn.net/topics/390157559 ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*******************************************************************************
* Revision History
* [type 'revision' and press Alt + / to insert revision block]
*
*
*
* Copyright 2017 AcconSys All rights reserved.
******************************************************************************/
/*******************************************************************************
* @project: OpenExplorer
* @package: com.burns.test
* @file: OpenExplorer.java
* @author: Administrator
* @created: 2017-9-8
* @purpose:
*
* @version: 1.0
*
* Revision History at the end of file.
*
* Copyright 2017 AcconSys All rights reserved.
******************************************************************************/
package com.burns.test;
import java.io.IOException;
import java.lang.reflect.Method;
/**
* java调用系统默认浏览器打开链接
*
* @author Administrator
*
*/
public class OpenExplorerTest4 {
// 主方法 测试类
public static void main(String[] args) {
String url = "http://iteye.blog.163.com/";
try {
OpenExplorerTest4.browse(url);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void browse(String url) throws Exception {
// 获取操作系统的名字
String osName = System.getProperty("os.name", "");
if (osName.startsWith("Mac OS")) {
// 苹果的打开方式
Class fileMgr = Class.forName("com.apple.eio.FileManager");
Method openURL = fileMgr.getDeclaredMethod("openURL",
new Class[] { String.class });
openURL.invoke(null, new Object[] { url });
} else if (osName.startsWith("Windows")) {
// windows的打开方式。
Runtime.getRuntime().exec(
"rundll32 url.dll,FileProtocolHandler " + url);
} else {
// Unix or Linux的打开方式
String[] browsers = { "firefox", "opera", "konqueror", "epiphany",
"mozilla", "netscape" };
String browser = null;
for (int count = 0; count < browsers.length && browser == null; count++)
// 执行代码,在brower有值后跳出,
// 这里是如果进程创建成功了,==0是表示正常结束。
if (Runtime.getRuntime()
.exec(new String[] { "which", browsers[count] })
.waitFor() == 0)
browser = browsers[count];
if (browser == null)
throw new Exception("Could not find web browser");
else
// 这个值在上面已经成功的得到了一个进程。
Runtime.getRuntime().exec(new String[] { browser, url });
}
}
}
/*******************************************************************************
* Revision History
* [type 'revision' and press Alt + / to insert revision block]
*
*
*
* Copyright 2017 AcconSys All rights reserved.
******************************************************************************/
一下摘自其他的网页简要:
Runtime.getRuntime.exec("C:\Program Files\Internet Explorer\iexplore xxx"); //xxx是url加参数
或者用直接用 java.awt.Desktop.getDesktop().brower(new URI(xxx));
如果不需要打开浏览器,方法很多,,还可以用URLConnection,或者调用第三方程序如wget,curl
之类的
打开页面可以用Desktop.getDesktop().browse(new URL("http://www.baidu.com").toURI());
向网页提交参数可以用Java开源组件:HttpClient,想POST啥都行。
HttpClient httpClient = new HttpClient();
httpClient.getHostConfiguration().setHost("mail.163.com",80,"http");
PostMethod postMethod = new PostMethod();
NameValuePair username = new NameValuePair("userName", "xxx");
NameValuePair userpassword = new NameValuePair("password", "xxx");
postMethod.setRequestBody(new NameValuePair[] { username,userpassword});
int statusCode = httpClient.executeMethod(postMethod);