日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

将给定数据源生成静态HTML页面持久化到项目之外的硬盘

發布時間:2023/12/10 HTML 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 将给定数据源生成静态HTML页面持久化到项目之外的硬盘 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、java代碼

設置好數據源map

Map<String,String> map=new HashMap<>();map.put("knowledgeName",tBasKnowledgebase.getKnowledgeName());map.put("htmlContent",tBasKnowledgebase.getHtmlContent());map.put("publishDate",new java.text.SimpleDateFormat("yyyy-MM-dd").format(tBasKnowledgebase.getPublishDate()));CreateHtmlUtils.MakeHtml(rpath,map,htmlPath,tBasKnowledgebase.getKnowledgeId());

工具類方法

/*** @Title: MakeHtml* @Description: 創建html* @param filePath 設定模板文件* @param map 需要顯示圖片的路徑* @param disrPath 生成html的存放路徑* @param fileName 生成html名字* @return void 返回類型* @throws*/public static void MakeHtml(String filePath, Map<String,String> map, String disrPath, String fileName ){try {System.out.print(filePath);String templateContent = "";// 讀取模板文件,模板文件,是工程中的一個html頁面,里面有一些需要替換的字段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");System.out.print(templateContent);// 替換掉模板中的一些字段,填充數據渲染頁面for (Map.Entry<String,String> entry : map.entrySet()) {String key=entry.getKey();String value=entry.getValue();templateContent = templateContent.replaceAll("###"+key+"###", value);System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());}System.out.print(templateContent);String fileame = fileName + ".html";// 生成的html文件保存路徑,html文件全路徑,選擇服務器上工程目錄下以外的路徑,持久化存儲到硬盤,這樣發布新版本原來的靜態文件不會丟失fileame = disrPath+"/" + fileame;// 根據文件全路徑創建file對象File file=new File(fileame);if(!file.getParentFile().exists()){file.getParentFile().mkdirs();}if(!file.exists()){file.createNewFile();}FileOutputStream fileOutputStream=new FileOutputStream(file);OutputStreamWriter oStreamWriter = new OutputStreamWriter(fileOutputStream, "utf-8");// 將替換完數據的模板頁面形成的文件流持久化到硬盤 oStreamWriter.append(templateContent);oStreamWriter.close();} catch (Exception e) {System.out.print(e.toString());}}

工程中的模板HTML文件:

<!DOCTYPE html> <html lang="en"> <head><meta charset="utf-8" /><title>###knowledgeName###</title><style>body{ text-align:center;border: 0;margin: 0;}.div{ margin:20px auto;max-width: 1000px}.title{font-size: 20px;font-weight: bold;margin-bottom: 20px;}.date{border-bottom: 1px solid #9a9a9a;padding: 0 16px 16px;font-size: 15px;}.content{text-align: left;padding-top: 10px;line-height: 30px;}.toolsBox {height: 36px;border: 1px solid #d8d8d8;background: #fafbfb;font-size: 14px;color: #666;line-height: 36px;text-align: center;margin-top: 70px;}.toolsBox a, .toolsBox i {color: #666;cursor: pointer;font-style: normal;text-decoration: none;}.toolsBox i.on {color: #ff8400;}</style> </head> <body> <div class="div"><div class="title">###knowledgeName###</div><div class="date">###publishDate###</div><div class="content">###htmlContent###</div><div class="toolsBox">字體:【 <i>大</i>  <i @cile class="on">中</i>  <i>小</i> 】<a href="javascript:window.print();">【打印 】</a><a href="javascript:window.close();">【關閉】</a></div> </div> <script src="/js/jquery-1.8.3.min.js"></script> <script type="text/javascript">(function () {// 字號設置 function setFontSize(oElement,size){var size = size || 14;$(oElement).css({'fontSize': size+"px"});}$('.toolsBox i').eq(0).click(function() {setFontSize('.content p','16');$(this).siblings('i').removeClass('on');$(this).addClass('on');});$('.toolsBox i').eq(1).click(function() {setFontSize('.content p','14');$(this).siblings('i').removeClass('on');$(this).addClass('on');});$('.toolsBox i').eq(2).click(function() {setFontSize('.content p','12');$(this).siblings('i').removeClass('on');$(this).addClass('on');});})();</script> </body> </html>

效果:

?

轉載于:https://www.cnblogs.com/wmqiang/p/11603143.html

總結

以上是生活随笔為你收集整理的将给定数据源生成静态HTML页面持久化到项目之外的硬盘的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。