将给定数据源生成静态HTML页面持久化到项目之外的硬盘
时间:2019-09-28
本文章向大家介绍将给定数据源生成静态HTML页面持久化到项目之外的硬盘,主要包括将给定数据源生成静态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
- RabbitMQ与AMQP协议
- 大数据算法设计模式(2) - 左外链接(leftOuterJoin) spark实现
- hs_err_pid
- django celery的分布式异步之路(二) 高并发
- django celery的分布式异步之路(一) 起步
- SpringMVC拦截器Interceptor
- 元宵快乐:看SQL大师们用SQL绘制的团圆
- Python Redis pipeline操作
- python concurrent.futures
- Deepmind的星际争霸2强化学习教程(1):建立环境与训练模型
- python contextlib 上下文管理器
- Django扩展自定义manage命令
- Elasticsearch分片、副本与路由(shard replica routing)
- EsRejectedExecutionException排错与线程池类型
- HTML 教程
- HTML 简介
- html div 标签介绍
- html span 标签介绍
- html a 超链接标签
- HTML Br换行标签介绍
- HTML P段落标签介绍
- HTML br与p标签区别
- Html H 标题标签
- html px em pt长度单位
- HTML form 标签
- HTML radio 单选框
- HTML B 加粗标签
- HTML strong加粗粗体标签
- HTML em 强调标签
- HTML i 斜体标签
- HTML u下划线标签
- HTML s 删除线标签
- Html img 图片标签
- Html上标注sup与下标注sub标签
- HTML nobr 禁止换行标签
- HTML hr 水平线标签
- HTML label 标签
- HTML input 标签
- HTML textarea 标签
- HTML select下拉列表标签
- HTML checkbox 多选框
- HTML font color 标签
- HTML iframe 框架标签
- HTML Table 表格
- HTML dl dt dd 标签
- HTML ol li有序列表标签
- HTML ul li 无序列表标签
- HTML 注释
- CSS 教程
- CSS 简介
- CSS 语法
- CSS Id 和 Class选择器
- CSS 样式的创建
- CSS background 背景介绍
- CSS 文本样式
- CSS font 字体
- CSS A 链接
- CSS ul ol列表样式
- CSS TABLE 样式
- CSS 框模型
- CSS border 边框
- CSS Outlines 轮廓
- CSS 外边距 Margin
- CSS Padding 内边距
- CSS 分组和嵌套选择器
- CSS 尺寸 (Dimension)
- CSS Display 属性
- CSS Position 定位
- CSS Float 浮动
- CSS 水平对齐(Horizontal Align)
- CSS 组合选择符
- CSS 伪类
- CSS 伪元素
- CSS 导航栏
- CSS 下拉菜单
- CSS 图片廊
- CSS 图像透明/不透明
- CSS sprite 图像拼合技术
- CSS 媒体类型
- CSS 属性选择器
- CSS 实例