将给定数据源生成静态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 ...
随机推荐
- 为了彻底搞懂 hashCode,我钻了一下 JDK 的源码
今天我们来谈谈 Java 中的 hashCode() 方法--通过源码的角度.众所周知,Java 是一门面向对象的编程语言,所有的类都会默认继承自 Object 类,而 Object 的中文意思就是& ...
- swift文件调用oc分类时崩溃解决办法(可能全网唯一)
背景 oc为基础创建的sdk混编工程,在被sdk关联的混编demo工程中swift文件调用时,会崩溃,提示找不到sdk中oc分类方法.常规的,在demo中设置-Objc和-all_load也还是会崩. ...
- Android开发如何准备技术面试(含Android面试押题)
今年毋庸置疑是找工作的寒冬,每一个出来找工作的同学都是值得尊敬的.现在找工作,虽然略难,但是反过来看也会逼迫我们成为更加优秀的自己. 但是不管是旺季还是寒冬,有一些优秀的同学找工作还是挺顺利的.所以说 ...
- Install Redmine Server with Bitnami Installer
Download bitnami installer: bitnami-redmine-2.4.1-1-linux-installer.run $ chmod 755 bitnami...instal ...
- 1002 A+B for Polynomials (25分) 格式错误
算法笔记上能踩的坑都踩了. #include<iostream> using namespace std; float a[1001];//至少1000个位置 int main(){ in ...
- JacksonUtils
package com.intfish.resourceserver.util; import com.fasterxml.jackson.annotation.JsonInclude; import ...
- 提取网页的markdown表格利器
在线Markdown表格转换器 markdown表格转换器,蛮好用的.偶然发现的开源工具,推荐一波. 这是目标链接:https://docs.locust.io/en/stable/configura ...
- 【XSS-labs】level 16-20
Level 16 查看源码:发现 空格 / 和script都被转义了. 用img标签的onerror事件(在加载外部文档或图片出错后触发),空格用%0a绕过, payload如下: <img% ...
- VsCode安装使用教程和插件安装方法
许多渗透方式都python写脚本,比较方便,写一下vscode和插件的安装办法,虽然不是很复杂,但是写一下做一下笔记: Visual Studio Code (简称 VS Code / VSC) 是一 ...
- Centos7上yum安装mongodb4-2
vim /etc/yum.repos.d/mongodb-org-4.2.repo [mongodb-org-4.2] name=MongoDB Repository baseurl=https:// ...