我正在使用@RequestPart
注释上载一些参数和一个图像文件。
但是我遇到了错误
Content type 'image/jpeg' not supported for bodyType=org.springframework.web.multipart.MultipartFile
下面是我的代码段。如果我在触发HTTP POST请求时跳过了文件部分。一切正常。
仅在传递文件期间。我得到了错误。
@PostMapping(value = "document/uploadFile", consumes = {"multipart/form-data"})
public void uploadFile(@RequestPart(value = "name", required = true) String name,
@RequestPart(value = "fileType", required = true) String fileType,
@RequestPart(value = "file",required = false) MultipartFile file)
{
..logic to pick the data using POJO
}
application.yaml
## MULTIPART (MultipartProperties)
# Enable multipart uploads
spring:
servlet:
multipart:
enabled: true
# Threshold after which files are written to disk.
file-size-threshold : 2KB
# Max file size.
max-file-size: 10MB
# Max Request Size
max-request-size : 20MB
HTTP生成的代码
POST /document/uploadFile HTTP/1.1
Host: localhost:8026
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="name"
xyz
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="fileType"
jpeg
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="/C:/Users/XYZ/Pictures/Test.jpg"
Content-Type: image/jpeg
(data)
----WebKitFormBoundary7MA4YWxkTrZu0gW
POSTMAN的输入
邮递员错误
{
"timestamp": "2020-01-09T11:17:49.398+0000",
"path": "/document/uploadFile",
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'image/jpeg' not supported for bodyType=org.springframework.web.multipart.MultipartFile"
}
答案 0 :(得分:0)
我怀疑这是问题所在,但您最后在这里缺少括号 consums = {“ multipart / form-data”}
无论如何使用您当前的代码,它都可以正常工作,我进行了本地测试,因此您的问题可能出在如何执行请求上。
请确保将其添加为其余客户端中的RequestHeader: Content-Type:multipart / form-data ,或者如果您使用的是表单,则需要这样添加:< / p>
<form method="POST" action="/upload" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="name" name="name"/>
<input type="fileType" name="fileType"/>
<button type="submit">Submit</button>
</form>