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. }