日期:2014-05-17 浏览次数:20698 次
@RequestMapping(value="/download")
public String downloadfile(@RequestParam("id") String id,HttpServletRequest request,HttpServletResponse response){
List<Reason> r=reasonService.find(id.trim());
if(r.size()!=1){
request.setAttribute("errors", "数据信息有误");
return "reason/error";
}
Reason reason=r.get(0);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
String filename=reason.getOldFileName();
String filepath=reason.getFile();
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+filename);
response.setHeader("Content-Length", String.valueOf(new File(reason.getFile()).length()));
bis = new BufferedInputStream(new FileInputStream(filepath));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[1024];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();
}catch (Exception e){
e.printStackTrace();
}finally{
}
request.setAttribute("errors", "下载成功");
return null;
}