Java 动态根据模板创建 html

作者官方网站: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;
    }
    
}

作者官方网站:http://www.wxl568.cn

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值