高性能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+"\n");  
  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.   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM