AttachedFile attachedFile = attachedFileService.selectById(fileId);
String filePath = pathHead + attachedFile.getFilePath();
String fileName = attachedFile.getFileName() + "." + attachedFile.getExtension();
FileSystemResource file = new FileSystemResource(filePath);
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", fileName));
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()));
//HttpHeaders headers = new HttpHeaders();
////下载显示的文件名,解决中文名称乱码问题
//String downloadFileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1");
////通知浏览器以attachment
//headers.setContentDispositionFormData("attachment", downloadFileName);
////application/octet-stream : 二进制流数据(最常见的文件下载)。
//headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//return new ResponseEntity<>(FileUtils.readFileToByteArray(file),
// headers, HttpStatus.CREATED);
我是按照网上正常给的教程写的,但是就是文件损坏,(好像是数据流就不对)
![图片说明](http://i![图片说明](https://img-ask.csdn.net/upload/201711/05/1509890721_393189.png)mg.ask.csdn.net/upload/201711/05/1509890701_819933.png)