经过本人对当今技术的观察发现,对动态新闻页面进行静态化是一个非常好的技术。
技术本身可以对现有的访问量大的网页进行静态化处理,当网页被静态化后,能保存为网页的部分就被保存为了静态网页,这样降低了与数据库的交互,节省了查询时间,能够让用户获得更好的体验
静态化类
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
*/
* 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;
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;
* @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 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) {
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();
}
}
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");
}
}
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目录要事先存在,否则会报空指针异常。
所以想要更好的使用这个类,就要求能用一个能够创建目录的方法或类,这样就能够更好的将类整合到实际的项目中去,有哪位仁兄弄好了这块,可以给我发邮件,好好讨论下,呵呵