一、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>
效果: