public synchronized static ResponseEntity<FileSystemResource> exportFile(File file, String tempFolderName, String fileName) {
if (file == null) {
return null;
}
try {
// 解决文件名中文乱码
fileName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition", "attachment; filename=" + fileName + EXCEL_TYPE);
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
headers.add("Last-Modified", new Date().toString());
headers.add("ETag", new Date().toString() + "_" + fileName);
return ResponseEntity
.ok()
.headers(headers)
.contentLength(file.length())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(new FileSystemResource(file));
}
文件名乱码:
例如:我 解析为: _ 解决方式: fileName = URLEncoder.encode(fileName,"UTF-8")
例如:我 解析为: %E6%88%91 解决方式: fileName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");