package self;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class FileDownLoader {
private static String path = "e:\\html";
/**
* 根据 url 和网页类型生成需要保存的网页的文件名 去除掉 url 中非文件名字符
*/
public String getFileNameByUrl(String url, String contentType) {
url = url.substring(7);// remove http://
if (contentType.indexOf("html") != -1)// text/html
{
url = url.replaceAll("[\\?/:*|<>\"]", "_") + ".html";
return url;
} else// 如application/pdf
{
return url.replaceAll("[\\?/:*|<>\"]", "_") + "."
+ contentType.substring(contentType.lastIndexOf("/") + 1);
}
}
/**
* 保存网页字节数组到本地文件 filePath 为要保存的文件的相对地址
*/
private void saveToLocal(byte[] data, String filePath) {
try {
File fp = new File(path);
if (!fp.exists()) {
fp.mkdir();
}
DataOutputStream out = new DataOutputStream(new FileOutputStream(fp
+ "/" + filePath));
for (int i = 0; i < data.length; i++)
out.write(data[i]);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/* 下载 url 指向的网页 */
public String downloadFile(String url) {
String filePath = null;
/* 1.生成 HttpClinet 对象并设置参数 */
HttpClient httpClient = new HttpClient();
// 设置 Http 连接超时 5s
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(
5000);
/* 2.生成 GetMethod 对象并设置参数 */
GetMethod getMethod = new GetMethod(url);
// System.out.println(getMethod);
// 设置 get 请求超时 5s
getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 2000);
// 设置请求重试处理
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
/* 3.执行 HTTP GET 请求 */
try {
int statusCode = httpClient.executeMethod(getMethod);
// 判断访问的状态码
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ getMethod.getStatusLine());
filePath = null;
}
/* 4.处理 HTTP 响应内容 */
// InputStream responseBody =
// getMethod.getResponseBodyAsStream();//读取为字节liu
//
// ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
//
// bytestream.write(responseBody.read());
//
// byte[] responseByte=bytestream.toByteArray();
String message = null;
byte[] responseBody = getMethod.getResponseBody();
// System.out.println(new String(responseBody));
String charSet = "gb2312";
message = new String(responseBody, charSet);
if (message.toLowerCase().indexOf("charset=utf-8") != -1) {
// message = new String(responseBody, "utf-8");
charSet = "utf-8";
} else if (message.toLowerCase().indexOf("charset=gbk") != -1) {
// message = new String(responseBody, "gbk");
charSet = "gbk";
} else if (message.toLowerCase().indexOf("charset=iso-8859-1") != -1) {
// message = new String(responseBody, "iso-8859-1");
charSet = "iso-8859-1";
}
message = new String(responseBody, charSet);
String catchKey = "QQ";
Pattern p = Pattern.compile(catchKey, Pattern.CASE_INSENSITIVE);
if (p.matcher(message).find()) {
String color = " <span><img src='flag red.gif'/><font color=\"red\">"
+ catchKey + "</font></span>";
color = "sytle=\"color:red\"" + catchKey;
// System.out.println(color);
message = js(message, catchKey, color);
// 根据网页 url 生成保存时的文件名
filePath = getFileNameByUrl(url, getMethod.getResponseHeader(
"Content-Type").getValue());
byte[] bytes = message.getBytes(charSet);
// saveToLocal(responseBody,filePath);
saveToLocal(bytes, filePath);
}
} catch (HttpException e) {
// 发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
} finally {
// 释放连接
getMethod.releaseConnection();
}
return filePath;
}
/**
* 替换所有指定字符
*
* @param htmlContext
* @param message
* @param js
* @return
*/
private static String js(String htmlContext, String message, String js) {
if (message == null)
message = "<script[^>]*?>.*?</script>";
Pattern p = Pattern.compile(message, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(htmlContext);
return m.replaceAll(js);
}
public void downLoadFiles(String url) {
FileDownLoaderSec fdls = new FileDownLoaderSec();
downloadFile(url);
path = path + "\\child";
List<String> urlList = fdls.extracLinks(url);
for (String secUrl : urlList) {
// System.out.println(secUrl);
downloadFile(secUrl);
}
}
// 测试的 main 方法
public static void main(String[] args) {
FileDownLoader downLoader = new FileDownLoader();
downLoader.downLoadFiles("http://www.baidu.com");
}
}
其中fdls.extracLinks(url)的解析方法如下
public static List<String> extracLinks(String url) {
String urlName = url;
urlName = urlName.substring(11);
urlName = urlName.substring(0, urlName.indexOf("."));
//方法最终返回的List
List<String> finalList=new ArrayList<String>();
List<String> urlList = new ArrayList<String>();
Set<String> urlSet=new HashSet<String>();
List<String> listStrings = new ArrayList<String>();
try {
Parser parser = new Parser(url);
parser.setEncoding("gb2312");
// 过滤 <frame> 标签的 filter,用来提取 frame 标签里的 src 属性所、表示的链接
NodeFilter frameFilter = new NodeFilter() {
public boolean accept(Node node) {
if (node.getText().startsWith("frame src=")) {
return true;
} else {
return false;
}
}
};
// OrFilter 来设置过滤 <a> 标签,<img> 标签和 <frame> 标签,三个标签是 or 的关系
OrFilter orFilter = new OrFilter(
new NodeClassFilter(LinkTag.class), new NodeClassFilter(
ImageTag.class));
OrFilter linkFilter = new OrFilter(orFilter, frameFilter);
// 得到所有经过过滤的标签
NodeList list = parser.extractAllNodesThatMatch(linkFilter);
for (int i = 0; i < list.size(); i++) {
Node tag = list.elementAt(i);
if (tag instanceof LinkTag)// <a> 标签
{
LinkTag link = (LinkTag) tag;
String linkUrl = link.getLink();// url\
if(!linkUrl.equals("")
&&!linkUrl.equals("void(0)")
&&!linkUrl.equals("javascript:void(0)")
&&!linkUrl.equals("void(0);")
&&!linkUrl.equals("javascript:void(0);")){
urlSet.add(linkUrl);
}
//String text = link.getLinkText();// 链接文字
//System.out.println(linkUrl + "**********" + text);
} else if (tag instanceof ImageTag)// <img> 标签
{
ImageTag image = (ImageTag) list.elementAt(i);
if(!image.getImageURL().equals("")&&!image.getImageURL().equals("void(0)")&&!image.getImageURL().equals("javascript:void(0)")&&!image.getImageURL().equals("void(0);")&&!image.getImageURL().equals("javascript:void(0);")){
urlList.add(image.getImageURL());
}
// System.out.print(image.getImageURL() + "********");// 图片地址
// System.out.println(image.getText());// 图片文字
} else// <frame> 标签
{
// 提取 frame 里 src 属性的链接如 <frame src="test.html"/>
String frame = tag.getText();
int start = frame.indexOf("src=");
frame = frame.substring(start);
int end = frame.indexOf(" ");
if (end == -1)
end = frame.indexOf(">");
frame = frame.substring(5, end - 1);
if(!frame.equals("")
&&!frame.equals("void(0)")
&&!frame.equals("javascript:void(0)")
&&!frame.equals("void(0);")
&&!frame.equals("javascript:void(0);")){
urlSet.add(frame);
}
}
}
listStrings.addAll(urlSet);
System.out.println(listStrings.size()+"+++++++++++");
for (int i = 0; i < listStrings.size(); i++) {
String urlStr = listStrings.get(i);
//去掉过长的域名(域名长度<200)和站外的域名
if ( urlStr.toUpperCase().length() <= 200&&urlStr.indexOf(urlName) != -1 ) {
finalList.add(urlStr);
}
}
// if(finalList==null||finalList.size()==0){
// extracLinks(url);
// }
return finalList;
} catch (ParserException e) {
e.printStackTrace();
return finalList;
}
}