Java程序打开指定地址网页

1、今天遇到了需要手动输入http地址打开指定网页的需求,试着做一个用程序打开指定网页的功能,搜了一下,还真有一个现成的例子,稍加改造,实现自己的需求;

2、代码不多,两个文件;如下:

package com.lgp.solr;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; public class JavaFile { public static List<String> getUrl(String path) {
List<String> urls = new ArrayList<String>();
try {
FileReader reader = new FileReader(path);
BufferedReader br = new BufferedReader(reader);
String str = null;
while ((str = br.readLine()) != null) {
if(str!=null && str.startsWith("http")){
urls.add(str);
}
}
br.close();
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return urls;
} }

这个类主要作用是读取指定文件的中的url地址,按行读取,过滤以http开头的行内容;

package com.lgp.solr;
/////////////////////////////////////////////////////////
//Bare Bones Browser Launch //
//Version 1.5 (December 10, 2005) //
//By Dem Pilafian //
//支持: Mac OS X, GNU/Linux, Unix, Windows XP//
//可免费使用 //
///////////////////////////////////////////////////////// import java.io.File;
/**
* @author Dem Pilafian
* @author John Kristian
*/
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List; public class BareBonesBrowserLaunch { public static void main(String[] args) { String path = System.getProperty("app.home"); if(path==null || "".equals(path)){
path = System.getProperty("file.path");
if(path==null || "".equals(path))
throw new RuntimeException("未配置app.home和file.path");
}
List<String> urls = new ArrayList<String>(); if(new File(path).isDirectory()){
File dir = new File(path);
for (File file : dir.listFiles()) {
urls.addAll(JavaFile.getUrl(file.getAbsolutePath()));
}
}else{
urls.addAll(JavaFile.getUrl(path));
} for (String url : urls) {
openURL(url);
} } public static void openURL(String url) {
try {
browse(url);
} catch (Exception e) {
}
} @SuppressWarnings({ "rawtypes", "unchecked" })
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的打开方式。
/*String browspath = System.getProperty("brows.path");
try {
if(browspath != null){
Runtime.getRuntime().exec("browspath " + url);
}
//Runtime.getRuntime().exec("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe " + url);
} catch (Exception e) {
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
}
*/
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 });
}
}
}

这是主类,适用于mac和Linux,mac系统,很强大;其中主要使用windows系统,打开默认浏览器;

3、打成可执行的jar包:注意设置main方法的路径,如图

Java程序打开指定地址网页      Java程序打开指定地址网页

从图1一路Next,设置jar包路径后,之后再继续设置图2,最后Finish;

4、通过bat文件运行jar:

run.bat文件:当让前提是设置了javahome和classpath等;

set dir=%CD%
java -Dapp.home=%CD%\config -jar %CD%\auto.jar

在jar的所在路径新建config文件夹,所以配置文件放到此文件夹内,点击run.bat测试运行结果。

上一篇:【代码笔记】iOS-NSNotificationCenter


下一篇:Winform常用操作