Created
December 4, 2018 03:13
-
-
Save qlong8807/1412705d67d49c3e538ebc2080d613a2 to your computer and use it in GitHub Desktop.
spring下载文件,解决中文文件名乱码问题
This file contains hidden or 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
response.setCharacterEncoding(request.getCharacterEncoding()); | |
response.setContentType(MediaType.APPLICATION_OCTET_STREAM.toString()); | |
String realFileName = URLEncoder.encode(realFileName, StandardCharsets.UTF_8.toString()); | |
// 解决中文文件名乱码关键行 | |
response.setHeader("Content-Disposition", "attachment; filename=\"" + realFileName + "\"; filename*=utf-8''" + realFileName); | |
Path path = Paths.get(filePath+filename); | |
Files.copy(path, response.getOutputStream()); | |
response.flushBuffer(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
原因请参考https://blog.robotshell.org/2012/deal-with-http-header-encoding-for-file-download/