Java生成HTML文件

实例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 +" ";
                    }
                    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

原文地址:https://www.cnblogs.com/lanliying/p/4496895.html