用戶登錄  |  用戶注冊
首 頁源碼下載網絡學院最新源碼源碼排行屏蔽廣告
當前位置:新興網絡 > 網絡學院 > Java編程 > Java Web

Java 讀取模板并生成HTML靜態文件實例

減小字體 增大字體 作者:九黎部落  來源:九黎部落  發布時間:2010-06-03 10:47:57

原理都很簡單,主要是對模板的解析.so,我們先準備一個html模板
mb.html,做個文件其中的###title###之類的標簽用于程序進行查詢替換.

HTML code復制代碼
<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

Java code復制代碼
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");
        }
}
  • 好的評價 如果您覺得此文章好,就請您
      88%(45)
  • 差的評價 如果您覺得此文章差,就請您
      12%(6)

網絡學院評論評論內容只代表網友觀點,與本站立場無關!

   評論摘要(共 1 條,得分 100 分,平均 100 分) 查看完整評論
[回復] 1游客   打分:100 分  發表時間:2017-11-08
· 運行結果是什么樣的啊
秒速时时彩