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