高效能Java Web 頁面靜態化技術(原創)__Java

來源:互聯網
上載者:User
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;/** * 靜態頁面引擎技術(突亂了亂碼問題UTF-8) * @author 吳彥文 * */public class HtmlGenerator extends BaseLog {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) {logger.fatal("靜態頁面引擎在解析"+url+"產生靜態頁面"+htmlFileName+"時出錯!");}else{//讀取解析結果sb = new StringBuffer();in = getMethod.getResponseBodyAsStream();//br = new BufferedReader(new InputStreamReader(in));//此方法預設會亂碼,經過長時期的摸索,下面的方法才可以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){logger.fatal("靜態頁面引擎在解析"+url+"產生靜態頁面"+htmlFileName+"時出錯:"+ex.getMessage());        }finally{        //釋放http串連        getMethod.releaseConnection();        }return status;}//將解析結果寫入指定的靜態HTML檔案中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");}}








聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.