JSP生成静态页面HTML的一种方法(一)
package com.easydone.cn.tools.utils;
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 + "秒");
}
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://www.wodt.cn/index.jsp";
writeHtml("c:/demo.htm", getHtmlCode(url), "NO");
}
}
JSP生成静态页面HTML的一种方法(二)
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
public class toHtml extends HttpServlet
{
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String url="";
String name="";
ServletContext sc = getServletContext();
String file_name=request.getParameter("file_name");//你要访问的jsp文件,如index.jsp
//则你访问这个servlet时加参数.?img align=absmiddle src=/images/emot/url.gif>http://localhost/toHtml?file_name=index
url = "/"+file_name+".jsp";//这是你要生成HTML的jsp文件,如
/http://localhost/index.jsp的执行结果.
name="/home/resin/resin-2.1.6/doc/"+file_name+".htm";//这是生成的html文件名,如index.htm.
RequestDispatcher rd = sc.getRequestDispatcher(url);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
final ServletOutputStream stream = new ServletOutputStream()
{
public void write(byte[] data, int offset, int length)
{
os.write(data, offset, length);
}
public void write(int b) throws IOException
{
os.write(b);
}
};
final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
HttpServletResponse rep = new HttpServletResponseWrapper(response)
{
public ServletOutputStream getOutputStream()
{
return stream;
}
public PrintWriter getWriter()
{
return pw;
}
};
rd.include(request, rep);
pw.flush();
FileOutputStream fos = new FileOutputStream(name); //把jsp输出的内容写到xxx.htm
os.writeTo(fos);
fos.close();
PrintWriter out=response.getWriter();
out.print("<p align=center><font size=3 color=red>首页已经成功生成!Andrew</font></p>");
}
}
更多的JSP生成静态页面HTML的方法请到论坛查看: http://BBS.TC711.COM
【 双击滚屏 】 【 评论 】 【 收藏 】 【 打印 】 【 关闭 】
来源:
互联网
日期:2008-1-30