- package com.yancms.util;
-
- import java.io.*;
- import org.apache.commons.httpclient.*;
- import org.apache.commons.httpclient.methods.*;
- import org.apache.commons.httpclient.params.HttpMethodParams;
-
- public class HtmlGenerator extends BaseLog {
- HttpClient httpClient = null;
- GetMethod getMethod =null;
- 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 = new HttpClient();
-
- httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");
-
- getMethod = new GetMethod(url);
-
- getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
-
- getMethod.addRequestHeader("Content-Type","text/html;charset=UTF-8");
-
- statusCode = httpClient.executeMethod(getMethod);
- if (statusCode!=200) {
- logger.fatal("静态页面引擎在解析"+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);
-
- writeHtml(htmlFileName,page);
- status = true;
- }
- }catch(Exception ex){
- logger.fatal("静态页面引擎在解析"+url+"产生静态页面"+htmlFileName+"时出错:"+ex.getMessage());
- }finally{
-
- getMethod.releaseConnection();
- }
- return status;
- }
-
-
- 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();
- }
-
-
- 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("webappname");
- h.createHtmlPage("http://localhost:8080/yanCms/three/three?parent_id=10&id=103&type=10","c:/a.html");
- System.out.println("静态页面已经生成到c:/a.html");
-
- }
-
- }
posted @
2016-12-26 11:45
奥特快啦
阅读(
2550)
评论()
编辑
收藏
举报