博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
将给定数据源生成静态HTML页面持久化到项目之外的硬盘
阅读量:4306 次
发布时间:2019-06-06

本文共 2861 字,大约阅读时间需要 9 分钟。

一、java代码

设置好数据源map

Map
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
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
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文件:

    
###knowledgeName###
###knowledgeName###
###publishDate###
###htmlContent###
字体:【
 
 
【打印 】
【关闭】

效果:

 

转载于:https://www.cnblogs.com/wmqiang/p/11603143.html

你可能感兴趣的文章
Maven:mirror和repository 区别
查看>>
微服务网关 Spring Cloud Gateway
查看>>
SpringCloud Feign的使用方式(一)
查看>>
SpringCloud Feign的使用方式(二)
查看>>
关于Vue-cli+ElementUI项目 打包时排除Vue和ElementUI
查看>>
Vue 路由懒加载根据根路由合并chunk块
查看>>
vue中 不更新视图 四种解决方法
查看>>
MySQL 查看执行计划
查看>>
OpenGL ES 3.0(四)图元、VBO、VAO
查看>>
OpenGL ES 3.0(五)纹理
查看>>
OpenGL ES 3.0(八)实现带水印的相机预览功能
查看>>
OpenGL ES 3.0(九)实现美颜相机功能
查看>>
FFmpeg 的介绍与使用
查看>>
Android 虚拟机简单介绍——ART、Dalvik、启动流程分析
查看>>
原理性地理解 Java 泛型中的 extends、super 及 Kotlin 的协变、逆变
查看>>
FFmpeg 是如何实现多态的?
查看>>
FFmpeg 源码分析 - avcodec_send_packet 和 avcodec_receive_frame
查看>>
FFmpeg 新旧版本编码 API 的区别
查看>>
RecyclerView 源码深入解析——绘制流程、缓存机制、动画等
查看>>
Android 面试题整理总结(一)Java 基础
查看>>