作者官方网站:http://www.wxl568.cn
Java 动态根据模板创建 html
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName: CreateHtmlUtils
* @Description: Java 根据模板创建 html
* @author
* @date 2016年4月22日 下午3:51:16
*/
public class CreateHtmlUtils {
private CreateHtmlUtils(){
}
public static void main(String[] args) {
Map<String,String> info=new HashMap<>();
info.put("filePath", "b");
CreateHtmlUtils.makeHtml(info);
}
/**
* @Title: MakeHtml
* @Description: 创建html
* @param filePath 设定模板文件
* @param imagePath 需要显示图片的路径
* @param disrPath 生成html的存放路径
* @param fileName 生成html名字
* @param templateNr模板内容
* @return void 返回类型
* @throws
*/
public static Map<String,String> makeHtml(Map<String,String> info){
Map<String,String> resmap=new HashMap<>();
try {
String templateContent =info.get("filePath");
FileInputStream fileinputstream = new FileInputStream(templateContent);// 读取模板文件
int lenght = fileinputstream.available();
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes);
fileinputstream.close();
templateContent = new String(bytes);
templateContent = templateContent.replaceAll("####wxltTypeNameWxl####", info.get("tTypeName"));
templateContent = templateContent.replaceAll("@@@@wxltTitleWxl@@@@", info.get("tTitle"));
templateContent = templateContent.replaceAll("####wxlContentWxl####", info.get("tContent"));
templateContent = templateContent.replaceAll("&&&&wxlcreateDateWxl&&&&", info.get("createDate"));
String fileame ="";
String tempfileame="";
String fileName=info.get("tUrl");
if(null!=fileName && !fileName.equals("")){//为空
fileame = fileName;
resmap.put("fileName",fileame);
File file = new File(info.get("disrPath")+ fileame);
file.delete();
fileame =info.get("disrPath")+ fileame;// 生成的html文件保存路径。
}else{//按年月月日生成
fileame = info.get("tType")+info.get("tId")+DateFormatUtil.getFormatNyd(new Date()) + ".html";
tempfileame=fileame;
fileame =info.get("disrPath")+ info.get("configUrl")+"/" + fileame;// 生成的html文件保存路径。
File targetFile = new File(info.get("disrPath")+ info.get("configUrl"));
if (!targetFile.exists()) {
targetFile.mkdirs();
}
resmap.put("fileName", info.get("configUrl")+"/" + tempfileame);
}
FileOutputStream fileoutputstream = new FileOutputStream(fileame);// 建立文件输出流
byte tagbytes[] = templateContent.getBytes();
fileoutputstream.write(tagbytes);
fileoutputstream.close();
} catch (Exception e) {
e.toString();
}
return resmap;
}
}