正确的MIME类型用于使用ByteArrayResource下载.msg文件。

huangapple go评论59阅读模式
英文:

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(&quot;/get/s3-file/{s3StorageId}&quot;)
public ResponseEntity&lt;?&gt; 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,&quot;document;filename=testFile&quot;)
.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&lt;byte[]&gt; downloadDocumentFromS3() {

    //Gets document from s3
    DocumentResult storageResult = s3Service.getDocument(s3StorageId);

    return ResponseEntity.ok()
         .contentType(MediaType.parseMediaType(&quot;application/vnd.ms-outlook&quot;))
         .header(HttpHeaders.CONTENT_DISPOSITION, &quot;attachment;filename=\&quot;testFile.msg\&quot;&quot;)
         .contentLength(storageResult.getResult().length)
         .body(storageResult.getResult());
}

huangapple
  • 本文由 发表于 2023年6月26日 02:15:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551819.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定