package com.wwxl.util;
import java.io.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;
/**
* 靜態(tài)頁面引擎技術(shù)
* @author 鄧文偉
*
*/
public class HtmlGenerator{
private Logger logger = Logger.getLogger(HtmlGenerator.class);
HttpClient httpClient = null; //HttpClient實(shí)例
GetMethod getMethod =null; //GetMethod實(shí)例
String page = null;
String webappname = null;
BufferedWriter fw = null;
BufferedReader br = null;
InputStream in = null;
StringBuffer sb = null;
String line = null;
//構(gòu)造方法
public HtmlGenerator(String webappname){
this.webappname = webappname;
}
/** 根據(jù)模版及參數(shù)產(chǎn)生靜態(tài)頁面 */
public boolean createHtmlPage(String url,String htmlFileName){
boolean status = false;
int statusCode = 0;
try{
//創(chuàng)建一個(gè)HttpClient實(shí)例充當(dāng)模擬瀏覽器
httpClient = new HttpClient();
//設(shè)置httpclient讀取內(nèi)容時(shí)使用的字符集
httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");
//創(chuàng)建GET方法的實(shí)例
getMethod = new GetMethod(url);
//使用系統(tǒng)提供的默認(rèn)的恢復(fù)策略凯亮,在發(fā)生異常時(shí)候?qū)⒆詣?dòng)重試3次
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
//設(shè)置Get方法提交參數(shù)時(shí)使用的字符集,以支持中文參數(shù)的正常傳遞
getMethod.addRequestHeader("Content-Type","text/html;charset=UTF-8");
//執(zhí)行Get方法并取得返回狀態(tài)碼弟塞,200表示正常芋绸,其它代碼為異常
statusCode = httpClient.executeMethod(getMethod);
if (statusCode!=200) {
logger.fatal("靜態(tài)頁面引擎在解析"+url+"產(chǎn)生靜態(tài)頁面"+htmlFileName+"時(shí)出錯(cuò)!");
}else{
//讀取解析結(jié)果
sb = new StringBuffer();
in = getMethod.getResponseBodyAsStream();
br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
while((line=br.readLine())!=null){
sb.append(line+"\n");
}
if(br!=null)br.close();
page = sb.toString();
//將頁面中的相對(duì)路徑替換成絕對(duì)路徑年缎,以確保頁面資源正常訪問
page = formatPage(page);
//將解析結(jié)果寫入指定的靜態(tài)HTML文件中因悲,實(shí)現(xiàn)靜態(tài)HTML生成
writeHtml(htmlFileName,page);
status = true;
}
}catch(Exception ex){
logger.fatal("靜態(tài)頁面引擎在解析"+url+"產(chǎn)生靜態(tài)頁面"+htmlFileName+"時(shí)出錯(cuò):"+ex.getMessage());
}finally{
//釋放http連接
getMethod.releaseConnection();
}
return status;
}
//將解析結(jié)果寫入指定的靜態(tài)HTML文件中
private synchronized void writeHtml(String htmlFileName,String page) throws Exception{
fw = new BufferedWriter(new FileWriter(htmlFileName));
OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(htmlFileName),"UTF-8");
fw.write(page);
if(fw!=null)fw.close();
}
//將頁面中的相對(duì)路徑替換成絕對(duì)路徑,以確保頁面資源正常訪問
private String formatPage(String page){
page = page.replaceAll("\\.\\./\\.\\./\\.\\./", webappname+"/");
page = page.replaceAll("\\.\\./\\.\\./", webappname+"/");
page = page.replaceAll("\\.\\./", webappname+"/");
return page;
}
//測(cè)試方法
public static void main(String[] args){
HtmlGenerator h = new HtmlGenerator("webappname");
h.createHtmlPage("http://www.baidu.com","c:/a.html");
System.out.println("靜態(tài)頁面已經(jīng)生成到c:/a.html");
}
}
[html] view plaincopy
添加pom依賴
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>