产生静态的hmtl文件
从publish.jsp开始执行,html.template是模板文件,publish.jsp提交给
publishOK.jsp处理,生成静态的html文件!
//html.template文件
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>学生商行『新闻系统』--<$title$></title>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="778" align="center">
<tr>
<td>标题:</td>
<td><$title$></td>
</tr>
<tr>
<td>内容:</td>
<td><$content$></td>
</tr>
</table>
</body>
</html>
//publish.jsp文件
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>首页</title>
</head>
<body>
<form name="form1" method="post" action="publishOK.jsp">
<table>
<tr>
<td>标 题:</td>
<td><input type="text" name="title" size="60"></td>
</tr>
<tr>
<td>内 容:</td>
<td><textarea name="content" rows="10" cols="100"> </textarea> </td>
</tr>
<tr>
<td><input type="submit" value="确定"/></td>
<td><input type="reset" value="重置"/></td>
</tr>
</table>
</form>
</body>
</html>
//publishOK.jsp文件
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ page import="com.ok.*" %>
<%@ page import="java.util.Calendar" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>处理转化成hmtl</title>
</head>
<body>
<%
String title = request.getParameter("title");
String content = request.getParameter("content");
String filePath = "";
filePath = application.getRealPath("/html.template"); //这个也可以获得路径,或通上下文也可以!
String[] flag = {"<$title$>","<$content$>"}; //html模板中要替代的字符串
String templateContent;
try
{
templateContent = ReadTemplates.getTlpContent(filePath);
}
catch(ReadTemplateException e){
throw new Exception(e.getMessage());
}
title = new String(title.getBytes("iso-8859-1"),"gbk"); //将其转化成GBK编码,不然输入中文时,不能正确显示!
content = new String(content.getBytes("iso-8859-1"),"gbk");
//要正确显示字符串的回车(使显示的内容和原来的一样),要将获得的内容的回车/n换成<br>.
title=title.replaceAll(" "," ");
content=content.replaceAll("/n","<br/>"); //换行转化
content=content.replaceAll(" "," "); //空格转化
//html模板中的<$title$>,<$content$>替换为实际内容
templateContent = ReplaceAll.replace(templateContent,flag[0],title);
templateContent = ReplaceAll.replace(templateContent,flag[1],content);
// 根据时间得文件名与路径名
Calendar calendar = Calendar.getInstance();
String fileName = String.valueOf(calendar.getTimeInMillis()) +".html";
// 如果是linux系统中,要把下面的 "//" 改为 "/"
String pathName = application.getRealPath("../news")+"/"+ calendar.get(Calendar.YEAR) +
"/"+ (calendar.get(Calendar.MONTH)+1) +"/"+ calendar.get(Calendar.DAY_OF_MONTH)+"/";
System.out.println("pathName="+pathName);//(正确)这是打印出的路径,如GenerateHtml/../news/2007/4/26/这是正确的,因为它创建文件夹时,用"/"或"//"都可以!
//只是打印出的信息/和/不一样转罢了.
System.out.println("fileName="+fileName);
/**
* 写文件
*/
try {
WriteHtml.save(templateContent,pathName,fileName);
}
catch(WriteFileException e){
throw new Exception("新闻发布失败。可能是目录不具备IO操作权限。请与管理员联系。");
}
//接下去,可以把生成的html路径写入数据库中就可以了,这样就生成了静静的html文件了。
//testForm.setNewsFilePath(calendar.get(Calendar.YEAR)+"/"+(calendar.get(Calendar.MONTH)+1)+"/"+ calendar.get(Calendar.DAY_OF_MONTH)+"/"+fileName);
out.println("测试成功!!");
out.println("生成静态html成功!!");
%>
</body>
</html>
在JSP文件中,要用到的类ReadTemplates.java、ReadTemplateException.java
ReplaceAll.java、WriteFileException.java、WriteHtml.java。
//ReadTemplates.java文件
package com.ok;
import java.io.FileInputStream;
import java.io.IOException;
public class ReadTemplates {
private static String tlpContent = null; //该字符串变量用来保存读取的文件内容
public ReadTemplates()
{
}
public static String getTlpContent(String s) //返回读取的文件内容,其中s是文件的存放路径
throws ReadTemplateException
{
if(tlpContent == null) // 如果字符串变量tlpContent为空
try
{
tlpContent = readTemplateContent(s); //调用readTemplateContent方法!
if(tlpContent == null)
throw new ReadTemplateException("tlpContent is null !");
}
catch(ReadTemplateException readtemplateexception)
{
throw new ReadTemplateException("read template error !");
}
return tlpContent;
}
private static synchronized String readTemplateContent(String s)
throws ReadTemplateException //读取的文件的方法,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,"gbk");
}
catch(IOException ioexception)
{
throw new ReadTemplateException("io error !");
}
System.out.println("ReadTemplates的readTemplateContent方法读取的字符串s1是否是(gbk)="+s1);
return s1;
}
}
//ReadTemplateException.java文件
package com.ok;
public class ReadTemplateException extends Exception
{
/**
*
*/
private static final long serialVersionUID = 1L;
public ReadTemplateException()
{
}
public ReadTemplateException(String s)
{
super(s);
}
}
//ReplaceAll.java文件
package com.ok;
public class ReplaceAll {
public ReplaceAll()
{
}
public static String replace(String s, String s1, String s2)
{ //s为模板文件中的读取内容;s1为插入位置(如<$title$>);s2为插入参数值(即<$title$>替换的内容)
if(s == null)
return null;
StringBuffer stringbuffer = new StringBuffer();
int i = s.length(); //模板文件中的字节长度
int j = s1.length(); //插入位置的标签名长度
int k;
int l;
for(k = 0; (l = s.indexOf(s1, k)) >= 0; k = l + j)
{
stringbuffer.append(s.substring(k, l));
stringbuffer.append(s2); //插入参数值(如<$title$>替换的内容)
}
if(k < i)
stringbuffer.append(s.substring(k));
System.out.println("ReplaceAll中方法replace的返回内容:(是否是GBK)="+stringbuffer.toString());
return stringbuffer.toString();
}
}
//WriteFileException文件
package com.ok;
public class WriteFileException extends Exception{
/**
*
*/
private static final long serialVersionUID = 1L;
public WriteFileException()
{
}
public WriteFileException(String s)
{
super(s);
}
}
//WriteHtml.java文件
package com.ok;
import java.io.*;
public class WriteHtml {
public WriteHtml()
{
}
public static void save(String s, String s1, String s2)
throws WriteFileException //s为要写入的内容,s1为要写入的文件目录,s2为文件名
{
try
{
getFile(s1); //要写入文件目录s1是否存在,不存在则创建!
FileOutputStream fileoutputstream = new FileOutputStream(s1 + s2); //初始化写入操作流
byte abyte0[] = s.getBytes();
fileoutputstream.write(abyte0); //将内容写入到文件中!
fileoutputstream.close();
}
catch(IOException ioexception)
{
throw new WriteFileException("write file error !");
}
}
private static void getFile(String s)
throws WriteFileException
{
try
{
File file = new File(s);
if(!file.exists()) //查看目录是否存在,如果不存在,则创建目录
file.mkdirs();
}
catch(Exception exception)
{
throw new WriteFileException("wirte File error !");
}
}
}