@RequestMapping("/fileDownLoad2")
public ResponseEntity fileDemo(){
try {
String fileName = "D:/yuSys/02 运行官方例子1575947491341.pptx";
FileSystemResource resource = new FileSystemResource(fileName);
String mediaTypeStr = URLConnection.getFileNameMap().getContentTypeFor(fileName);
mediaTypeStr = (mediaTypeStr == null) ? MediaType.APPLICATION_OCTET_STREAM_VALUE : mediaTypeStr;
MediaType mediaType = MediaType.parseMediaType(mediaTypeStr);
HttpHeaders headers = new HttpHeaders();
String filenames = new String("123.ppt".getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
headers.setContentDispositionFormData("attachment", filenames);
headers.setContentType(mediaType);
log.info("sucess");
return ResponseEntity.ok()
.headers(headers)
.contentLength(resource.getInputStream().available())
.body(resource);
} catch (IOException e) {
log.error("文件读写错误", e);
return null;
}
}