-
高性能Java Web 页面静态化技术(原创)
- 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+"
");
- }
- 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");
-
- }
-
- }
-
相关阅读:
cogs 1682. [HAOI2014]贴海报 WW
cogs 2039. 树的统计
cogs luogu [NOIP2011] 选择客栈
cogs luogu 1804. [NOIP2014]联合权值 WD
cogs luogu [NOIP2014]生活大爆炸版石头剪刀布
leetcode[119]Pascal's Triangle II
leetcode[120]Triangle
leetcode[121]Best Time to Buy and Sell Stock
leetcode[122]Best Time to Buy and Sell Stock II
leetcode[123]Best Time to Buy and Sell Stock III
-
原文地址:https://www.cnblogs.com/tuojunjie/p/6221809.html
Copyright © 2020-2023
润新知