/**
* 下载文件
* @param request
* @param noticeId
* @param model
* @return
*/
@RequestMapping(value=“download”)
public void download(HttpServletRequest request,HttpServletResponse response,
String noticeId, Model model) {
Notice notice = noticeService.getAttachOne(noticeId);
//下载文件路径
String path = notice.getAttachPath();
//下载文件名称
String filename=notice.getAttachName();
if(null!=path&&null!=filename){
try {
File file = new File(path + File.separator + filename);
if (!file.exists()) {
response.sendError(404, “File is not found!”);
return;
}
String downloadFielName = null;
String userAgent = request.getHeader("user-agent").toLowerCase();
if(userAgent.toLowerCase().contains("msie") || userAgent.toLowerCase().contains("like gecko")){
downloadFielName = URLEncoder.encode(filename, "UTF-8");
}else{
downloadFielName = new String(filename.getBytes("UTF-8"),"iso-8859-1");
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + downloadFielName);
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0){
out.write(buf, 0, len);
}
br.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}