com.luues.util.freemarker.FreemarkerUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-util Show documentation
Show all versions of commons-util Show documentation
A Simple Tool Operations Class
package com.luues.util.freemarker;
import com.luues.util.logs.LogUtil;
import com.luues.util.uuid.JUUID;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Map;
/**
* 生成freemarker模板类
*/
public class FreemarkerUtil {
private static Configuration configuration = null;
private final static String freemarker = "/freemarker/temp";
static {
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
}
/**
* 填充内容并获取ftl模板里面的内容
*
* @param file ftl文件上级目录
* @param dataMap
* @return
*/
public String getTemplateContent(HttpServletRequest request, File file, Map dataMap) {
Template template = null;
try {
configuration.setDirectoryForTemplateLoading(new File(file.getParent()));
} catch (IOException e2) {
e2.printStackTrace();
}
try {
template = configuration.getTemplate(file.getName());
} catch (IOException e) {
e.printStackTrace();
}
String outPath = request.getRealPath(freemarker);
File outFile = new File(outPath);
if (!outFile.exists()) {
outFile.mkdirs();
}
String outName = JUUID.getShortUUID() + ".doc";
outFile = new File(outPath, outName);
if (!outFile.exists()) {
try {
outFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Writer out = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(outFile);
OutputStreamWriter oWriter = null;
try {
oWriter = new OutputStreamWriter(fos, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
out = new BufferedWriter(oWriter);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
try {
template.process(dataMap, out);
} catch (TemplateException e) {
e.printStackTrace();
}
out.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
return outPath + "/" + outName;
}
/**
* 填充内容并获取ftl模板里面的内容
*
* @param file ftl文件上级目录
* @param dataMap
* @return
*/
public String getTemplateContent(String outPath, File file, Map dataMap) {
Template template = null;
try {
configuration.setDirectoryForTemplateLoading(new File(file.getParent()));
} catch (IOException e2) {
e2.printStackTrace();
}
try {
template = configuration.getTemplate(file.getName());
} catch (IOException e) {
e.printStackTrace();
}
File outFile = new File(outPath);
if (!outFile.exists()) {
outFile.mkdirs();
}
String outName = JUUID.getShortUUID() + ".doc";
outFile = new File(outPath, outName);
if (!outFile.exists()) {
try {
outFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Writer out = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(outFile);
OutputStreamWriter oWriter = null;
try {
oWriter = new OutputStreamWriter(fos, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
out = new BufferedWriter(oWriter);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
try {
template.process(dataMap, out);
} catch (TemplateException e) {
e.printStackTrace();
}
out.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
return outPath + "/" + outName;
}
/**
* 导出word表格
*
* @param file
* 模板上级目录
* @param ftlName
* 模板名称(必须,模板需放在ftl文件夹里面)
* @param dataMap
* 数据(必须)
* @param outName
* 输入文件名(可选)
* @return 文件路径+文件名
*/
public String createWord(File file, String ftlName, Map dataMap, String outName) {
Template t = null;
try {
configuration.setDirectoryForTemplateLoading(file);
} catch (IOException e) {
LogUtil.error(e.getMessage());
}
try {
t = configuration.getTemplate(ftlName); // 文件名
} catch (IOException e) {
LogUtil.error(e.getMessage());
}
String outPath = file.getPath() + "/" + freemarker;
File outFile = new File(outPath);
if (!outFile.exists()) {
outFile.mkdirs();
}
if (null == outName) {
outName = JUUID.generateUUID() + ".doc";
}
outFile = new File(outPath, outName);
if (!outFile.exists()) {
try {
outFile.createNewFile();
} catch (IOException e) {
LogUtil.error(e.getMessage());
}
}
Writer out = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(outFile);
OutputStreamWriter oWriter = null;
try {
oWriter = new OutputStreamWriter(fos, "UTF-8");
} catch (UnsupportedEncodingException e) {
LogUtil.error(e.getMessage());
}
out = new BufferedWriter(oWriter);
} catch (FileNotFoundException e1) {
LogUtil.error(e1.getMessage());
}
try {
t.process(dataMap, out);
out.close();
fos.close();
} catch (TemplateException e) {
LogUtil.error(e.getMessage());
} catch (IOException e) {
LogUtil.error(e.getMessage());
}
return outName;
}
}