Created
March 30, 2018 06:23
-
-
Save windhc/9c7b40508fb90b12fc41979888f5682e to your computer and use it in GitHub Desktop.
spring mvc + ajax无刷新下载文件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RequestMapping(value = "/download") | |
public ResponseEntity<byte[]> download(HttpServletRequest request, | |
@RequestParam("filename") String filename, | |
Model model) throws Exception { | |
//下载文件路径 | |
String path = request.getServletContext().getRealPath("/images/"); | |
File file = new File(path + File.separator + filename); | |
HttpHeaders headers = new HttpHeaders(); | |
//下载显示的文件名,解决中文名称乱码问题 | |
String downloadFielName = new String(filename.getBytes("UTF-8"), "iso-8859-1"); | |
//通知浏览器以attachment(下载方式)打开图片 | |
headers.setContentDispositionFormData("attachment", downloadFielName); | |
//application/octet-stream : 二进制流数据(最常见的文件下载)。 | |
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); | |
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment