Java產生HTML檔案

來源:互聯網
上載者:User

標籤:

執行個體HTML檔案
<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>

Java代碼:

 

package com.util;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.text.SimpleDateFormat;

import com.entity.Template;

/**
 * 產生HTML
 */
public class MakeHTML {
  /**
     * 根據本地模板產生靜態頁面
     * @param JspFile    jsp路經
     * @param HtmlFile html路經
     * @return
     */
    public static boolean JspToHtmlFile(Template t,String filePath, String HtmlFile) {
     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String str = "";
            try {
                    String tempStr = "";
                    InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), "UTF-8");
                    BufferedReader br = new BufferedReader(isr);
                    while ((tempStr = br.readLine()) != null){
                      str = str + tempStr +"\n";
                    }
                    System.out.println(str);
            } catch (IOException e) {
                    e.printStackTrace();
                    return false;
            }
            try {
              
              str = str.replaceAll("###softwareName###",t.getSoftwareName());
              str=str.replaceAll("###downloads###", t.getDownloads());
              str = str.replaceAll("###icon###",t.getIcon());
              str = str.replaceAll("###type###",t.getType());
              str = str.replaceAll("###size###",t.getSize());
              str = str.replaceAll("###version###",t.getVersion());
              str=str.replaceAll("###updateTime###", format.format(t.getUpdateTime()));
              str = str.replaceAll("###qrcode###",t.getQrcode());
              str = str.replaceAll("###filepath###",t.getFilepath());
              str = str.replaceAll("###introduce###",t.getIntroduce());
              str = str.replaceAll("###screenshot###",t.getScreenshot());
              str = str.replaceAll("###feature###",t.getFeature());//替換掉模組中相應的地方
              //必須設定編碼格式不然會出現亂碼
              BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(HtmlFile),"UTF-8"));
              bufferedWriter.write(str);
              bufferedWriter.newLine();//換行
              /* * 重新整理該流的緩衝。
              * 關鍵的一行代碼。如果沒有加這行代碼。資料只是儲存在緩衝區中。沒有寫進檔案。
              * 加了這行才能將資料寫入目的地。 * */
              bufferedWriter.flush();
              bufferedWriter.close();
            } catch (IOException e) {
                    e.printStackTrace();
                    return false;
            }
            return true;
    }
}

參考:http://www.newxing.com/Tech/Java/Web/107.html

http://blog.csdn.net/maxracer/article/details/5436580

http://www.itzk.com/thread-581970-52-1.shtml

http://blog.csdn.net/qingchenyuji/article/details/8236322

 

Java產生HTML檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.