今天看项目中别人用 response 写的个下载功能,复制一下当中的代码
public String execute() throws Exception {
ApArticleAttachEntity file = baseDataService.getArticleAttach(attachId);
if (file == null)
return NONE;
String fileName = (file.getAttachName() == null
|| "".equals(file.getAttachName())) ? "unknownFileName.tmp" : file
.getAttachName();
String suffixName = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());
if("doc".equalsIgnoreCase(suffixName)){
response.setContentType("application/msword");
}else if("xls".equalsIgnoreCase(suffixName)){
response.setContentType("application/vnd.ms-excel");
}else if("pdf".equalsIgnoreCase(suffixName)){
response.setContentType("application/pdf");
}else{
response.setContentType("application/download");
}
response.setHeader("Content-Disposition", "attachment; filename=\""
+ new String(fileName.getBytes("GBK"), "iso8859-1") + "\"");
OutputStream os = response.getOutputStream();
os.write(file.getAttachContent());
os.flush();
os.close();
return NONE;
}