1.一个根据动态页路径和编码格式获得html格式文本的方法源代码:
Java代码
1. // 返回html代码
2. public final static String getHtmlCode(String httpUrl, String ecode) {
3. // 构造HttpClient的实例
4. HttpClient httpClient = new HttpClient();
5. // 创建GET方法的实例
6. GetMethod getMethod = new GetMethod(httpUrl);
7. // 使用系统提供的默认的恢复策略
8. getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
9. new DefaultHttpMethodRetryHandler());
10. try {
11. // 执行getMethod
12. int statusCode = httpClient.executeMethod(getMethod);
13. if (statusCode != HttpStatus.SC_OK) {
14. System.err.println("Method failed: "
15. + getMethod.getStatusLine());
16. }
17. // 读取内容
18. byte[] responseBody = getMethod.getResponseBody();
19. // 处理内容
20. return new String(responseBody, ecode);
21. } catch (HttpException e) {
22. // 发生致命的异常,可能是协议不对或者返回的内容有问题
23. System.out.println("Please check your provided http address!");
24. e.printStackTrace();
25. } catch (IOException e) {
26. // 发生网络异常
27. e.printStackTrace();
28. } finally {
29. // 释放连接
30. getMethod.releaseConnection();
31. }
32. return null;
33. }
// 返回html代码
public final static String getHtmlCode(String httpUrl, String ecode) {
// 构造HttpClient的实例
HttpClient httpClient = new HttpClient();
// 创建GET方法的实例
GetMethod getMethod = new GetMethod(httpUrl);
// 使用系统提供的默认的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
try {
// 执行getMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
// 读取内容
byte[] responseBody = getMethod.getResponseBody();
// 处理内容
return new String(responseBody, ecode);
} catch (HttpException e) {
// 发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
} finally {
// 释放连接
getMethod.releaseConnection();
}
return null;
}
2.将html文本写入一个文件中的方法(para1:保存文件名 para2:html文本内容)
Java代码
1. public final static void saveHtmlCode(String filePath, String htmlCode) {
2. FileOutputStream fOut = null;
3. OutputStreamWriter out = null;
4. try {
5. fOut = new FileOutputStream(filePath);
6. out = new OutputStreamWriter(fOut, "UTF-8");
7. out.write(htmlCode);
8. } catch (Exception e) {
9. e.printStackTrace();
10. } finally {
11. try {
12. out.flush();
13. } catch (IOException e) {
14. e.printStackTrace();
15. }
16. try {
17. fOut.flush();
18. } catch (IOException e) {
19. e.printStackTrace();
20. }
21. try {
22. out.close();
23. } catch (IOException e) {
24. e.printStackTrace();
25. }
26. try {
27. fOut.close();
28. } catch (IOException e) {
29. e.printStackTrace();
30. }
31. }
32. }
public final static void saveHtmlCode(String filePath, String htmlCode) {
FileOutputStream fOut = null;
OutputStreamWriter out = null;
try {
fOut = new FileOutputStream(filePath);
out = new OutputStreamWriter(fOut, "UTF-8");
out.write(htmlCode);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.下面是一段伪代码.根据当前时间现在c盘建立要保存文件的文件夹,然后保存文件
Java代码
1. public static void main(String[] args) {
2. String msgSaveFile = countHtmlFilePath("c://", new Date()) + "/test.html";
3. String messageHtmlCode = getHtmlCode("test.jsp", "UTF-8");
4. saveHtmlCode(msgSaveFile, messageHtmlCode);
5. }
6.
7. // 获得存储文件的目录
8. public final static String countHtmlFilePath(String url, Date date) {
9. SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd-hh");
10. String time = sim.format(date);
11. String[] result = time.split("-");
12. String year = result[0];
13. String month = result[1];
14. String day = result[2];
15. String hour = result[3];
16. // 首先判断是否有年文件夹
17. MyFileFilter filter = new TestClass().new MyFileFilter(true);
18. boolean isExists = filter.isExistsDirectory(url, year + "");
19. if (isExists) {
20. if(filter.isExistsDirectory(url + "/" + year, month + "")) {
21. if(filter.isExistsDirectory(url + "/" + year + "/" + month, day + "")) {
22. if(!filter.isExistsDirectory(url + "/" + year + "/" + month + "/" + hour, hour + "")) {
23. new File(url + "/" + year + "/" + month + "/" + day + "/" + hour).mkdir();
24. }
25. } else {
26. new File(url + "/" + year + "/" + month + "/" + day).mkdir();
27. }
28. } else {
29. new File(url + "/" + year + "/" + month).mkdir();
30. }
31. } else {
32. new File(url + "/" + year).mkdir();
33. new File(url + "/" + year + "/" + month).mkdir();
34. new File(url + "/" + year + "/" + month + "/" + day).mkdir();
35. new File(url + "/" + year + "/" + month + "/" + day + "/" + hour).mkdir();
36. }
37. return url + "/" + year + "/" + month + "/" + day + "/" + hour;
38. }
39.
40.
41. class MyFileFilter implements java.io.FileFilter {
42.
43. private boolean isDirectory;
44.
45. public MyFileFilter(boolean isDir) {
46. this.isDirectory = isDir;
47. }
48.
49. @Override
50. public boolean accept(File f) {
51. if (f.isDirectory() == isDirectory) {
52. return true;
53. } else
54. return false;
55. }
56.
57. public boolean isExistsDirectory(String path, String dir) {
58. File file = new File(path);
59. File[] childFiles = file.listFiles(this);
60. for (File cFile : childFiles) {
61. if(cFile.equals(dir)) {
62. return true;
63. }
64. }
65. return false;
66. }
67. }
分享到:
相关推荐
HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议 本帮助文档是chm格式,方便使用
用HtmlClient4.3和HTMLParse 实现爬虫
#html5QQClient#使用HTML5+ 开发的第三方手机QQ客户端代码地址 ##概述使用HTML5+ 来实现这个QQ客户端还得从 这个编辑器开始说起其实从上个学期开始我就有接触过这款编辑器。听说语法补全功能特别好用我就试了试,...
包括lucene,包,tika包,IKAnalyzer包,UML建模工具,htmlClient包,htmlParser包,外观梅花substance包 ,mysql连接包
代码有非常详细地注释,下载java相关包可以直接运行,希望能为将要和打算要学习HtmlClient的人提供一些前期的帮助。
网络爬虫,初学必看,用链表实现贮存待捉的url队列,用堆栈检查url是否已经捉了。用了HtmlClient和HtmlParser实现。
基于htmlparser和htmlclient的java搜索ed2k工具,然后你也可以改了搜索其他内容,并按标题和内容存入数据库,web部分还未完善先上传了
分享给大家供大家参考,具体如下: ...转换出来的格式相对还是有些粗糙,不喜勿喷。...class HTMLClient: #获取html网页源码 def GetPage(self, url): #user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows N