页面静态化

在网站应用中,为了提高页面的访问速度,经常需要将动态页面静态化以提高页面的访问速度,因为动态页面一般要从数据库检索信息,频繁访问动态页面会大大提高数据库的负载,并且访问速度也比静态页面慢.本文通过在程序里建立一个http请求,将返回的输出流存储为html文件的方式来生成静态页面.CMS类的应用中,信息发布完可直接调用这段代码,给定一个动态连接地址如http://localhost:8080/cms/info.jsp?infoid=001,生成一个静态页面。

 

/**
     * 将信息转化为静态html
     *
     * @param sSourceUrl
     *            动态信息访问URL
     * @param sDestDir
     *            存储为静态文件的目录
     * @param sHtmlFile
     *            生成的静态文件名,可以按信息的唯一ID+.html命名
     * @throws IOException
     */
    public static void convert2Html(String sSourceUrl, String sDestDir,
           String sHtmlFile) throws IOException {
       int HttpResult;
       URL url = new URL(sSourceUrl);
       URLConnection urlconn = url.openConnection();
       urlconn.connect();
       HttpURLConnection httpconn = (HttpURLConnection) urlconn;
       HttpResult = httpconn.getResponseCode();
       if (HttpResult != HttpURLConnection.HTTP_OK) {
 
       } else {
 
           InputStreamReader isr = new InputStreamReader(httpconn
                  .getInputStream());
           BufferedReader in = new BufferedReader(isr);
 
           String inputLine;
           if (!sDestDir.endsWith("/"))
              sDestDir += "/";
           FileOutputStream fout = new FileOutputStream(sDestDir + sHtmlFile);
           while ((inputLine = in.readLine()) != null) {
              fout.write(inputLine.getBytes());
 
           }
           in.close();
           fout.close();
 
       }
 
    }
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值