文件下载及文件名乱码问题的解决示例

文件下载时文件名乱码问题的解决示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@RequestMapping(value = "getpdfcredit")
public void getPDFCreditReport(@RequestParam("cookies") String cookies,
@RequestParam("imageCode") String imageCode,
HttpServletRequest request, HttpServletResponse response) throws IOException {
//
File file = new File("xxx.pdf");
//
String charset = request.getCharacterEncoding();
response.setHeader("Content-Disposition",
"attachement;filename="
+new String(file.getName().getBytes(charset), "ISO8859-1"));
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
OutputStream os = response.getOutputStream();
FileUtils.copyFile(file, os);
os.close();
}