页面静态化

在做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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值