提交 | 用户 | 时间
|
449017
|
1 |
package com.iailab.module.model.mpk.common.utils; |
D |
2 |
|
|
3 |
import cn.hutool.core.io.FileUtil; |
|
4 |
import com.iailab.module.model.mpk.common.MdkConstant; |
|
5 |
import com.iailab.module.model.mpk.dto.MpkFileDTO; |
|
6 |
import lombok.extern.slf4j.Slf4j; |
|
7 |
import org.apache.commons.io.FileUtils; |
|
8 |
import org.apache.commons.io.IOUtils; |
|
9 |
import org.apache.velocity.Template; |
|
10 |
import org.apache.velocity.VelocityContext; |
|
11 |
import org.apache.velocity.app.Velocity; |
|
12 |
|
|
13 |
import java.io.File; |
|
14 |
import java.io.IOException; |
|
15 |
import java.io.OutputStream; |
|
16 |
import java.io.StringWriter; |
|
17 |
import java.nio.file.Files; |
|
18 |
import java.util.*; |
|
19 |
import java.util.zip.ZipEntry; |
|
20 |
import java.util.zip.ZipOutputStream; |
|
21 |
|
|
22 |
@Slf4j |
|
23 |
public class GenUtils { |
|
24 |
/** |
|
25 |
* 渲染模板 |
|
26 |
**/ |
|
27 |
public static StringWriter drawTemplate(String template,Map<String, Object> map) { |
|
28 |
//模板数据 |
|
29 |
VelocityContext context = new VelocityContext(map); |
|
30 |
return drawTemplate(template,context); |
|
31 |
} |
|
32 |
|
|
33 |
public static StringWriter drawTemplate(String template,VelocityContext context) { |
|
34 |
Properties prop = new Properties(); |
|
35 |
prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); |
|
36 |
Velocity.init(prop); |
|
37 |
|
|
38 |
StringWriter writer = new StringWriter(); |
|
39 |
Template tpl = Velocity.getTemplate("template/" + template, "UTF-8"); |
|
40 |
tpl.merge(context, writer); |
|
41 |
return writer; |
|
42 |
} |
|
43 |
|
1fea3e
|
44 |
public static void drawTemplate(String template,Map<String, Object> map,File toFile) { |
449017
|
45 |
VelocityContext context = new VelocityContext(map); |
D |
46 |
drawTemplate(template,context,toFile); |
|
47 |
} |
|
48 |
|
1fea3e
|
49 |
public static void drawTemplate(String template,VelocityContext context,File toFile) { |
D |
50 |
try { |
|
51 |
StringWriter writer = drawTemplate(template,context); |
449017
|
52 |
|
1fea3e
|
53 |
FileUtil.mkParentDirs(toFile); |
449017
|
54 |
|
1fea3e
|
55 |
if (!toFile.exists()) { |
D |
56 |
toFile.createNewFile(); |
|
57 |
} |
|
58 |
|
|
59 |
FileUtil.writeUtf8String(writer.toString(),toFile); |
|
60 |
IOUtils.closeQuietly(writer); |
|
61 |
} catch (IOException e) { |
912a1e
|
62 |
throw new RuntimeException("模板渲染异常",e); |
449017
|
63 |
} |
D |
64 |
} |
|
65 |
} |