Java根据html模板创建 html文件

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功能就实现了。

时间: 2024-08-12 16:26:52

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

.net 根据模板创建html文件

模板文件: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Co

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

StringBuilder sBuilder = new StringBuilder();            String[] pah = path.split("/");            // 先创建目录,在转到当前目录,再保存文件            // 分层创建目录            for (String pa : pah)            {                if (StringUtils.isNotBlank(pa))         

java.io,PrintWriter可以用来创建一个文件并向本文文件写入数据

PrintWriter(String filename); PrintWriter(File file);<!--创建一个向指定文件的新的PrintWriter--> PrintWriter(OutoutStream out); PrintWriter(OutoutStream out,Boolean antoflush);<!--创建一个中介输出流,创建一个向此输出流写入数据的新的PrintWriter--> PrintWriter(Writer out); PrintWrite

java 根据实体类创建映射文件

import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.lang.reflect.Field; import java.util.List; import java.util.Set; import com.mohe.model.entity.Teach; /** * 根据实体类生成映射文件 * * @author dxm * */ public class Obj

jxl创建Excel文件java代码示例

记得要下载 并 导入 jxl.jar 包,免积分下载地址:http://download.csdn.net/detail/u010011052/7561041 package Test; import java.io.*; import jxl.*; import jxl.format.Colour; import jxl.write.*; public class JXLTest { private static WritableWorkbook book; private static Wr

如何创建JAR文件?如何运行.jar形式的Java程序?

一.如何创建JAR文件? .jar是用来压缩档案或者解压档案的文件格式,其特点是具有无损压缩的功能.想知道如何创建这种程序?请访问 http://www.cnblogs.com/yjmyzz/p/executable-jar.html .jar可以通过整理Java程序的类别文件来变为可执行文件,而编译器或Java虚拟机可以识别这些格式..jar文件是通过javaw(Java应用程序管理器)执行的.你需要设置.jar文件中的应用程序接入点(即含有程序主功能的文件类别).你需要通过清单文件(Mani

java根据模板生成word文件

原文:java根据模板生成word文件 源代码下载地址:http://www.zuidaima.com/share/1550463664884736.htm AVA生成word模板程序步骤 1. 将freemarker-2.3.13.jar复制到项目\WEB-INF\lib目录下 2. 编辑模板文件 (1) 将DOC文件另存为xml文件,将xml文件在eclipse环境下打开,右键选"源"→"格式".此处注意xml文件属性是UTF-8. (2) 将xml文件中需要替

java file 操作之创建、删除文件及文件夹

本文章向大家讲解java文件的基本操作,包括java创建文件和文件夹.java删除文件.java获取指定目录的全部文件.java判断指定路径是否为目录以及java搜索指定目录的全部内容等.请看下面实例. 创建文件File 的两个常量(File.separator.File.pathSeparator). 直接在windows下使用\进行分割是可以的.但是在linux下就不是\了.所以,要想使得我们的代码跨平台,更加健壮,所以,大家都采用这两个常量吧. public static void cre

java+selenium+new——创建一个文件,判断文件的目录知否存在,以及创建目录

package rjcs; import java.io.File; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebElement; import org.openqa.seleniu