頁面靜態化

在做web開發時,經常會遇到需要將一些動態頁面靜態化爲html,提高訪問的效率,減輕服務器壓力。如百度音樂,中的歌曲,歌手,專輯,以及榜單全是靜態化的頁面。

下面兩種方法都是通過文件流來實現的,注意轉碼問題。我在些就不詳細解釋,只接上源碼

 //把流輸出
private static void getBuffer(String tofilename,String url) throws IOException{
InputStream l_urlStream = getHtmlSource(url);
BufferedInputStream  bufferedInputStream=new BufferedInputStream(l_urlStream);
Reader isr=new InputStreamReader(bufferedInputStream,"utf-8");
PrintWriter out = null;
BufferedReader br = null;
//BufferedOutputStream bufferedOutputStream = null;
       try{
        br=new BufferedReader(isr);
//            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(tofilename));
           String content;
           out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tofilename), "utf-8"));


           while((content=br.readLine()) !=null){
            out.println(content);
           }
        //bufferedOutputStream.flush();
       }catch(Exception exception){
       }finally{
        try {
if(l_urlStream!=null){
l_urlStream.close();
}
if(bufferedInputStream!=null){
bufferedInputStream.close();
}
if(br!=null){
br.close();
}
if(out!=null){
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
       }
       
}
 
//根據路徑獲得文件流
private static InputStream getHtmlSource(String getURL){
  java.io.InputStream l_urlStream=null;  
try{
java.net.URL l_url = new java.net.URL(getURL);  


java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection();
l_connection.setRequestProperty("User-Agent","Mozilla/6.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
int state=l_connection.getResponseCode();
if(state==200){
l_connection.connect();  
l_urlStream = l_connection.getInputStream();  
}
}catch(Exception ex){
ex.printStackTrace();
}
return l_urlStream;
  }




/**
* 生成(分類主頁面)靜態頁面
* 方法名:   goIndexStaticMain   
* 方法描述: TODO
* 傳入參數:  
* 返回值類型: String
* 異常對象:
* @throws Exception 
*/
public String goCateContentMain() throws Exception{
    String webUrl = SystemConfig.getInstance().getCateContentMainUrl();
String tofilename =  this.getRequest().getRealPath("/")+"page\\pageCategory\\categoryContent\\cateContentMain.html";


File file = new File(tofilename);
if(file.exists()){
file.delete();
}
file.createNewFile();
getBuffer(tofilename,webUrl);


   this.outputDateinfo("生成成功!");
   return NONE;
 
}


public static void createFile1(String path,String value) throws IOException{
File f = new File(path);
if(f.exists()){
f.delete();
}
FileOutputStream os = new FileOutputStream(f);
OutputStreamWriter out = new OutputStreamWriter(os,"utf-8");
out.write(value);
out.close();
os.close();
}

public static  String getHtmlSource1(String getURL){
  String html="";
try{
String sCurrentLine;  


StringBuffer sTotalString = new StringBuffer("");  


sCurrentLine="";  


java.io.InputStream l_urlStream;  
java.net.URL l_url = new java.net.URL(getURL);  


java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection();
int state=l_connection.getResponseCode();
if(state==200){
l_connection.connect();  


l_urlStream = l_connection.getInputStream();  


java.io.BufferedReader l_reader = new java.io.BufferedReader(new java.io.InputStreamReader(l_urlStream,"utf-8"));  


//得到返回的信息
while ((sCurrentLine = l_reader.readLine()) != null) {  
 sTotalString.append(sCurrentLine); 
 sTotalString.append("\n");
}  
l_reader.close();
html = sTotalString.toString();

}else{
html=null;
}
l_connection.disconnect();
}catch(Exception ex){
ex.printStackTrace();
}
return html;
}



發佈了18 篇原創文章 · 獲贊 3 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章