zoukankan      html  css  js  c++  java
  • 高性能Java Web 页面静态化技术(原创)

    [java] view plain copy 在CODE上查看代码片派生到我的代码片
    1. package com.yancms.util;  
    2.   
    3. import java.io.*;  
    4. import org.apache.commons.httpclient.*;  
    5. import org.apache.commons.httpclient.methods.*;  
    6. import org.apache.commons.httpclient.params.HttpMethodParams;  
    7.   
    8. /** 
    9.  * 静态页面引擎技术(突乱了乱码问题UTF-8) 
    10.  * @author 吴彦文 
    11.  * 
    12.  */  
    13. public class HtmlGenerator extends BaseLog {  
    14.     HttpClient httpClient = null; //HttpClient实例  
    15.     GetMethod getMethod =null; //GetMethod实例  
    16.     BufferedWriter fw = null;  
    17.     String page = null;  
    18.     String webappname = null;  
    19.     BufferedReader br = null;  
    20.     InputStream in = null;  
    21.     StringBuffer sb = null;  
    22.     String line = null;   
    23.     //构造方法  
    24.     public HtmlGenerator(String webappname){  
    25.         this.webappname = webappname;  
    26.           
    27.     }  
    28.       
    29.     /** 根据模版及参数产生静态页面 */  
    30.     public boolean createHtmlPage(String url,String htmlFileName){  
    31.         boolean status = false;   
    32.         int statusCode = 0;               
    33.         try{  
    34.             //创建一个HttpClient实例充当模拟浏览器  
    35.             httpClient = new HttpClient();  
    36.             //设置httpclient读取内容时使用的字符集  
    37.             httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");           
    38.             //创建GET方法的实例  
    39.             getMethod = new GetMethod(url);  
    40.             //使用系统提供的默认的恢复策略,在发生异常时候将自动重试3次  
    41.             getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());  
    42.             //设置Get方法提交参数时使用的字符集,以支持中文参数的正常传递  
    43.             getMethod.addRequestHeader("Content-Type","text/html;charset=UTF-8");  
    44.             //执行Get方法并取得返回状态码,200表示正常,其它代码为异常  
    45.             statusCode = httpClient.executeMethod(getMethod);             
    46.             if (statusCode!=200) {  
    47.                 logger.fatal("静态页面引擎在解析"+url+"产生静态页面"+htmlFileName+"时出错!");  
    48.             }else{  
    49.                 //读取解析结果  
    50.                 sb = new StringBuffer();  
    51.                 in = getMethod.getResponseBodyAsStream();  
    52.                 //br = new BufferedReader(new InputStreamReader(in));//此方法默认会乱码,经过长时期的摸索,下面的方法才可以  
    53.                 br = new BufferedReader(new InputStreamReader(in,"UTF-8"));  
    54.                 while((line=br.readLine())!=null){  
    55.                     sb.append(line+" ");  
    56.                 }  
    57.                 if(br!=null)br.close();  
    58.                 page = sb.toString();  
    59.                 //将页面中的相对路径替换成绝对路径,以确保页面资源正常访问  
    60.                 page = formatPage(page);  
    61.                 //将解析结果写入指定的静态HTML文件中,实现静态HTML生成  
    62.                 writeHtml(htmlFileName,page);  
    63.                 status = true;  
    64.             }             
    65.         }catch(Exception ex){  
    66.             logger.fatal("静态页面引擎在解析"+url+"产生静态页面"+htmlFileName+"时出错:"+ex.getMessage());           
    67.         }finally{  
    68.             //释放http连接  
    69.             getMethod.releaseConnection();  
    70.         }  
    71.         return status;  
    72.     }  
    73.       
    74.     //将解析结果写入指定的静态HTML文件中  
    75.     private synchronized void writeHtml(String htmlFileName,String content) throws Exception{  
    76.         fw = new BufferedWriter(new FileWriter(htmlFileName));  
    77.         OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(htmlFileName),"UTF-8");  
    78.         fw.write(page);   
    79.         if(fw!=null)fw.close();       
    80.     }  
    81.       
    82.     //将页面中的相对路径替换成绝对路径,以确保页面资源正常访问  
    83.     private String formatPage(String page){       
    84.         page = page.replaceAll("\.\./\.\./\.\./", webappname+"/");  
    85.         page = page.replaceAll("\.\./\.\./", webappname+"/");  
    86.         page = page.replaceAll("\.\./", webappname+"/");            
    87.         return page;  
    88.     }  
    89.       
    90.     //测试方法  
    91.     public static void main(String[] args){  
    92.         HtmlGenerator h = new HtmlGenerator("webappname");  
    93.         h.createHtmlPage("http://localhost:8080/yanCms/three/three?parent_id=10&id=103&type=10","c:/a.html");  
    94.         System.out.println("静态页面已经生成到c:/a.html");  
    95.           
    96.     }  
    97.   
  • 相关阅读:
    Future接口和Callable接口以及FeatureTask详解
    puppet的使用:ERB模板介绍
    puppet的使用:依赖关系整理
    数字证书常见格式整理
    c3p0配置文件
    dockerfile简述
    Grape简介
    keytool和openssl生成的证书转换
    Grape教程-params
    耿丹CS16-2班助教总结
  • 原文地址:https://www.cnblogs.com/tuojunjie/p/6221809.html
Copyright © 2011-2022 走看看