Skip to content

Instantly share code, notes, and snippets.

@qlong8807
Created December 4, 2018 03:13
Show Gist options
  • Save qlong8807/1412705d67d49c3e538ebc2080d613a2 to your computer and use it in GitHub Desktop.
Save qlong8807/1412705d67d49c3e538ebc2080d613a2 to your computer and use it in GitHub Desktop.
spring下载文件,解决中文文件名乱码问题
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();
@qlong8807
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment