HttpClient生成静态HTML页面ITeye - 娱乐之横扫全球

HttpClient生成静态HTML页面ITeye

2019年03月01日09时23分02秒 | 作者: 鸿博 | 标签: 页面,静态,生成 | 浏览: 2189

一般生成HTML页时,都会用freemarker,可是每次都要编写模板文件,有时依据项目需求运用它略显费事。这儿运用httpclient的get办法,去读某个动态的URL,然后把读出的内容再保存成HTML的,下面比如介绍一下:

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 java.io.*;
 * Created with GQ.
 * User: Administrator
 * Date: 13-11-1
 * Time: 下午1:52
public class HtmlGenerator {
 HttpClient httpClient = null; //HttpClient实例
 GetMethod getMethod = null; //GetMethod实例
 BufferedWriter fw = null;
 String page = null;
 String webappname = null;
 BufferedReader br = null;
 InputStream in = null;
 StringBuffer sb = null;
 String line = null;
 //结构办法
 public HtmlGenerator(String webappname) {
 this.webappname = webappname;
 * 依据模版及参数发作静态页面
 public boolean createHtmlPage(String url, String htmlFileName) {
 boolean status = false;
 int statusCode = 0;
 try {
 //创立一个HttpClient实例充任模仿浏览器
 httpClient = new HttpClient();
 //设置httpclient读取内容时运用的字符集
 httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
 //创立GET办法的实例
 getMethod = new GetMethod(url);
 //运用体系供给的默许的康复战略,在发作反常时分将主动重试3次
 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
 //设置Get办法提交参数时运用的字符集,以支撑中文参数的正常传递
 getMethod.addRequestHeader("Content-Type", "text/html;charset=UTF-8");
 //履行Get办法并获得回来状况码,200表明正常,其它代码为反常
 statusCode = httpClient.executeMethod(getMethod);
 if (statusCode != 200) {
 System.out.println("静态页面引擎在解析" + url + "发作静态页面" + htmlFileName + "时犯错!");
 } else {
 //读取解析成果
 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();
 //将页面中的相对路径替换成绝对路径,以保证页面资源正常拜访
 page = formatPage(page);
 //将解析成果写入指定的静态HTML文件中,完成静态HTML生成
 writeHtml(htmlFileName, page);
 status = true;
 } catch (Exception ex) {
 System.out.println("静态页面引擎在解析" + url + "发作静态页面" + htmlFileName + "时犯错:" + ex.getMessage());
 } finally {
 //开释http衔接
 getMethod.releaseConnection();
 return status;
 //将解析成果写入指定的静态HTML文件中
 private synchronized void writeHtml(String htmlFileName, String content) throws Exception {
 // 指定生成文件的编码办法为UTF-8,防止中文乱码
 PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFileName),"UTF-8")));
 // fw = new BufferedWriter(new FileWriter(htmlFileName)); 此办法生成的文件编码格局为:ANSI,会导致中文乱码(另存为文件时可检查详细编码格局)
 fw = new BufferedWriter(out);
 fw.write(page);
 if (fw != null) fw.close();
 //将页面中的相对路径替换成绝对路径,以保证页面资源正常拜访
 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("");
 h.createHtmlPage("http://localhost:8098/app/common/reg.jsp", "c:/reg.html");
}

 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表娱乐之横扫全球立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章