将给定数据源生成静态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>
效果:
将给定数据源生成静态HTML页面持久化到项目之外的硬盘的更多相关文章
- JSP生成静态Html页面
[转载]JSP生成静态Html页面 在网站项目中,为了访问速度加快,为了方便百度爬虫抓取网页的内容,需要把jsp的动态页面转为html静态页面.通常有2种常用的方式: 1.伪静态,使用URL Rewr ...
- 利用PHP的ob函数实现生成静态化页面
之前用过一些开源的CMS管理系统,当时就很好奇后台中的生成HTML静态文件是怎么实现的.今天和同事讨论了下,没想到同事之前做过这类的生成静态页面的功能,果断向他请教了下. 经他讲解后,才知道其实生成静 ...
- 用php生成静态html页面(通用2种方法)
因为每次用户点击动态链接的时候都会对服务器发送数据查询的要求 对于一个访问量可能达百万千万级别的网站来说 这无疑是服务器一个大大的负担 所以把动态数据转换成静态html页面就成了节省人力物力的首选 因 ...
- java使用freemarker生成静态html页面
1. 模板文件static.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...
- 【转载】JSP生成静态Html页面
在网站项目中,为了访问速度加快,为了方便百度爬虫抓取网页的内容,需要把jsp的动态页面转为html静态页面.通常有2种常用的方式: 1.伪静态,使用URL Rewriter 2.纯静态,本文中代码实现 ...
- springboot2.0结合freemarker生成静态化页面
目录 1. pom.xml配置 2. application.yml配置 3. 使用模板文件静态化 3.1 创建测试类,编写测试方法 3.2 使用模板字符串静态化 使用freemarker将页面生成h ...
- PHP基础-生成静态html页面原理是怎样
设置example.html为模板文件,然后按照此模板文件生成article-1.html~article-5.html,以此来做简单的演示,代码如下: <?php//将数据存入二维数组$con ...
- 比较详细PHP生成静态页面教程
一,PHP脚本与动态页面. PHP脚本是一种服务器端脚本程序,可通过嵌入等方法与HTML文件混合, 也可以类,函数封装等形式,以模板的方式对用户请求进行处理.无论以何种方式,它的基本原理是这样的.由客 ...
- .NET生成静态页面的方案总结
转载自:http://www.cnblogs.com/cuihongyu3503319/archive/2012/12/06/2804233.html 方法一:在服务器上指定aspx网页,生成html ...
随机推荐
- JAVA预科:Markdown基础语法
JAVA预科:Markdown语法 什么是Markdown?Markdown是一种轻量级的「标记语言」,通常为程序员群体所用,目前它已是全球最大的技术分享网站 GitHub 和技术问答网站 Stack ...
- WinUI 3学习笔记(2)—— 用ListView来展示集合
在WPF的时代,我们多是使用ListBox和ListView来展示,纵向滚动条显示的集合数据.这两个控件的默认样式,以及对触控的支持,已完全落后于时代.他们两个分别长这样,和Win10及Win11的风 ...
- MySQL的函数使用
目录 COUNT()函数基本使用 SUM( )函数基本使用 AVG()函数基本使用 MAX()函数基本使用 MIN()函数基本使用 字符函数 concat()和concat_ws() LEFT()与R ...
- Android招聘市场技术要求越来越高,从事三年开发是否应该考虑转行?
这是在某论坛看到的一个网友的疑问,他说感觉现在Android 开发这条路越来越难走了.下面是他的分享: 本来已经做好了好几个月找不到工作的准备.不过这两周感觉面试机会还算可以.两周10个面试感觉刚刚好 ...
- Logback 快速入门 / 使用详解
官方文档: http://logback.qos.ch/manual/index.html 一.简介 Java 开源日志框架,以继承改善 log4j 为目的而生,是 log4j 创始人 Ceki Gü ...
- Setup a Simple HTTP Proxy Server
The host 10.21.3.69 has no H3C client, so it can't access the internet. With Tinyproxy, we can setuu ...
- Java程序员的推荐阅读书籍
作为Java程序员来说,最痛苦的事情莫过于可以选择的范围太广,可以读的书太多,往往容易无所适从.我想就我自己读过的技术书籍中挑选出来一些,按照学习的先后顺序,推荐给大家,特别是那些想不断提高自己技术水 ...
- SpringMVC学习06(JSON)
6.Json 6.1 什么是JSON? JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式,目前使用特别广泛. 采用完全独立于编程语言的文本 ...
- Something about 树链剖分
声明:部分思路与图片源于OI Wiki 关于树链剖分 树链剖分用于将树分割成若干条链的形式,以维护树上路径的信息. 树链剖分有多种形式,如 重链剖分,长链剖分 和用于 $LCT$ 的剖分,大多数情况下 ...
- NOIP 模拟 $17\; \rm 时间机器$
题解 \(by\;zj\varphi\) 一道贪心的题目 我们先将节点和电阻按左边界排序,相同的按右边界排序 对于每一个节点,我们发现,选取左边界小于等于它的电阻中右边界大于它且最接近的它的一定是最优 ...