ChenJiaHe
2021-05-06 8d87cf8d33815bdd6e7d0c968d105a09c5f5eac5
提交 | 用户 | age
5c5945 1 package com.hx.util;
E 2
3 import freemarker.template.Configuration;
4 import freemarker.template.DefaultObjectWrapper;
5 import freemarker.template.Template;
6 import freemarker.template.TemplateException;
7 import org.springframework.core.io.ClassPathResource;
8
9 import java.io.*;
10 import java.util.Date;
11 import java.util.HashMap;
12 import java.util.Map;
13
14 public class TempltUtil {  
15
16     public static final String PREVIEW_DOC = "/temp.docx";
17     public static final String TEMP_URL = "temp";
18     public static final String TEMP_NAME = "contract.tpl";
19
20     /**
21      *
22      * @param tempUrl 模板所在文件夹
23      * @param tempName 模板名称
24      * @return
25      * @throws IOException
26      */
27     public static Template configTemplate(String tempUrl,String tempName) throws IOException {
28         Configuration config = new Configuration();
29         ClassPathResource classPathResource = new ClassPathResource(tempUrl);
30         config.setDirectoryForTemplateLoading(classPathResource.getFile());
31         config.setObjectWrapper(new DefaultObjectWrapper());
32         Template template = config.getTemplate(tempName, "UTF-8");
33         return template;
34     }
35
36     /**
37      *
38      * @param tempUrl 模板所在文件夹
39      * @param temp 模板名称
40      * @param copyUrl 复制文件路径
41      * @param data 模板数据map
42      * return 生成路径
43      */
44     public static String toPreview(String tempUrl,String temp,String copyUrl, Map<?, ?> data){
45         try {
46         Template template = configTemplate(tempUrl,temp);
47         ClassPathResource classPathResource = new ClassPathResource(tempUrl+"/"+PREVIEW_DOC);
48         //生成新的模板
49         File tempFile =  classPathResource.getFile();
50         copyUrl = copyUrl+"/"+DateUtil.dateFormat(new Date(),"yyyyMMddHHmmss")+tempFile.getName();
51         File file = new File(copyUrl);
52         FileUtils.copyFile(tempFile,file);
53
54         File file2 = new File(copyUrl);
55
56         FileOutputStream fos = new FileOutputStream(file2);
57         Writer out = new OutputStreamWriter(fos, "UTF-8");
58             template.process(data, out);
59             out.flush();
60             out.close();
61         } catch (Exception e) {
62         e.printStackTrace();
63         }
64         return copyUrl;
65     }
66
67     /**
68      *
69      * @param templateUrl 模板路径目录,不包括模板名称,如 ./src/main/resources/jsonFile
70      * @param templateName 模板名称,如:data.tpl
71      * @param savaPath 生成文件存放目录,如 ./src/main/resources/jsonFile
72      * @param name 生成的文件名称,包括后缀名称
73      * @param data 模板里面内容
74      * @throws Exception
75      */
76     public static String createDataFile(String templateUrl,String templateName,
77                                       String savaPath, String name,Map<String, Object> data) throws IOException {
78         String targetFile = null;
79         Writer out = null;
80         if(!SimpleTool.checkNotNull(templateUrl)) {
81             throw new RuntimeException("templateUrl 为空");
82         }
83         if(!SimpleTool.checkNotNull(templateName)) {
84             throw new RuntimeException("templateName 为空");
85         }
86         if(!SimpleTool.checkNotNull(savaPath)) {
87             throw new RuntimeException("savaPath 为空");
88         }
89         try {
90             if(!savaPath.endsWith("/")){
91                 savaPath = savaPath+"/";
92             }
93             if(!templateUrl.endsWith("/")){
94                 templateUrl = templateUrl +"/";
95             }
96
97             //为了解决spingBoot打包成jar获取不了文件,把文件保存到本地
98             //拿到jar里面的文件流
99             ClassPathResource classPathResource = new ClassPathResource(templateUrl+templateName);
100             //保存模板图片路径
101             String saveTempUrl = savaPath+"temp/";
102             //判断是否存在了
103             File temFile = new File(saveTempUrl+templateName);
104             if(temFile.exists()){
105                 temFile = temFile.getParentFile();
106             }else{
107                 //不存在就创建一个
108                 temFile = FileUtils.inputStreamToFile(classPathResource.getInputStream(),saveTempUrl,templateName);
109                 temFile = temFile.getParentFile();
110             }
111
112             // 反射end
113             Configuration cfg = new Configuration();
114             // 指定模板文件从何处加载的数据源,这里设置成一个文件目录
115             cfg.setDirectoryForTemplateLoading(temFile);
116             cfg.setObjectWrapper(new DefaultObjectWrapper());
117             cfg.setDefaultEncoding("UTF-8");
118             // 获取或创建模板
119             Template template = cfg.getTemplate(templateName);
120             template.setEncoding("UTF-8");
121
122             // String targetFile = "./src/template/test.html";
123             //生成路径
124             targetFile = savaPath;
125             File file = new File(targetFile);
126             //没有找到就新建一个
127             if (!file.isDirectory() && !file.exists()) {
128                 file.mkdirs();
129             }
130             targetFile += name;
131             System.out.println("targetFile:"+targetFile);
132             // 将模板和数据模型合并 输出到Console
133             out = new BufferedWriter(new OutputStreamWriter(
134                     new FileOutputStream(targetFile), "UTF-8"));
135             template.process(data, out);
136         } catch (IOException e) {
137             e.printStackTrace();
138         } catch (TemplateException e) {
139             // TODO Auto-generated catch block
140             e.printStackTrace();
141         }finally {
142             if(SimpleTool.checkNotNull(out)){
143                 out.flush();
144                 out.close();
145             }
146         }
147         return targetFile;
148     }
149
150 }