Java读取根据HTML模板生成HTML页面

首先,我们需要一个html模板:

  1. <html>
  2. <head>
  3. <title>###title###</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gbk">
  5. </head>
  6.  
  7. <body>
  8. <table width="500" border="0" align="center" cellpadding="0" cellspacing="2">
  9. <tr>
  10. <td align="center">###title###</td>
  11. </tr>
  12. <tr>
  13. <td align="center">###author### </td>
  14. </tr>
  15. <tr>
  16. <td>###content###</td>
  17. </tr>
  18. <tr>
  19. <td>###html###</td>
  20. </tr>
  21.  
  22. </table>
  23. </body>
  24. </html>

其次:我们编写java代码实现模板读取,页面生成:

  1. import javax.servlet.http.HttpServletRequest;
  2. import java.util.List;
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.net.URL;
  6. import java.net.URLConnection;
  7. import java.io.*;
  8.  
  9. /**
  10. * Filename: JspToHtml.java <br>
  11. * Ttitle: jsp转换成html<br>
  12. * De.ion: 把动态网页转换成静态网页<br>
  13. * Copyright: Copyright (c) 2002-2008 BocSoft,Inc.All Rights Reserved. <br>
  14. * Company: BocSoft<br>
  15. * Author: <a href="mailto:sgicer@163.com">阿汐</a> <br>
  16. * Date: 2006-6-19 <br>
  17. * Time: 16:41:09 <br>
  18. * Version: 2.0.0 <br>
  19. */
  20. public class JspToHtml {
  21.  
  22. private static String title ="标题测试";
  23. private static String context ="标题测试";
  24. private static String editer ="标题测试";
  25.  
  26. /**
  27. * 根据本地模板生成静态页面
  28. * @param JspFile jsp路经
  29. * @param HtmlFile html路经
  30. * @return
  31. */
  32. public static boolean JspToHtmlFile(String filePath, String HtmlFile) {
  33. String str = "";
  34. long beginDate = (new Date()).getTime();
  35. try {
  36. String tempStr = "";
  37.   FileInputStream is = new FileInputStream(filePath);//读取模块文件
  38. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  39. while ((tempStr = br.readLine()) != null)
  40. str = str + tempStr ;
  41. is.close();
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. return false;
  45. }
  46. try {
  47.  
  48. str = str.replaceAll("###title###",title);
  49.    str = str.replaceAll("###content###",context);
  50.    str = str.replaceAll("###author###",editer);//替换掉模块中相应的地方
  51.  
  52. File f = new File(HtmlFile);
  53. BufferedWriter o = new BufferedWriter(new FileWriter(f));
  54. o.write(str);
  55. o.close();
  56. System.out.println("共用时:" + ((new Date()).getTime() - beginDate) + "ms");
  57. } catch (IOException e) {
  58. e.printStackTrace();
  59. return false;
  60. }
  61. return true;
  62. }
  63.  
  64. /**
  65. * 根据url生成静态页面
  66. *
  67. * @param u 动态文件路经 如:http://www.163.com/x.jsp
  68.  
  69. * @param path 文件存放路经如:x:\\abc\bbb.html
  70. * @return
  71. */
  72. public static boolean JspToHtmlByURL(String u, String path) {
  73. //从utl中读取html存为str
  74. String str = "";
  75. try {
  76. URL url = new URL(u);
  77. URLConnection uc = url.openConnection();
  78. InputStream is = uc.getInputStream();
  79. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  80. while (br.ready()) {
  81. str += br.readLine() + "\n";
  82.  
  83. }
  84. is.close();
  85. //写入文件
  86. File f = new File(path);
  87. BufferedWriter o = new BufferedWriter(new FileWriter(f));
  88. o.write(str);
  89. o.close();
  90. str = "";
  91. return true;
  92. } catch (Exception e) {
  93. e.printStackTrace();
  94. return false;
  95. }
  96. }
  97.  
  98. /**
  99. * 根据url生成静态页面
  100. *
  101. * @param url 动态文件路经 如:http://www.163.com/x.jsp
  102.  
  103. * @return d
  104. */
  105. public static StringBuffer getHtmlTextByURL(String url) {
  106. //从utl中读取html存为str
  107. StringBuffer sb = new StringBuffer();
  108. try {
  109. URL u = new URL(url);
  110. URLConnection uc = u.openConnection();
  111. InputStream is = uc.getInputStream();
  112. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  113. while (br.ready()) {
  114. sb.append(br.readLine() + "\n");
  115. }
  116. is.close();
  117. return sb;
  118. } catch (Exception e) {
  119. e.printStackTrace();
  120. return sb;
  121. }
  122. }
  123.  
  124. /**
  125. * 测试main 函数
  126. *
  127. * @param arg
  128. */
  129. public static void main(String[] arg) {
  130. long begin = System.currentTimeMillis();
  131.    //循环生成10个html文件
  132. for (int k = 0; k < 10; k++) {
  133. String url = "E:\\workspace\\oa\\golatel\\utils\\Temp\\mb.htm";//模板文件地址
  134. String savepath = "d:/" + k + ".html";//生成文件地址
  135. JspToHtmlFile(url, savepath);
  136. }
  137. System.out.println("用时:" + (System.currentTimeMillis() - begin) + "ms");
  138. }
  139. }
posted @ 2019-08-30 16:14  那些年的代码  阅读(4404)  评论(0编辑  收藏  举报