java开发如何将动态页面转化为静态页面发布
2013-04-07
展开全部
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.*;
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");
}
}
//模板文件名称 leon.html
<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###
</tr>
<tr>
<td align="center">
作者:###author###
</tr>
<tr>
<td align="center">
###content###
</td>
</tr>
</table>
</body>
</html>
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.*;
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");
}
}
//模板文件名称 leon.html
<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###
</tr>
<tr>
<td align="center">
作者:###author###
</tr>
<tr>
<td align="center">
###content###
</td>
</tr>
</table>
</body>
</html>
展开全部
如果是伪静态只要想办法包装一下url,让他看起来像个静态地址就行。
如果想要真静态,可以通过io,把页面写到一个真实的html文件中,以后可以直接访问这个这个文件地址。
如果想要真静态,可以通过io,把页面写到一个真实的html文件中,以后可以直接访问这个这个文件地址。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用IO流将动态内容往指定目录写成HTML,作为静态页面进行访问
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |