Spring MVC之ResposeEntity下載文件


Spring Mvc中用ResponseEntity方式下載文件如下:

@RequestMapping("/download")
public ResponseEntity<byte[]> download(HttpServletRequest request,@RequestParam("fileName") String fileName) throws IOException {
String path = request.getServletContext().getRealPath("/uploadImages/");
File dwFile = new File(path+File.separator+fileName);
HttpHeaders headers = new HttpHeaders();
//下載顯示的中文名,解決中文名稱亂碼問題
String downloadFileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1");
//application/octet-stream:二進制流數據
headers.setContentDispositionFormData("attachment",downloadFileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(dwFile),headers, HttpStatus.CREATED);
}
}

  


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com