日期:2011-01-26 02:54:00 来源:本站整理
java 生成静态html的一段代码[Java编程]
本文“java 生成静态html的一段代码[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
在某些利用中,将本该由动态页面每次获得客户端恳求时去调用数据的历程转换为在增添数据时即生成为静态页面,对服务器的压力,数据库检索的压力,以及搜索引擎收录,包含避免SQL注入都是由极大的好处的.常见的做法有很多种,包含web服务器启用rewrite,io操作生成文件等等,我恰好有一段从前做j2ee开辟时保存的一段io操作的方法生成html.
原理都很简单,主如果对模板的解析.so,我们先预备一个html模板
mb.html,做个文件此中的###title###之类的标签用于程序举行查询替换.
<html>
<head>
<title>###title###</title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
</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>
<tr>
<td>###html###</td>
</tr>
</table>
</body>
</html>
<head>
<title>###title###</title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
</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>
<tr>
<td>###html###</td>
</tr>
</table>
</body>
</html>
下面是具体的代码:JspToHtml.java
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.net.URL;
import java.net.URLConnection;
import java.io.*;
/**
* Filename: JspToHtml.java <br>
* Ttitle: jsp转换成html<br>
* De.ion: 把动态网页转换成静态网页<br>
* Copyright: Copyright (c) 2002-2008 BocSoft,Inc.All Rights Reserved. <br>
* Company: BocSoft<br>
* Author: <a href="mailto:sgicer@163.com">阿汐</a> <br>
* Date: 2006-6-19 <br>
* Time: 16:41:09 <br>
* Version: 2.0.0 <br>
*/
public class JspToHtml {
private static String title ="标题测试";
private static String context ="标题测试";
private static String editer ="标题测试";
/**
* 按照本地模板生成静态页面
* @param JspFile jsp路经
* @param HtmlFile html路经
* @return
*/
public static boolean JspToHtmlFile(String filePath, String HtmlFile) {
String str = "";
long beginDate = (new Date()).getTime();
try {
String tempStr = "";
FileInputStream is = new FileInputStream(filePath);//读取模块文件
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while ((tempStr = br.readLine()) != null)
str = str + tempStr ;
is.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
try {
str = str.replaceAll("###title###",
title);
str = str.replaceAll("###content###",
context);
str = str.replaceAll("###author###",
editer);//替换掉模块中呼应的地方
File f = new File(HtmlFile);
BufferedWriter o = new BufferedWriter(new FileWriter(f));
o.write(str);
o.close();
System.out.println("共用时:" + ((new Date()).getTime() - beginDate) + "ms");
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 按照url生成静态页面
*
* @param u 动态文件路经 如:http://www.163.com/x.jsp
* @param path 文件存放路经如:x:\\abc\bbb.html
* @return
*/
public static boolean JspToHtmlByURL(String u, String path) {
//从utl中读取html存为str
String str = "";
try {
URL url = new URL(u);
URLConnection uc = url.openConnection();
InputStream is = uc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while (br.ready()) {
str += br.readLine() + "\n";
}
is.close();
//写入文件
File f = new File(path);
BufferedWriter o = new BufferedWriter(new FileWriter(f));
o.write(str);
o.close();
str = "";
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 按照url生成静态页面
*
* @param url 动态文件路经 如:http://www.163.com/x.jsp
* @return d
*/
public static StringBuffer getHtmlTextByURL(String url) {
//从utl中读取html存为str
StringBuffer sb = new StringBuffer();
try {
URL u = new URL(url);
URLConnection uc = u.openConnection();
InputStream is = uc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while (br.ready()) {
sb.append(br.readLine() + "\n");
}
is.close();
return sb;
} catch (Exception e) {
e.printStackTrace();
return sb;
}
}
/**
* 测试main 函数
*
* @param arg
*/
public static void main(String[] arg) {
long begin = System.currentTimeMillis();
//循环生成20个html文件
for (int k = 0; k < 20; k++) {
String url = "E:\\workspace\\oa\\golatel\\utils\\Temp\\mb.htm";//模板文件地址
String savepath = "d:/" + k + ".html";//生成文件地址
JspToHtmlFile(url, savepath);
}
System.out.println("用时:" + (System.currentTimeMillis() - begin) + "ms");
}
}
import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.net.URL;
import java.net.URLConnection;
import java.io.*;
/**
* Filename: JspToHtml.java <br>
* Ttitle: jsp转换成html<br>
* De.ion: 把动态网页转换成静态网页<br>
* Copyright: Copyright (c) 2002-2008 BocSoft,Inc.All Rights Reserved. <br>
* Company: BocSoft<br>
* Author: <a href="mailto:sgicer@163.com">阿汐</a> <br>
* Date: 2006-6-19 <br>
* Time: 16:41:09 <br>
* Version: 2.0.0 <br>
*/
public class JspToHtml {
private static String title ="标题测试";
private static String context ="标题测试";
private static String editer ="标题测试";
/**
* 按照本地模板生成静态页面
* @param JspFile jsp路经
* @param HtmlFile html路经
* @return
*/
public static boolean JspToHtmlFile(String filePath, String HtmlFile) {
String str = "";
long beginDate = (new Date()).getTime();
try {
String tempStr = "";
FileInputStream is = new FileInputStream(filePath);//读取模块文件
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while ((tempStr = br.readLine()) != null)
str = str + tempStr ;
is.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
try {
str = str.replaceAll("###title###",
title);
str = str.replaceAll("###content###",
context);
str = str.replaceAll("###author###",
editer);//替换掉模块中呼应的地方
File f = new File(HtmlFile);
BufferedWriter o = new BufferedWriter(new FileWriter(f));
o.write(str);
o.close();
System.out.println("共用时:" + ((new Date()).getTime() - beginDate) + "ms");
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 按照url生成静态页面
*
* @param u 动态文件路经 如:http://www.163.com/x.jsp
* @param path 文件存放路经如:x:\\abc\bbb.html
* @return
*/
public static boolean JspToHtmlByURL(String u, String path) {
//从utl中读取html存为str
String str = "";
try {
URL url = new URL(u);
URLConnection uc = url.openConnection();
InputStream is = uc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while (br.ready()) {
str += br.readLine() + "\n";
}
is.close();
//写入文件
File f = new File(path);
BufferedWriter o = new BufferedWriter(new FileWriter(f));
o.write(str);
o.close();
str = "";
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 按照url生成静态页面
*
* @param url 动态文件路经 如:http://www.163.com/x.jsp
* @return d
*/
public static StringBuffer getHtmlTextByURL(String url) {
//从utl中读取html存为str
StringBuffer sb = new StringBuffer();
try {
URL u = new URL(url);
URLConnection uc = u.openConnection();
InputStream is = uc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while (br.ready()) {
sb.append(br.readLine() + "\n");
}
is.close();
return sb;
} catch (Exception e) {
e.printStackTrace();
return sb;
}
}
/**
* 测试main 函数
*
* @param arg
*/
public static void main(String[] arg) {
long begin = System.currentTimeMillis();
//循环生成20个html文件
for (int k = 0; k < 20; k++) {
String url = "E:\\workspace\\oa\\golatel\\utils\\Temp\\mb.htm";//模板文件地址
String savepath = "d:/" + k + ".html";//生成文件地址
JspToHtmlFile(url, savepath);
}
System.out.println("用时:" + (System.currentTimeMillis() - begin) + "ms");
}
}
以上是“java 生成静态html的一段代码[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |
评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论