实际上就是添加X-Content-Type-Options nosniff 规避浏览器嗅探,规避一些问题
参考代码
以下是一个部分参考代码
@GetMapping("/{filename:.+}")
public ResponseEntity<Resource> downloadFile(@PathVariable String filename) throws IOException {
Resource resource = fileService.loadFileAsResource(filename);
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"");
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
headers.add(HttpHeaders.X_CONTENT_TYPE_OPTIONS, "nosniff");
return ResponseEntity.ok()
.headers(headers)
.body(resource);
}
说明
当然不只是spring boot 其他下载的都可以参考
参考资料
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options
https://stackoverflow.com/questions/18337630/what-is-x-content-type-options-nosniff