完整代碼:
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>