日期:2014-05-16  浏览次数:20357 次

jsp静态化之简单介绍
package com.zwd.text;

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;

public class Test {
	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();
		StringBuffer htmlCode = new StringBuffer(); //这里我做了改动,原文是用String,这样内存消耗会太大,原因我就不说了
		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");//这句很重要,预防服务器拦截我们java程序的请求,如果少了就会被弹403等错误。
			connection.connect();
                        //连接部分完成
                        //=======================           华丽分界线            =================
                        //io读写开始  
                        in = connection.getInputStream();   
			java.io.BufferedReader breader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
			String currentLine;  
			while ((currentLine = breader.readLine()) != null) {  //以行的方式读取
				htmlCode.append(currentLine);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
                        //计算时耗
                        Date after = new Date();
 			end = after.getTime();
			 ttime = end - star;
			 System.out.println(" 执行时间 :" + ttime + " 毫秒 ");
 		}
		 return htmlCode.toString();
	 }

	 // 存储文件 这里就做详细解释了,就是把上文获取的内容保存到指定文件而已
	 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://localhost:8080/online/index.do"; //目标网站
		writeHtml("d:/demo.html", getHtmlCode(url), "NO");     //目标网站静态化
	}
}


这些代码是我从网上搜来的,其实网上材料很多,本人觉得这个写的是最好的,所以挑他来说下,其实静态化也不难,只是用最常见的方法,对把目标文件下载,或者应该说读取出来而已,

所以关键就在于io流的应用,从代码上看,其实也很简单,我也为大家一一注释下,希望对新手有点帮助,当然后事还没完成,列子只是给大家把怎么把一个页面静态化而已,如果大家想偷懒想容器自动静态化的话,只要配合写一个定时器就行了(定时器我别的文章页介绍了,这里就不多少)

核心部分也就是这样,如果等本人有时间,我再做一个实例,上传上来,提供大家参考吧.时间嘛,待定,呵呵