package com.iailab.module.model.mpk.common.utils; import cn.hutool.core.io.FileUtil; import com.iailab.module.model.mpk.common.MdkConstant; import com.iailab.module.model.mpk.dto.MpkFileDTO; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.io.StringWriter; import java.nio.file.Files; import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @Slf4j public class GenUtils { /** * 渲染模板 **/ public static StringWriter drawTemplate(String template,Map map) { //模板数据 VelocityContext context = new VelocityContext(map); return drawTemplate(template,context); } public static StringWriter drawTemplate(String template,VelocityContext context) { Properties prop = new Properties(); prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); Velocity.init(prop); StringWriter writer = new StringWriter(); Template tpl = Velocity.getTemplate("template/" + template, "UTF-8"); tpl.merge(context, writer); return writer; } public static void drawTemplate(String template,Map map,File toFile) { VelocityContext context = new VelocityContext(map); drawTemplate(template,context,toFile); } public static void drawTemplate(String template,VelocityContext context,File toFile) { try { StringWriter writer = drawTemplate(template,context); FileUtil.mkParentDirs(toFile); if (!toFile.exists()) { toFile.createNewFile(); } FileUtil.writeUtf8String(writer.toString(),toFile); IOUtils.closeQuietly(writer); } catch (IOException e) { throw new RuntimeException("模板渲染异常"); } } }