從網上找到了兩種方法都是大同小異,經過我整理了一下。先將兩種方法貼出來 尊重原作者,向其致敬
1.作者:倉木澤
為了減輕服務器壓力,將原來的文章管理系統由JSP文件的從數據庫中取數據顯示改為由jsp生成靜態html文件后,將相應的文件信息存入數據庫中,然后直接訪問html文件<這個問題我想了好久,我怎么就沒有向這方面想呢,要不是別人提醒,恐怕我還要想一陣子,真的是氣人呢,呵呵>。下面是一個簡單的示例

1.buildhtml.jsp

<%@ page contentType="text/html; charset=gb2312" import="java.util.*,java.io.*"%>
<%
try{
String title="jsp生成靜態html文件";
String content="小樣,還搞不定你?";
String editer="hpsoft";
String filePath = "";
filePath = request.getRealPath("/")+"template.htm";
out.print(filePath);
String templateContent="";
FileInputStream fileinputstream = new FileInputStream(filePath);//讀取模塊文件
int lenght = fileinputstream.available();
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes);
fileinputstream.close();
templateContent = new String(bytes);
out.print(templateContent);
templateContent=templateContent.replaceAll("###title###",title);
templateContent=templateContent.replaceAll("###content###",content);
templateContent=templateContent.replaceAll("###author###",editer);//替換掉模塊中相應的地方
out.print(templateContent);
// 根據時間得文件名
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
fileame = request.getRealPath("/")+fileame;//生成的html文件保存路徑
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件輸出流
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
}
catch(Exception e){
out.print(e.toString());
}

%>

模板文件

2. template.htm

<html>
<head>
<title>###title###</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<LINK href="../css.css" rel=stylesheet type=text/css>
</head>

<body>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="2">
<tr>
? <td align="center">###title###</td>
</tr>
<tr>
? <td align="center">作者:###author### </td>
</tr>
<tr>
? <td>###content###
</td>

</tr>

</table>
</body>
</html>
2. 作者 :xxx(不好意思給忘了)

<%@ page contentType="text/html; charset=gb2312"%>
<%
String[] flag = {"<$title$>","<$date$>","<$author$>","<$content$>"};
String title=request.getParameter("title");
String content=request.getParameter("content");
String editer="admin";
//Session.getAttribute("s_userName");
String filePath = "";
filePath = application.getRealPath("./adminroot/news.template");
String templateContent;
try{
templateContent = ReadTemplates.getTlpContent(filePath);
}catch(Exception e){
throw new Exception("模板信息讀取失敗。請聯系系統管理員。");
}
templateContent = ReplaceAll.replace(templateContent,flag[0],title);
templateContent = ReplaceAll.replace(templateContent,flag[1],GetDate.getStringDate());
templateContent = ReplaceAll.replace(templateContent,flag[2],editer);
templateContent = ReplaceAll.replace(templateContent,flag[3],content);

// 根據時間得文件名與路徑名
Calendar calendar = Calendar.getInstance();
String fileName = String.valueOf(calendar.getTimeInMillis()) +".shtml";
String pathName = application.getRealPath("./news")+"\\"+ calendar.get(Calendar.YEAR) +
"\\"+ (calendar.get(Calendar.MONTH)+1) +"\\"+ calendar.get(Calendar.DAY_OF_MONTH)+"\\";
try{
WriteHtml.save(templateContent,pathName,fileName);
}catch(WriteFileException we){
throw new Exception("操作失敗!");
}%>
import java.io.*;

public class WriteHtml {

? public WriteHtml() {
? }

? public static void save(String s, String s1, String s2)

? {
??? try {
????? a(s1);
????? FileOutputStream fileoutputstream = new FileOutputStream(s1 + s2);
????? byte abyte0[] = s.getBytes();
????? fileoutputstream.write(abyte0);
????? fileoutputstream.close();
??? }
??? catch (IOException e) {
????? System.out.println( " write html error" +e.getMessage());
??? }
? }

? private static void a(String s) {
??? try {
????? File file = new File(s);
????? if (!file.exists())
??????? file.mkdirs();
??? }
??? catch (Exception e) {
????? System.out.println( " mkdirs error!" +e.getMessage());
??? }
? }
}
import java.io.*;
?public class? ReplaceAll
?{
? private static Object a = new Object();
? public ReplaceAll()
? {
? }

? public String replace(String content,String flag,String temp)
? {
?? String str = null;

???????? try
???????? {
????????? //System.out.println(&quot;before-----&quot;+content);
????????? String s1 = content;
???????????? str = s1.replaceAll(flag,temp);
??????????? // System.out.println(&quot;replace after-----&quot;+str);
???????? }
???????? catch(Exception e)
???????? {
????????? System.out.println("replace all error:"+e.getMessage());
???????? }
???????? return str;

???? }
???? private static void a(String s)
???? {
???????? File file = new File(s);
???????? if(!file.exists())
???????????? file.mkdirs();
???? }
?}

