1,根據(jù)模板生成靜態(tài)頁面
導(dǎo)入依賴:
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
package com.wjb.util;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;
import java.io.*;
/**
* Created by wjb on 2017/5/9.
*/
public class HtmlGenerator {
private final static Logger logger = Logger.getLogger(HtmlGenerator.class);
HttpClient httpClient = null; //HttpClient實(shí)例
GetMethod getMethod = null; //GetMethod實(shí)例
BufferedWriter fw = null;
String page = null;
String webappname = 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));//此方法默認(rèn)會(huì)亂碼,經(jīng)過長時(shí)期的摸索,下面的方法才可以
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 content) 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;
}
//測試方法
public static void main(String[] args) {
HtmlGenerator h = new HtmlGenerator("wjbHtml");
h.createHtmlPage("http://www.itdecent.cn/u/1b21f2e84e3e", "src/main/resources/templates/a.html");
h.createHtmlPage("http://localhost:8080/wjb", "src/main/resources/templates/c.html");
System.out.println("靜態(tài)頁面已經(jīng)生成");
}
}
2,拼接標(biāo)簽生成頁面
package com.wjb.util;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
/**
* Created by wjb on 2017/5/9.
*/
public class DynamicHtml {
public static void main(String[] args) {
StringBuilder stringHtml = new StringBuilder();
PrintStream ps = null;
try {
ps = new PrintStream(new FileOutputStream("src/main/resources/templates/b.html"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
stringHtml.append("<html><head>");
stringHtml.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=GBK\">");
stringHtml.append("<title>測試頁面</title>");
stringHtml.append("</head>");
stringHtml.append("<body>");
stringHtml.append("<div>hello</div>");
stringHtml.append("</body></html>");
ps.println(stringHtml.toString());
}
}