1.一个根据动态页路径和编码格式获得html格式文本的方法源代码:
// 返回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文本内容)
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盘建立要保存文件的文件夹,然后保存文件
public static void main(String[] args) {
String msgSaveFile = countHtmlFilePath("c://", new Date()) + "/test.html";
String messageHtmlCode = getHtmlCode("test.jsp", "UTF-8");
saveHtmlCode(msgSaveFile, messageHtmlCode);
}
// 获得存储文件的目录
public final static String countHtmlFilePath(String url, Date date) {
SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd-hh");
String time = sim.format(date);
String[] result = time.split("-");
String year = result[0];
String month = result[1];
String day = result[2];
String hour = result[3];
// 首先判断是否有年文件夹
MyFileFilter filter = new TestClass().new MyFileFilter(true);
boolean isExists = filter.isExistsDirectory(url, year + "");
if (isExists) {
if(filter.isExistsDirectory(url + "/" + year, month + "")) {
if(filter.isExistsDirectory(url + "/" + year + "/" + month, day + "")) {
if(!filter.isExistsDirectory(url + "/" + year + "/" + month + "/" + hour, hour + "")) {
new File(url + "/" + year + "/" + month + "/" + day + "/" + hour).mkdir();
}
} else {
new File(url + "/" + year + "/" + month + "/" + day).mkdir();
}
} else {
new File(url + "/" + year + "/" + month).mkdir();
}
} else {
new File(url + "/" + year).mkdir();
new File(url + "/" + year + "/" + month).mkdir();
new File(url + "/" + year + "/" + month + "/" + day).mkdir();
new File(url + "/" + year + "/" + month + "/" + day + "/" + hour).mkdir();
}
return url + "/" + year + "/" + month + "/" + day + "/" + hour;
}
class MyFileFilter implements java.io.FileFilter {
private boolean isDirectory;
public MyFileFilter(boolean isDir) {
this.isDirectory = isDir;
}
@Override
public boolean accept(File f) {
if (f.isDirectory() == isDirectory) {
return true;
} else
return false;
}
public boolean isExistsDirectory(String path, String dir) {
File file = new File(path);
File[] childFiles = file.listFiles(this);
for (File cFile : childFiles) {
if(cFile.equals(dir)) {
return true;
}
}
return false;
}
}