将给定数据源生成静态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 ...
随机推荐
- YsoSerial 工具常用Payload分析之Common-Collections2、4(五)
前言 Common-Collections <= 3.2.1 对应与YsoSerial为CC1.3.5.6.7 ,Commno-collections4.0对应与CC2.4. 这篇文章结束官方原 ...
- JS基础-数据类型判断typeof、instanceof、Object.prototype.toString
typeof用在基本数据类型和函数时,返回其对应类型的描述,对于引用类型都返回为object. instanceof无法判断基本数据类型,对于引用类型数据,返回其其对应类型. Object.proto ...
- OceanBase三节点部署&&扩容
OceanBase三节点部署&&扩容 环境信息搭建三节点(1-1-1)创建资源池和租户查看数据分布 环境信息 IP OB目录 端口 192.168.43.89 /data/observ ...
- 解决Win10用户VS Code的C/C++更新到1.6.0后无法调试的问题
今天突然遇到一个问题 Win10上 vscode C++突然无法正常调试 在运行调试后 编译成功后没有任何提示 直接就停止了 没有错误 不运行程序 尝试重新写一遍launch.json 自动生成lau ...
- 【Unity3D】Android App Bundle(aab)打包上架Google Play介绍
总体说来,Android App Bundle打包有3种方式,每种方式都有成功上架Google Play进行测试通过,因此实用程度还是挺高的.能够理解以下内容的前提是会打apk包,知道如何生成Asse ...
- 如何让win10开机默认开启小键盘?
如何让win10开机默认开启小键盘? windows10默认是关闭小键盘的.每次输入开机密码的时候都需要按一下小键盘开启键(numlock),才能按数字键 一.运行注册表编辑器,修改InitialKe ...
- solr7.4.0+mysql+solrj(简而优美)
目录: 1 solr7部署+创建核心2 solr mysql 连接 2.1 导入相关 jar包 2.2 配置连接信息 2.3 配置中文分析器3 solrj JAVA客户端应用 3.1 solrj 构建 ...
- 5、Python断言及常用断言函数总结
Python断言 Python assert 语句,又称断言语句,可以看做是功能缩小版的 if 语句,它用于判断某个表达式的值,如果值为真,则程序可以继续往下执行:反之,Python 解释器会报 As ...
- Java - Enum 枚举类型
目录 前言 应用 定义 基本Enum特性 Enum的静态导入 Enum中添加新方法 Switch语句中的Enum Enum的继承 EnumSet的使用 EnumMap的使用 常量相关方法 枚举值向枚举 ...
- NOIP 模拟 $15\; \text{夜莺与玫瑰}$
题解 一道很妙的题,让求对于一个矩阵中,两点相连成线,有多少条直线,他们的交集是有限集. 转化一下题目,发现水平和竖直的只有 \(n+m\) 条,而左斜和右斜的条数是相同的,所以我们只需求出左或右中的 ...