一、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页面持久化到项目之外的硬盘的更多相关文章

  1. JSP生成静态Html页面

    [转载]JSP生成静态Html页面 在网站项目中,为了访问速度加快,为了方便百度爬虫抓取网页的内容,需要把jsp的动态页面转为html静态页面.通常有2种常用的方式: 1.伪静态,使用URL Rewr ...

  2. 利用PHP的ob函数实现生成静态化页面

    之前用过一些开源的CMS管理系统,当时就很好奇后台中的生成HTML静态文件是怎么实现的.今天和同事讨论了下,没想到同事之前做过这类的生成静态页面的功能,果断向他请教了下. 经他讲解后,才知道其实生成静 ...

  3. 用php生成静态html页面(通用2种方法)

    因为每次用户点击动态链接的时候都会对服务器发送数据查询的要求 对于一个访问量可能达百万千万级别的网站来说 这无疑是服务器一个大大的负担 所以把动态数据转换成静态html页面就成了节省人力物力的首选 因 ...

  4. java使用freemarker生成静态html页面

    1. 模板文件static.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...

  5. 【转载】JSP生成静态Html页面

    在网站项目中,为了访问速度加快,为了方便百度爬虫抓取网页的内容,需要把jsp的动态页面转为html静态页面.通常有2种常用的方式: 1.伪静态,使用URL Rewriter 2.纯静态,本文中代码实现 ...

  6. springboot2.0结合freemarker生成静态化页面

    目录 1. pom.xml配置 2. application.yml配置 3. 使用模板文件静态化 3.1 创建测试类,编写测试方法 3.2 使用模板字符串静态化 使用freemarker将页面生成h ...

  7. PHP基础-生成静态html页面原理是怎样

    设置example.html为模板文件,然后按照此模板文件生成article-1.html~article-5.html,以此来做简单的演示,代码如下: <?php//将数据存入二维数组$con ...

  8. 比较详细PHP生成静态页面教程

    一,PHP脚本与动态页面. PHP脚本是一种服务器端脚本程序,可通过嵌入等方法与HTML文件混合, 也可以类,函数封装等形式,以模板的方式对用户请求进行处理.无论以何种方式,它的基本原理是这样的.由客 ...

  9. .NET生成静态页面的方案总结

    转载自:http://www.cnblogs.com/cuihongyu3503319/archive/2012/12/06/2804233.html 方法一:在服务器上指定aspx网页,生成html ...

随机推荐

  1. 自学linux——10.Linux的网络知识

    linux的网络知识 一.网络相关概述 1.网络的分类 局域网(LAN):在几百米到十几公里内办公楼群或校园内的计算机相互连接所构成的计算机网络 城域网(MAN):覆盖相距不远的几栋办公楼,也可以覆盖 ...

  2. python ui自动化之多窗口切换

    wd.current_window_handle #获取当前窗口句柄 wd.window_handles #获取所有窗口句柄 wd.switch_to.window(wd.window_handles ...

  3. 算法竞赛中的常用JAVA API:PriorityQueue(优先队列)(转载)

    算法竞赛中的常用JAVA API:PriorityQueue(优先队列) PriorityQueue 翻译过来就是优先队列,本质是一个堆, 默认情况下堆顶每次都保留最小值,每插入一个元素,仍动态维护堆 ...

  4. VIM正则表达式的懒惰模式

    例如有如下一段话: Another whale sighting occurred on , <2004>. AK and HI 要匹配每一个<...>,如果用<.> ...

  5. STP工作流程

    第一步:选择一个根网桥: 第二步:在每个非根网乔上选举一个根端口: 第三步:在每个网段上选举一个指定端口: 第四步:阻塞非根,非指定端口:

  6. MATLAB—M函数文件

    文章目录 一.M文件 1.脚本文件和函数文件的区别 二.M函数文件 1.创建方法 2.文件形式 2.函数形参及注释 3.函数的调用 4.函数变量 5.主函数.子函数 三.函数句柄 一.M文件 首先,要 ...

  7. python初始化定义空变量-小记

    python初始化定义空变量 数值 digital_value = 0 字符串 str_value = "" 列表 list_value = [] 字典 ditc_value = ...

  8. NOIP 模拟 $14\; \text{抛硬币}$

    题解 \(by\;\;zj\varphi\) 签到题,自己看题解 Code #include<bits/stdc++.h> #define ri register signed #defi ...

  9. noip模拟15

    T1 恶心的数学题,还卡空间... 于是考虑数组二次调用,用完memset 记录一手二维前缀和对不同询问离线修改,最后一块回答即可 Code #include<cstdio> #inclu ...

  10. 转: SIFT原理解释

    1.SIFT概述 SIFT的全称是Scale Invariant Feature Transform,尺度不变特征变换,由加拿大教授David G.Lowe提出的.SIFT特征对旋转.尺度缩放.亮度变 ...