方法体
@RequestMapping("/jobapply/o_resume_download.do")
public void resumeDownload(Integer id, HttpServletRequest request, HttpServletResponse response){
CmsUserResume resume = cmsUserResumeMng.findById(id);//获取对象数据
Map<String, Object> dataMap = new HashMap<String, Object>();//创建Map用于写入
dataMap.put("resume", resume);
if (StringUtils.isNotBlank(resume.getIcon()))
dataMap.put("resumeImg",resume.getIcon());
else {
dataMap.put("resumeImg", null);
}
String educationJson = resume.getEducationJson();
if(StringUtils.isNotBlank(educationJson)){
List<Education> educationList = JsonUtil.jsonStrToList(educationJson, Education.class);
if(educationList!=null && educationList.size()>0){
dataMap.put("educationList", educationList);
}
}
String workJson = resume.getWorkJson();
if(StringUtils.isNotBlank(workJson)){
List<Work> workList = JsonUtil.jsonStrToList(workJson, Work.class);
if(workList!=null && workList.size()>0){
dataMap.put("workList", workList);
}
}
String familyJson = resume.getFamilyJson();
if(StringUtils.isNotBlank(familyJson)){
List<Family> familyList = JsonUtil.jsonStrToList(workJson, Family.class);
if(familyList!=null && familyList.size()>0){
dataMap.put("familyList", familyList);
}
}
try{
File file = ExportDoc.createDoc(realPathResolver.get("/tpl_res/upload/") + request.getSession().getId()+".doc", dataMap);//写入文件
InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
String ctime = formatter.format(resume.getSignedDate());
String filename = resume.getName()+"-"+ctime+".doc";
filename = new String(filename.getBytes("gbk"), "iso8859-1");
response.setContentType("application/octet-stream");
response.addHeader("Content-Type", "application/msword; charset=utf-8");
response.addHeader("Content-Disposition", "attachment;filename=" + filename);
response.addHeader("Content-Length", String.valueOf(file.length()));
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
toClient.write(buffer);
toClient.flush();
toClient.close();
file.delete();
} catch (IOException ex) {
ex.printStackTrace();
}
}
写入类
package com.ezsite.doc;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import com.ezsite.cms.entity.main.CmsUserResume;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class ExportDoc {
//word文档的XML格式名称
private final static String MODEL_NAME = "docTemplate.xml";
// 2个参数分别是:模板的名称,导出文件的路径
public static File createDoc(String exportFilePath, Map<String, Object> dataMap) {
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
Template t = null;
// 1、导入模板
configuration.setClassForTemplateLoading(ExportDoc.class, "");
try {
// test.ftl为要装载的模板
t = configuration.getTemplate(MODEL_NAME);
} catch (IOException e) {
e.printStackTrace();
}
// 3、导出文件
// 输出文档路径及名称
File outFile = new File(exportFilePath);
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
try {
t.process(dataMap, out);
out.flush();
out.close();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return outFile;
}
public static void main(String[] args) {
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("resume",new CmsUserResume());
ExportDoc.createDoc("D:\\jbodoc.doc", dataMap);
}
}