在我的 Vue 应用程序中,我有这个 文件输入 :
<input type="file" multiple @input="handleFileInput" />
选定的文件被传递给我的这个上传方法: public async uploadFiles(file: File) {
[...]
const formData = new FormData();
formData.append('fileName', file.name);
formData.append('file', file);
[...]
await axios.post('my-upload-url', formData);
}
发送前的js日志:发送后的网络日志:
我的 Resteasy Controller 回答:
@POST
@Transactional
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadMultipartData(@MultipartForm @Valid MyDto dto) {
// dto.file -> my file
}
DTO:public class MyDto {
@NotNull
@FormParam("file")
@PartType(MediaType.APPLICATION_OCTET_STREAM)
public InputStream file;
}
转账时其他文件类型 (pdf, png, jpg, mp3, ...) 该文件按预期位于 DTO 的 InputStream 中。 但是,当文件是 .eml 文件时,InputStream 为空。 更新:将文件从 text.eml 重命名为 test.txt 时,上传有效。
为什么?
换句话说,如何在我的 REST Api 中使用内容类型为 message/rfc822 的文件?我需要一个单独的方法来接受这个特定的媒体类型吗?
最佳答案
所以这肯定与您的服务器如何处理上传的文件有关。 application/octet-stream
简单地表示一个通用/未知的二进制文件。来自 RFC 2046 § 4.5.1:
The "octet-stream" subtype is used to indicate that a body contains arbitrary binary data.
大多数应用程序会尝试通过查看其文件的扩展名来确定文件的 mimetype,但如果扩展名对他们来说是未知的,则很难确定它。一个
eml
file 只是一个文本文件,这解释了为什么将扩展名更改为 txt
工作没有问题。通过简单的 txt
来验证这一点的最简单方法文件并将其扩展名更改为 unknown
(例如)并尝试上传该文件。我设置了example复制完全相同的场景,在这种情况下,服务器读取文件内容没有问题。
关于javascript - 如何在 MultipartFormData 的 REST API 中使用内容类型为 message/rfc822 的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70025523/