@RequestMapping(value = "/download")
public ResponseEntity<byte[]> download(HttpServletRequest request) throws Exception {
log.info("download start.");
InputStream is=null;
String fileName = request.getParameter("fileName");
try{
File file = new File(SysEnv.LocalFilePath+"/"+fileName);
byte[] body = null;
is = new FileInputStream(file);
body = new byte[is.available()];
is.read(body);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attchement;filename=" + file.getName());
HttpStatus statusCode = HttpStatus.OK;
ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
log.info("download stop.");
return entity;
}catch(Exception e){
log.error("System Error.", e);
throw new RuntimeException(e);
}finally{
if(is!=null){
is.close();
}
}
}
SpringMVC文件下载
最新推荐文章于 2024-07-09 20:20:12 发布