一、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

将给定数据源生成静态HTML页面持久化到项目之外的硬盘相关推荐

  1. 生成html页面的ftl文件,FreeMarker生成静态HTML页面的工具类FreeMarkerUtil

    FreeMarker生成静态HTML页面的工具类FreeMarkerUtil 一.FreeMarkerUtil工具类: import com.huaxia.entity.News; import co ...

  2. smarty能创建 php页面,smarty 生成静态html页面

    生成静态页面,用php生成会比smarty生成要快,但是如果你用smarty模板做的页面,现在要生成html的话,就可以看看这篇smarty 生成静态html页面教程物.<?php $tpl = ...

  3. 利用Java生成静态HMTL页面的方法收集

    利用Java生成静态HMTL页面的方法收集 生成静态页面技术解决方案之一 转载者前言:这是一个全面的jsp动态页面静态化方案,本站的帖子静态化方案将借鉴这篇帖子中方法.向[url]http://www ...

  4. php动态生成页面结构,PHP 动态生成静态HTML页面示例代码

    temp.html {penglig_site_title} test.php header('content-type:text/html; charset=utf-8');//防止生成的页面乱码 ...

  5. freeMarker生成静态html页面

    yml配置freemarker freemarker:request-context-attribute: req #req访问requestsuffix: .ftl #后缀名content-type ...

  6. 通过xml 生成html页面,使用xmldom在服务器端生成静态html页面

    服务器端的生成的页面数据,为了提高访问速度,往往需要生成静态的htm页面. 通常,可以使用fso生成静态的htm页. 但如果是fso被禁止或没有使用fso的权限,就需要其他的方法来解决. 利用xmld ...

  7. 记一次在nuxt中使用generate批量生成静态站点页面

    做过seo的同学都知道,一些不是实时变化的页面(比如一个星期更新一次内容)我们可以生成静态的站点去让爬虫去爬去,这样就无需一次次地调用接口了. 那么在nuxt中我们怎么去做呢? nuxt为我们提供了一 ...

  8. Freemaker生成静态html页面

    FreeMarker 是一个用Java编写的模板引擎,主要用来生成HTML Web页面,特别是基于MVC模式的应用程序.虽然FreeMarker具有一些编程的能力,但不像PHP,通常由Java程序准备 ...

  9. JSP生成静态HTML页面的实践方法和设计思路

    我们以两个大型网站为例作比较: 51job和智联招聘(先声明我不是为他们作广告,仅以他们为例作技术上的比较) 51job采用的是比较"先进"的php技术,而智联用的是比较落后的as ...

最新文章

  1. node随笔-数据流Stream
  2. Python 中reload一个文件时报错 ( reload() argument must be module)
  3. linux (阿里云 CentOS7) 中安装配置 RocketMQ
  4. IOS学习之多线程(3)--线程安全
  5. 【自动化__持续集成】___java___代码非空指针
  6. ajax无法访问,Ajax不能跨域访问的解决方案
  7. C语言取反(~)简单理解
  8. 奇迹按键精灵挂机脚本_奇迹挂机捡物按键精灵源码
  9. android soundpool 封装,Android 音频播放之SoundPool的使用和封装
  10. 越狱开发笔记(三)——非越狱App砸壳
  11. 矩阵求导法则,梯度求导方式
  12. Nucleus SE RTOS 中的中断
  13. U-Net在2022年相关研究的论文推荐
  14. 视频转GIF软件哪个好用 怎么将视频转为GIF
  15. 理想RISO ORPHIS EX7250 打印机驱动
  16. 印度软件巨头Infosys的成功之道
  17. HECATE G2耳机麦克风问题解决
  18. C++与C#相比,哪个更适合开发大型游戏?
  19. 加工奶制品的生产计划 lingo代码
  20. 电动汽车充电桩管理平台

热门文章

  1. luogu2770 航空路线问题 网络流
  2. 008. 限制上传文件的大小
  3. iOS 开发者账号共用发布证书 (Distribution)问题
  4. Ubuntu 12.10使用apt安装Oracle/Sun JDK
  5. 【VC6.0】getline需要输入2次回车才会结束的BUG修复方法
  6. hdu 2531 Catch him
  7. 使用VS2005进行负载测试
  8. 总结 构造函数与非构造函数 原型继承的一个方法
  9. Linux 下实现普通用户只能写入某个目录
  10. PostgreSQL Oracle 兼容性之 - INDEX SKIP SCAN (递归查询变态优化) 非驱动列索引扫描优化...