1、后端代码:
public class DownloadActor extends AbstractSupportActor {
private Configuration configuration;
public LeaveQuestionDownloadActor() {
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
// FTL文件所存在的位置
configuration.setClassForTemplateLoading(this.getClass(), "");
}
public void Execute(Message message) throws Exception {
// 查询下载文件所需数据
String filename = 文件名 + ".doc";
// 查询文件存储路径
String downpath = this.getSqlSession().selectOne("XXX.XXX");
// 判断该文件时候已经存在
File outFile = new File(downpath + "/" + filename);
if (outFile.exists()) {
outFile.delete();
}
// 判断路径是否存在
folder = new File(downpath);
// 如果不存在,创建目录
if (!folder.exists()) {
folder.mkdirs();
}
try {
Template t = configuration.getTemplate("Questionnaire.ftl");
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
t.process(map, writer);
writer.close();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}