Jsp自动生成html
先建立一个模板页面:template.htm
\OP%U7MoQO0<html><head><title>###title###</title>
k7SC;v.\ S L3UD3w0<meta. http- equiv="Content-Type" content="text/html; charset=gb2312">
0n Ky0F*O"o:?5Mz0<LINK href="../css.css" rel=stylesheet type=text/css></head>
+J{Ax&eX|0<body>
@$c ?V[(H1z0<table width="500" border="0" align="center" cellpadding="0" cellspacing="2">ITPUB个人空间%~7Pm4Ix:p
<tr><td align="center">###title###</tr>
/e;Wjy8pC5Lh0<tr><td align="center">作者:###author###</tr>
ml Rxn0<tr><td align="center">###content###</td></tr>ITPUB个人空间,Y].}u,uQq D QP
</table>
#pp"J G]n0</body>
FrET0N-hd.I0</html>
mE5f'MC0
w/?K,|ES~0再写一个Java页面: buildhtml.java:ITPUB个人空间/v |*B(v_X
ITPUB个人空间5dUK Ij
import java.util.*;ITPUB个人空间k;AHA1yPb ^%J
import java.io.*;ITPUB个人空间GEwR X$|C4C|^
public class HtmlFile{
Ei9qN!S4Y3bP:Y-F0public static void main(String[] args){
Zq ]{)_xh*L0try{ITPUB个人空间Iboa t!r5sM4s
String title="Make Html";ITPUB个人空间;U,{H$~bCL s0dF
String content="小样,还搞不定你?";
(c6?gi/M)c0String editer="Hulihutu";ITPUB个人空间K)O/\6QPR2z
String filePath = "";
1C&I#|&@V!q.I'fw-R0filePath ="template.html";
y(s#Jh?0c*Qo0System.out.print(filePath);ITPUB个人空间2h'u:I+k8GE9ySX
String templateContent="";
6Pz,h4m#UK9w.M0FileInputStream fileinputstream = new FileInputStream(filePath);// 读取模板文件ITPUB个人空间x8`/y9sA{ qd~
int lenght = fileinputstream.available();
O;]Y8P%H0byte bytes[] = new byte[lenght];
2G4SvP0i_0fileinputstream.read(bytes);
n w^-JwbtU0fileinputstream.close();ITPUB个人空间k0Aq%? y*n(S
templateContent = new String(bytes);ITPUB个人空间]xnQH?b-h u
System.out.print(templateContent);ITPUB个人空间sr/\x*K&n~h _([
templateContent=templateContent.replaceAll("###title###",title);ITPUB个人空间%w,NA s,e ^&U;f B!}"a5U
templateContent=templateContent.replaceAll("###content###",content);ITPUB个人空间 a Yc v5}mUl5J
templateContent=templateContent.replaceAll("###author###",editer);// 替换掉模板中相应的地方
0[ B*h r?9gYm%Mx0System.out.print(templateContent);
i&`8Md9p|-Z k/W0ITPUB个人空间;^aPBHc
// 根据时间得文件名ITPUB个人空间9| _.M(L)j OZZ
Calendar calendar = Calendar.getInstance();
nc{!O!u^2g;O/z n0String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";ITPUB个人空间:@6e^m,bdvx
fileame = "/" + fileame;// 生成的html文件保存路径。
@-n#P)o!i+qe;gp&H0FileOutputStream fileoutputstream = new FileOutputStream(fileame);// 建立文件输出流ITPUB个人空间I'\`hb
System.out.print("文件输出路径:");ITPUB个人空间d7jWH E)uxoLG
System.out.print(fileame);
O8wM7fv0byte tag_bytes[] = templateContent.getBytes();
/]A9x0@ H3{0fileoutputstream.write(tag_bytes);
?6QlaKT}0s0fileoutputstream.close();ITPUB个人空间)U y8V!d\\#ci5Gu
}catch(Exception e){
|RId]*w{{^0System.out.print(e.toString());ITPUB个人空间g;A `| u8wsm o,~
}
(y]w;U N oI0}ITPUB个人空间;XX9M Bv$W ZkI4UJj
}
\OP%U7MoQO0<html><head><title>###title###</title>
k7SC;v.\ S L3UD3w0<meta. http- equiv="Content-Type" content="text/html; charset=gb2312">
0n Ky0F*O"o:?5Mz0<LINK href="../css.css" rel=stylesheet type=text/css></head>
+J{Ax&eX|0<body>
@$c ?V[(H1z0<table width="500" border="0" align="center" cellpadding="0" cellspacing="2">ITPUB个人空间%~7Pm4Ix:p
<tr><td align="center">###title###</tr>
/e;Wjy8pC5Lh0<tr><td align="center">作者:###author###</tr>
ml Rxn0<tr><td align="center">###content###</td></tr>ITPUB个人空间,Y].}u,uQq D QP
</table>
#pp"J G]n0</body>
FrET0N-hd.I0</html>
mE5f'MC0
w/?K,|ES~0再写一个Java页面: buildhtml.java:ITPUB个人空间/v |*B(v_X
ITPUB个人空间5dUK Ij
import java.util.*;ITPUB个人空间k;AHA1yPb ^%J
import java.io.*;ITPUB个人空间GEwR X$|C4C|^
public class HtmlFile{
Ei9qN!S4Y3bP:Y-F0public static void main(String[] args){
Zq ]{)_xh*L0try{ITPUB个人空间Iboa t!r5sM4s
String title="Make Html";ITPUB个人空间;U,{H$~bCL s0dF
String content="小样,还搞不定你?";
(c6?gi/M)c0String editer="Hulihutu";ITPUB个人空间K)O/\6QPR2z
String filePath = "";
1C&I#|&@V!q.I'fw-R0filePath ="template.html";
y(s#Jh?0c*Qo0System.out.print(filePath);ITPUB个人空间2h'u:I+k8GE9ySX
String templateContent="";
6Pz,h4m#UK9w.M0FileInputStream fileinputstream = new FileInputStream(filePath);// 读取模板文件ITPUB个人空间x8`/y9sA{ qd~
int lenght = fileinputstream.available();
O;]Y8P%H0byte bytes[] = new byte[lenght];
2G4SvP0i_0fileinputstream.read(bytes);
n w^-JwbtU0fileinputstream.close();ITPUB个人空间k0Aq%? y*n(S
templateContent = new String(bytes);ITPUB个人空间]xnQH?b-h u
System.out.print(templateContent);ITPUB个人空间sr/\x*K&n~h _([
templateContent=templateContent.replaceAll("###title###",title);ITPUB个人空间%w,NA s,e ^&U;f B!}"a5U
templateContent=templateContent.replaceAll("###content###",content);ITPUB个人空间 a Yc v5}mUl5J
templateContent=templateContent.replaceAll("###author###",editer);// 替换掉模板中相应的地方
0[ B*h r?9gYm%Mx0System.out.print(templateContent);
i&`8Md9p|-Z k/W0ITPUB个人空间;^aPBHc
// 根据时间得文件名ITPUB个人空间9| _.M(L)j OZZ
Calendar calendar = Calendar.getInstance();
nc{!O!u^2g;O/z n0String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";ITPUB个人空间:@6e^m,bdvx
fileame = "/" + fileame;// 生成的html文件保存路径。
@-n#P)o!i+qe;gp&H0FileOutputStream fileoutputstream = new FileOutputStream(fileame);// 建立文件输出流ITPUB个人空间I'\`hb
System.out.print("文件输出路径:");ITPUB个人空间d7jWH E)uxoLG
System.out.print(fileame);
O8wM7fv0byte tag_bytes[] = templateContent.getBytes();
/]A9x0@ H3{0fileoutputstream.write(tag_bytes);
?6QlaKT}0s0fileoutputstream.close();ITPUB个人空间)U y8V!d\\#ci5Gu
}catch(Exception e){
|RId]*w{{^0System.out.print(e.toString());ITPUB个人空间g;A `| u8wsm o,~
}
(y]w;U N oI0}ITPUB个人空间;XX9M Bv$W ZkI4UJj
}
导入论坛 引用链接 收藏 分享给好友 推荐到圈子 管理 举报
TAG: