parent
e912268968
commit
bcb05530e6
@ -0,0 +1,87 @@
|
|||||||
|
package com.example.frame.utils;
|
||||||
|
|
||||||
|
import com.example.frame.auth.LocalData;
|
||||||
|
import freemarker.cache.ClassTemplateLoader;
|
||||||
|
import freemarker.cache.MultiTemplateLoader;
|
||||||
|
import freemarker.cache.TemplateLoader;
|
||||||
|
import freemarker.template.Configuration;
|
||||||
|
import freemarker.template.Template;
|
||||||
|
import freemarker.template.TemplateException;
|
||||||
|
import freemarker.template.TemplateExceptionHandler;
|
||||||
|
import org.springframework.util.ClassUtils;
|
||||||
|
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||||
|
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.io.Writer;
|
||||||
|
|
||||||
|
public class FreeMarkerUtil {
|
||||||
|
public static Configuration getDefaultConfiguration() {
|
||||||
|
return LocalData.getBean(Configuration.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static FreeMarkerConfigurer getDefaultConfigurer() {
|
||||||
|
return LocalData.getBean(FreeMarkerConfigurer.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static FreeMarkerConfigurer getNewConfigurer(String basePackagePath) {
|
||||||
|
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
|
||||||
|
// 初始化一些配置
|
||||||
|
Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
|
||||||
|
cfg.setDefaultEncoding("UTF-8");
|
||||||
|
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
|
||||||
|
cfg.setNumberFormat("0.##");
|
||||||
|
cfg.setDateFormat("yyyy-MM-dd");
|
||||||
|
cfg.setDateTimeFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
|
||||||
|
MultiTemplateLoader multiTemplateLoader = new MultiTemplateLoader(new TemplateLoader[]{
|
||||||
|
new ClassTemplateLoader(ClassUtils.getDefaultClassLoader(), basePackagePath),
|
||||||
|
});
|
||||||
|
|
||||||
|
cfg.setTemplateLoader(multiTemplateLoader);
|
||||||
|
freeMarkerConfigurer.setConfiguration(cfg);
|
||||||
|
return freeMarkerConfigurer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Configuration getNewConfiguration(FreeMarkerConfigurer configurer) {
|
||||||
|
return configurer.getConfiguration();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void outFile(String templatePath, String outPath, Object model) {
|
||||||
|
Configuration defaultConfiguration = getDefaultConfiguration();
|
||||||
|
try {
|
||||||
|
// 获取模板
|
||||||
|
Template template = defaultConfiguration.getTemplate(templatePath);
|
||||||
|
// 输出文件路径
|
||||||
|
Writer wr = new OutputStreamWriter(new FileOutputStream(outPath), "UTF-8");
|
||||||
|
// 写入
|
||||||
|
template.process(model, wr);
|
||||||
|
// 关闭流
|
||||||
|
wr.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (TemplateException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void outOutput(String templatePath, OutputStream outputStream, Object model) {
|
||||||
|
Configuration defaultConfiguration = getDefaultConfiguration();
|
||||||
|
try {
|
||||||
|
// 获取模板
|
||||||
|
Template template = defaultConfiguration.getTemplate(templatePath);
|
||||||
|
// 输出文件路径
|
||||||
|
Writer wr = new OutputStreamWriter(outputStream, "UTF-8");
|
||||||
|
// 写入
|
||||||
|
template.process(model, wr);
|
||||||
|
// 关闭流
|
||||||
|
wr.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (TemplateException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue