Spring 文件下载,出现中文乱码问题
@GetMapping("/file/{id}")
public ResponseEntity<InputStreamResource> downloadFile(@PathVariable int id)
throws IOException {
FileMe fileMe = fileMapper.getById(id);
String[] fd = fileMe.fileName.split("\\$");
String filePath = "course/"+fd[0]+"/" + fd[1];
System.out.println(filePath);
FileSystemResource file = new FileSystemResource(filePath);
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(file.getFilename(), "utf-8"));
// headers.add("Content-Disposition", String.format("attachment;filename=\"%s\"",
// new String(file.getFilename()).getBytes("UTF-8"),"iso-8859-1"));
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
return ResponseEntity
.ok()
.headers(headers)
.contentLength(file.contentLength())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(new InputStreamResource(file.getInputStream()));
}