import java.io.*;
?import java.util.*;

?public class ReadTemplates
?{

???? private String temp = null;
???? private Object a = new Object();

???? public ReadTemplates()
???? {
???? }

???? public String getTlpContent(String s)

???? {
???????? if(temp == null)
???????????? synchronized(a)
???????????? {
???????????????? if(temp == null)
???????????????????? try
???????????????????? {
???????????????????????? System.out.println("----------------------------");
???????????????????????? temp = a(s);
???????????????????????? //System.out.println(&quot;test the temp&quot;+temp);
???????????????????? }
???????????????????? catch(Exception e)
???????????????????? {
??????????????????????? System.out.println("get tlpconente error"+e.getMessage());
???????????????????? }
???????????? }
???????? return temp;
???? }

???? private synchronized String a(String s)

???? {
???????? String s1 = null;
???????? try
???????? {
???????????? FileInputStream fileinputstream = new FileInputStream(s);
???????????? int i = fileinputstream.available();
???????????? byte abyte0[] = new byte[i];
???????????? fileinputstream.read(abyte0);
???????????? fileinputstream.close();
???????????? s1 = new String(abyte0);
???????? }
???????? catch(IOException e)
???????? {
????????? System.out.println("ioexception error"+e.getMessage());
???????? }
???????? return s1;
???? }

?}
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<LINK href="../css.css" rel=stylesheet type=text/css>
</head>

<body>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="2">
<tr>
<td align="center"><$title$></td>
</tr>
<tr>
<td align="center">作者:<$author$>? 發布時間:<font color=#ff0000><$date$></font></td>
</tr>
<tr>
<td><$content$>
</td>

</tr>

</table>
</body>
</html>
下面是我整理的方法:
1.我使用了已有大部分程序,但借用了pathfile字段,將得到的路徑分類的保存到系統路徑下。
我寫了一個bean已實現這個功能:
package com.binghe.publish.database;

import java.io.*;

public class JspToHtml {
? private String temp = "";
? private Object a = new Object();
? public JspToHtml() {
? }

? public String readTemple(String filepath) {

??? if (temp == null) {
????? synchronized (a) {
??????? System.out.println("---------------");
??????? try {
????????? FileInputStream fileinputstream = new FileInputStream(filepath);
????????? int i = fileinputstream.available();
????????? byte byte0[] = new byte[i];
????????? fileinputstream.read(byte0);
????????? fileinputstream.close();
????????? temp = new String(byte0);
??????? }
??????? catch (FileNotFoundException ex) {
????????? System.out.println("FileNotFoundException error" + ex.getMessage());
??????? }
??????? catch (IOException ex1) {
????????? System.out.println("IOException error" + ex1.getMessage());
??????? }
????? }

??? }
??? return temp;
? }

? public String replaceAll(String content, String flag, String s) {
??? String str = null;
??? String s1 = content;
??? try {
????? str = s1.replaceAll(flag, s);
??? }
??? catch (Exception ex2) {
????? System.out.println("Exception error" + ex2.getMessage());
??? }
??? return str;
? }

? public static void writeHtml(String content, String pathname, String filename) {
??? File file = new File(pathname);
??? if (!file.exists()) {
????? file.mkdir();
??? }
??? try {
????? FileOutputStream fileoutputstream = new FileOutputStream(pathname +
????????? filename);
????? byte byte0[] = content.getBytes();

????? fileoutputstream.close();

??? }
??? catch (FileNotFoundException ex) {
????? System.out.println("FileNotFoundException error" + ex.getMessage());
??? }
??? catch (IOException ex1) {
????? System.out.println("IOException error" + ex1.getMessage());
??? }

? }

}