英文:
Correct MIME Type for .msg file download using ByteArrayResource
问题
我有一些存储在AWS S3中的文件,并且我有一个获取该文件所有信息的Java对象DocumentResult的方法。
DocumentResult{
private byte[] result;
private fileName;
private contentType;
private String s3StorageId;
}
我编写了一个示例代码,用于从DocumentResult中检索byte[]
并返回一个新的ByteArrayResource
。
@GetMapping("/get/s3-file/{s3StorageId}")
public ResponseEntity<?> downloadDocumentFromS3(@PathParam String s3StorageId) {
//从S3获取文档
DocumentResult storageResult = s3Service.getDocument(s3StorageId);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(storageResult.getContentType()))
.header(Httpheaders.CONTENT_DISPOSITION, "document;filename=testFile")
.contentLength(storageResult.getResult().length)
.body(new ByteArrayResource(storageResult.getResult()));
}
该代码在storageResult.getContentType()
返回MIME类型application/vnd.ms-outlook
时运行正常,但下载的文档具有扩展名testFile.vnd.ms-outlook
。
我期望下载testFile.msg
,以便查看电子邮件内容。
英文:
I have a files stored in AWS S3 , and I do have a provision of getting hold of all the information of that file in one java object DocumentResult.
DocumentResult{
private byte[] result;
private fileName;
private contentType;
private String s3StorageId;
}
I have written a sample code to retrieve byte[]
from DocumentResult and return a new ByteArrayResource
.
@GetMapping("/get/s3-file/{s3StorageId}")
public ResponseEntity<?> downloadDocumentFromS3(@PathParam String s3StorageId) {
//Gets document from s3
DocumentResult storageResult = s3Service.getDocument(s3StorageId);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(storageResult.getContentType()))
.header(Httpheaders.CONTENT_DISPOSITION,"document;filename=testFile")
.contentLength(storageResult.getResult().length)
.body(new ByteArrayResource(storageResult.getResult()));
}
The code runs fine where storageResult.getContentType()
yields MIME type application/vnd.ms-outlook
but the document that gets downloaded has extension testFile.vnd.ms-outlook
.
I am expecting testFile.msg
to be downloaded so that I can look at the email contents.
答案1
得分: 1
你没有在设置的 Content-Disposition
标头中包含 .msg
扩展名,而且使用了错误的 document
,应该使用 attachment
:
@GetMapping
public ResponseEntity<byte[]> downloadDocumentFromS3() {
// 从S3获取文档
DocumentResult storageResult = s3Service.getDocument(s3StorageId);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/vnd.ms-outlook"))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"testFile.msg\"")
.contentLength(storageResult.getResult().length)
.body(storageResult.getResult());
}
英文:
You haven't included the .msg
extension in the Content-Disposition
header you're setting and use wrong document
, instead of attachment
:
@GetMapping
public ResponseEntity<byte[]> downloadDocumentFromS3() {
//Gets document from s3
DocumentResult storageResult = s3Service.getDocument(s3StorageId);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/vnd.ms-outlook"))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"testFile.msg\"")
.contentLength(storageResult.getResult().length)
.body(storageResult.getResult());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论