zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    爬楼梯
    字母异位词分组
    发射子弹过程
    删除元素
    删除排序数组中的重复项
    JAVA编程-------------9、查找1000以内的完数
    JAVA-------------8、计算a+aa+aaa+.....
    JAVA编程----------7、统计一段字符串中的英语字母数,空格数,数字和其他字符数
    JAVA编程---------6、最大公约数和最小公倍数
    JAVA编程----------5、利用条件运算符的嵌套来完成:成绩>=90(A) 60-89(B) 60分以下(C)
  • 原文地址:https://www.cnblogs.com/lanliying/p/4496895.html
Copyright © 2011-2022 走看看