文章发布网页静态化

本文探讨了网页静态化技术,通过将动态新闻页面转换为静态网页,减少数据库交互,提高用户访问速度。提供了一个Java实现的MakeHtml类,该类能够获取网页HTML代码并将其写入文件。需要注意,调用writeHtml方法时,目标目录需预先存在,否则可能抛出空指针异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

经过本人对当今技术的观察发现,对动态新闻页面进行静态化是一个非常好的技术。
技术本身可以对现有的访问量大的网页进行静态化处理,当网页被静态化后,能保存为网页的部分就被保存为了静态网页,这样降低了与数据库的交互,节省了查询时间,能够让用户获得更好的体验
 
静态化类
package com.easydone.cn.tools.utils;
/*
 * Created on 2006-3-4
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
/**
 * @author Administrator To change the template for this generated type comment
 *         go to Window>Preferences>Java>Code Generation>Code and
 *         Comments
 */
public class MakeHtml {
 private static long star = 0;
 private static long end = 0;
 private static long ttime = 0;
 // 返回html代码
 public static String getHtmlCode(String httpUrl) {
  Date before = new Date();
  star = before.getTime();
  String htmlCode = "";
  try {
   InputStream in;
   URL url = new java.net.URL(httpUrl);
   HttpURLConnection connection = (HttpURLConnection) url
     .openConnection();
   connection = (HttpURLConnection) url.openConnection();
   connection.setRequestProperty("User-Agent", "Mozilla/4.0");
   connection.connect();
   in = connection.getInputStream();
   java.io.BufferedReader breader = new BufferedReader(
     new InputStreamReader(in, "GBK"));
   String currentLine;
   while ((currentLine = breader.readLine()) != null) {
    htmlCode += currentLine;
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   Date after = new Date();
   end = after.getTime();
   ttime = end - star;
   System.out.println("执行时间:" + ttime + "M秒");
  }
  return htmlCode;
 }
 // 存储文件
 public static synchronized void writeHtml(String filePath, String info,
   String flag) {
  PrintWriter pw = null;
  try {
   File writeFile = new File(filePath);
   boolean isExit = writeFile.exists();
   if (isExit != true) {
    writeFile.createNewFile();
   } else {
    if (!flag.equals("NO")) {
     writeFile.delete();
     writeFile.createNewFile();
    }
   }
   pw = new PrintWriter(new FileOutputStream(filePath, true));
   pw.println(info);
   pw.close();
  } catch (Exception ex) {
   System.out.println(ex.getMessage());
  } finally {
   pw.close();
  }
 }
 public static void main(String[] args) {
  String url = " http://mag.book.qq.com/a/20090506/000003_18.htm";
  writeHtml("c:/box/2009050600qq-home.htm", getHtmlCode(url), "NO"); 
 }
}
 
其中writeHtml("c:/box/2009050600qq-home.htm", getHtmlCode(url), "NO"); 的c:/box目录要事先存在,否则会报空指针异常。
 
所以想要更好的使用这个类,就要求能用一个能够创建目录的方法或类,这样就能够更好的将类整合到实际的项目中去,有哪位仁兄弄好了这块,可以给我发邮件,好好讨论下,呵呵

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值