完整代码:
package ....service; import java.io.FileInputStream; import java.io.FileOutputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; public class HtlmInfo { //主页面本地地址 //模板路径 private static String filePath = "D:\\tycx\\item\\AllHtml.html"; //生成主页面路径 private static String disrPath = "D:\\tycx\\item\\"; //子页面本地地址 //模板1路径 private static String AllfilePath = "D:\\tycx\\item\\table.html"; //模板2路径 private static String AllfilePathTS = "D:\\tycx\\item\\tableTS.html"; //生成子页面路径 private static String AlldisrPath = "D:\\tycx\\item\\AllHtml\\"; //文件后缀 private static String fileName = ".html"; //主页面 public static void MakeHtml(JSONArray JGarray,JSONArray GRarray){ try { String templateContent = ""; FileInputStream fileinputstream = new FileInputStream(filePath);// 读取模板文件 int lenght = fileinputstream.available(); byte bytes[] = new byte[lenght]; fileinputstream.read(bytes); fileinputstream.close(); templateContent = new String(bytes,"UTF-8"); //按时间对jsonArray数组排序 String JGarrays = jsonArraySort(JGarray.toString()); JSONArray JGarr = JSONArray.parseArray(JGarrays); String GRarrays = jsonArraySort(GRarray.toString()); JSONArray GRarr = JSONArray.parseArray(GRarrays); //机构 StringBuffer JGtitleTH = new StringBuffer(); for(int i=0;i<JGarr.size();i++){ JSONObject json = JGarr.getJSONObject(i); String JGtitle = json.containsKey("title")?json.getString("title"):""; String JGpublishtime = json.containsKey("publish_time")?json.getString("publish_time"):""; //将时间戳转换 SimpleDateFormat JGsdf = new SimpleDateFormat("yyyy 年 MM 月 dd 日 "); String JGpublish_time= JGsdf.format(new Date(Long.parseLong(String.valueOf(JGpublishtime)))); //给JGtitlet添加跳转子页面的超连接 String count = "<tr><td><font size='4' color='blue'><a href='../item/AllHtml/"+JGtitle+".html'>"+ JGtitle+"</a></td>"+ "<td><font size='4' color='Gray'>"+JGpublish_time+"</td></tr>"; JGtitleTH .append(count); System.out.println("JGtitleTH : "+JGtitleTH); } System.out.println("JGtitleTH : "+JGtitleTH); //把模板页面上的 ###JGcount### 替换成 JGtitleTH 里的内容 templateContent = templateContent.replaceAll("###JGcount###", JGtitleTH.toString()); //个人(一样的逻辑) StringBuffer GRtitleTH = new StringBuffer(); for(int i=0;i<GRarr.size();i++){ JSONObject json = GRarr.getJSONObject(i); String GRtitle = json.getString("title"); String GRpublishtime = json.getString("publish_time"); SimpleDateFormat GRsdf = new SimpleDateFormat("yyyy 年 MM 月 dd 日 "); String GRpublish_time= GRsdf.format(new Date(Long.parseLong(String.valueOf(GRpublishtime)))); String count = "<tr><td><font size='4' color='blue'><a href='../item/AllHtml/"+GRtitle+".html'>"+ GRtitle+"</a></td>"+ "<td><font size='4' color='Gray'>"+GRpublish_time+"</td></tr>"; GRtitleTH .append(count); System.out.println("JGtitleTH : "+GRtitleTH); } System.out.println("JGtitleTH : "+GRtitleTH); //把模板页面上的 ###GRcount### 替换成 GRtitleTH 里的内容 templateContent = templateContent.replaceAll("###GRcount###", GRtitleTH.toString()); //开始输出生成的页面 String fileame = "bsznSearch"+fileName; fileame = disrPath + fileame;// 生成的html文件保存路径。 FileOutputStream fileoutputstream = new FileOutputStream(fileame);// 建立文件输出流 System.out.print("文件输出路径:"); System.out.print(fileame); byte tag_bytes[] = templateContent.getBytes("UTF-8"); fileoutputstream.write(tag_bytes); fileoutputstream.close(); } catch (Exception e) { System.out.print(e.toString()); } } //子页面 public static void MakeAllHtml(JSONObject json){ try { String templateContent = ""; FileInputStream fileinputstream=null; //这个JSON判断可以忽略(可能是接口返回数据问题必须这么写,不然不生效) String author =json.containsKey("author")?json.getString("author"):""; if(author!=null){ fileinputstream = new FileInputStream(AllfilePath);// 读取标准模板文件 }else{ fileinputstream = new FileInputStream(AllfilePathTS);// 读取非标准模板文件 } int lenght = fileinputstream.available(); byte bytes[] = new byte[lenght]; fileinputstream.read(bytes); fileinputstream.close(); templateContent = new String(bytes,"UTF-8"); String title = json.containsKey("title")?json.getString("title"):""; String html_text = json.containsKey("html_text")?json.getString("html_text"):""; String publishtime = json.containsKey("publish_time")?json.getString("publish_time"):""; //看场景,这里需要转译 html_text = html_text.replaceAll("\\\\u200c\\\\u200b", ""); SimpleDateFormat sdf = new SimpleDateFormat("yyyy 年 MM 月 dd 日 "); String publish_time= sdf.format(new Date(Long.parseLong(String.valueOf(publishtime)))); //把模板页面上的 ###text### 替换成 title 里的内容 templateContent = templateContent.replaceAll("###title###", title.trim()); //标准模板比非标准模板多个替换项,不判断会报错 if( author!=null){ templateContent = templateContent.replaceAll("###author###", author.trim()); } templateContent = templateContent.replaceAll("###html_text###", html_text.trim()); templateContent = templateContent.replaceAll("###publish_time###", publish_time); String fileame = title+fileName; fileame = AlldisrPath + fileame;// 生成的html文件保存路径。 FileOutputStream fileoutputstream = new FileOutputStream(fileame);// 建立文件输出流 // System.out.print("文件输出路径:"); // System.out.print(fileame); byte tag_bytes[] = templateContent.getBytes("UTF-8"); fileoutputstream.write(tag_bytes); fileoutputstream.close(); } catch (Exception e) { System.out.print(e.toString()); } } /** * 按照JSONArray中的对象的某个字段进行排序(采用fastJson) * * @param jsonArrStr * json数组字符串 * */ public static String jsonArraySort(String jsonArrStr) { JSONArray jsonArr = JSON.parseArray(jsonArrStr); JSONArray sortedJsonArray = new JSONArray(); List<JSONObject> jsonValues = new ArrayList<JSONObject>(); for (int i = 0; i < jsonArr.size(); i++) { jsonValues.add(jsonArr.getJSONObject(i)); } Collections.sort(jsonValues, new Comparator<JSONObject>() { // You can change "Name" with "ID" if you want to sort by ID private static final String KEY_NAME = "publish_time"; @Override public int compare(JSONObject a, JSONObject b) { String valA = new String(); String valB = new String(); try { // 这里是a、b需要处理的业务,需要根据你的规则进行修改。 String aStr = a.getString(KEY_NAME); valA = aStr.replaceAll("-", ""); String bStr = b.getString(KEY_NAME); valB = bStr.replaceAll("-", ""); } catch (JSONException e) { // do something } return -valA.compareTo(valB); // if you want to change the sort order, simply use the following: // return -valA.compareTo(valB); } }); for (int i = 0; i < jsonArr.size(); i++) { sortedJsonArray.add(jsonValues.get(i)); } return sortedJsonArray.toString(); } }
html模板的替换参数示例
<body> <div id="container"> <div class="common-crumb"> <div class="new-common-content"> <ul> <li>当前位置:</li> <li><a href="">首页</a> <i class="fa fa-angle-right"></i></li> <li>事项定位</li> </ul> </div> </div> <div class='new-common-content contentBox'> <div class="page-title"> <h2 class="text-center fs28 fw_blod marginB20P" style="padding: 0 50px;margin-top: 50px;; text-align:center">###title###</h2> <p style="font-size:18PX;color:#A1A1A1;text-align:center"> <span>###publish_time###</span> <span>###author###</span> </p> </div> <div class="fs16" style="font-size:19PX; padding:50px;line-height:1.5;"> ###html_text### </div> </body>