【发布时间】:2020-01-02 19:39:21
【问题描述】:
使用 spring boot 下载时 Excel 下载不工作 - 问题可能是设置 CONTENT TYPE ,我怀疑! 我尝试设置不同的 CONTENT TYPE 标题,但不能使用 EXCEL。
我的要求:下面的代码应该对所有类型的文件都是通用的。
@GetMapping(value = "/files/{id}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<ByteArrayResource> downloadAnyFile()
{
byte[] byteArray; // data comes from external service call in byte[]
ByteArrayResource resource = new ByteArrayResource(byteArray, docName);
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, ATTACHMENT + docName)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.contentLength(resource.contentLength())
.body(resource);
// Also tried this below content-type but not working
String mimeType = ServletContext.getMimeType(fileName);
.contentType(MediaType.parseMediaType(mimeType))
}
I am seeing warning of file corrupted while opening , The downloaded excel 文件不应损坏。它应该像上传前一样打开。
【问题讨论】:
-
@Babasheb: 谢谢你的回复,# 我已经试过了,但是不行。 #
-
你已经开始工作了吗?我也面临同样的问题。
标签: java spring rest spring-boot