1.创建html的java代码

package com.tydic.eshop.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Calendar; /**
* @ClassName: CreateHtmlUtils
* @Description: Java 根据模板创建 html
* @author
* @date 2016年4月22日 下午3:51:16
*/
public class CreateHtmlUtils { public static void main(String[] args) {
String filePath = "E:\\hh_web_space\\ecp\\web\\ecp_web_page\\src\\main\\webapp\\template\\template.html";
String imagePath ="http://localhost:8080/ecp/upload/1461293787628/1461293787628.jpg";
String disrPath = "E:\\hh_web_space\\ecp\\web\\ecp_web_page\\src\\main\\webapp\\template\\";
String fileName = "liuren";
MakeHtml(filePath,imagePath,disrPath,fileName);
}
/**
* @Title: MakeHtml
* @Description: 创建html
* @param filePath 设定模板文件
* @param imagePath 需要显示图片的路径
* @param disrPath 生成html的存放路径
* @param fileName 生成html名字
* @return void 返回类型
* @throws
*/
public static void MakeHtml(String filePath,String imagePath,String disrPath,String fileName ){
try {
String title = "<image src="+'"'+imagePath+'"'+"/>";
System.out.print(filePath);
String templateContent = "";
FileInputStream fileinputstream = new FileInputStream(filePath);// 读取模板文件
int lenght = fileinputstream.available();
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes);
fileinputstream.close();
templateContent = new String(bytes);
System.out.print(templateContent);
templateContent = templateContent.replaceAll("###title###", title);
System.out.print(templateContent); String fileame = fileName + ".html";
fileame = disrPath+"/" + fileame;// 生成的html文件保存路径。
FileOutputStream fileoutputstream = new FileOutputStream(fileame);// 建立文件输出流
System.out.print("文件输出路径:");
System.out.print(fileame);
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
} catch (Exception e) {
System.out.print(e.toString());
}
} }

2.然后是html的模板template.html,根据此模板生成的新的html文件

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>宣传活动</title>
<style>
body{ text-align:center;border: 0px;margin: 0px;background-color: #F4F4F4;}
.div{ margin:0 auto; width:1188px; height:auto;}
</style>
</head>
<body>
<div class="div">
<div>
###title###
</div>
</div>
</body>
</html>

3.java代码会常见新的html文件,并替换掉 ###title### 为图片的标签。

然后可以生成静态网页了。效果图如下:

一个简单的java生成html功能就实现了。

Java根据html模板创建 html文件的更多相关文章

  1. Java 操纵XML之创建XML文件

    Java 操纵XML之创建XML文件 一.JAVA DOM PARSER DOM interfaces The DOM defines several Java interfaces. Here ar ...

  2. Java根据Freemarker模板生成Word文件

    1.  准备模板 模板 + 数据 = 模型 1.将准备好的Word模板文件另存为.xml文件(PS:建议使用WPS来创建Word文件,不建议用Office) 2.将.xml文件重命名为.ftl文件 3 ...

  3. java Ftp上传创建多层文件的代码片段

    StringBuilder sBuilder = new StringBuilder();            String[] pah = path.split("/");   ...

  4. java根据word模板导出word文件

    1.word模板文件处理,如下图所示在word 文档中填值的地方写入占位变量 2.将word文档另存为xml文件.编辑如下图,找到填写的占位,修改为${bcrxm}格式 3.将文件后缀名改为.ftl文 ...

  5. java 根据html模板生成html文件

    1.代码部分 import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test. ...

  6. 关于Java里面File类创建txt文件重复???

    private JButton getOpenButton() { if (openButton == null) { openButton = new JButton(); openButton.s ...

  7. java通过FreeMarker模板生成Excel文件之.ftl模板制作

    关于怎么通过freemarker模板生成excel的文章很多,关键点在于怎么制作模板文件.ftl 网上的办法是: (1)把Excel模板的格式调好,另存为xml文件 (2)新建一个.ftl文件,把xm ...

  8. java创建TXT文件并进行读、写、修改操作

    import java.io.*; /**  *   * 功能描述:创建TXT文件并进行读.写.修改操作  *        * @author <a href="mailto:zha ...

  9. Java 在Word中创建邮件合并模板并合并文本和图片

    Word里面的邮件合并功能是一种可以快速批量操作同类型数据的方式,常见的如数据填充.打印等.其中必不可少的步骤包括用于填充的模板文档.填充的数据源以及实现邮件合并的功能.下面,通过Java程序展示如何 ...

随机推荐

  1. phpcms v9

    栏目列表 {pc:content action="category" catid="$catid" num="34" siteid=&quo ...

  2. php获取指定日期所在星期的开始时间与结束时间

    function getWeekRange($date){    $ret=array();    $timestamp=strtotime($date);    $w=strftime('%u',$ ...

  3. python学习小结4:类

    虽然Python是解释性语言,但是它是面向对象的,能够进行对象编程. 类和对象是面向对象编程的两个主要方面.类:创建一个新类型,而对象是这个类的实例,类使用class关键字创建.类的域和方法被列在一个 ...

  4. python学习小结3:函数

    Python是对接口编程,而不是对数据类型编程.例如我们定义了一个函数,在函数里用到了in这个接口,那么只要传入的参数实现了这个接口就可以,我们不在乎它是list还是tuple. 简单的函数 使用de ...

  5. Opencv图像与矩阵的操作

    #include "stdafx.h" #include <cv.h> #include <cxcore.h> #include <highgui.h ...

  6. 【转】jquery-取消冒泡

    转自:http://blog.163.com/css_mm/blog/static/209182176201262665157634/ 1.通过返回false来取消默认的行为并阻止事件起泡. jQue ...

  7. PVPGN 暗黑破坏神2 1.11b战网配置问题汇总

    写了第一篇配置指南之后,很多人向我咨询有关战网搭建的问题.于是觉得很有必要把若干常见的问题,和常用的进阶配置汇总一下,以方便更多人. 1.游戏版本和PVPGN与D2GS版本的问题. PVPGN建议选择 ...

  8. shell date

    .查看指定时间的时间戳 查看当前时间 #date +%s 查看指定时间 #date -d -- +%s #date -d +%s .将时间戳转换成date #date -d '1970-01-01 U ...

  9. UVA 10720 Graph Construction 贪心+优先队列

    题目链接: 题目 Graph Construction Time limit: 3.000 seconds 问题描述 Graph is a collection of edges E and vert ...

  10. 【转】Tarjan&LCA题集

    转自:http://blog.csdn.net/shahdza/article/details/7779356 [HDU][强连通]:1269 迷宫城堡 判断是否是一个强连通★2767Proving ...