[英]"Content type 'image/jpeg' not supported for bodyType=org.springframework.web.multipart.MultipartFile" in Spring Boot
I am using @RequestPart
annotation to upload some parameters and an image file.我正在使用
@RequestPart
批注上传一些参数和图像文件。
But I am getting below error但我收到以下错误
Content type 'image/jpeg' not supported for bodyType=org.springframework.web.multipart.MultipartFile
Below is my code snippet.下面是我的代码片段。 If I skip the file part while firing the HTTP POST Request.
如果我在触发 HTTP POST 请求时跳过文件部分。 It's working fine.
它工作正常。
Only during the passing file.仅在传递文件期间。 I am getting the error.
我收到错误。
@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应用程序.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 Generated Code 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
Input from POSTMAN来自邮递员的输入
Error in 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"
}
I doubt that is the problem but you're missing a parenthesis here at the end consumes = {"multipart/form-data"}我怀疑这是问题所在,但您最后在这里缺少一个括号消耗 = {"multipart/form-data"}
In any case with your current code it should work flawlessly, I made a local test so probably your problems lies in how you perform the request.在任何情况下,您当前的代码都应该可以完美运行,我进行了本地测试,因此您的问题可能在于您如何执行请求。
Be sure to add this as a RequestHeader in your rest client: Content-Type: multipart/form-data , or in case you are using a form you need to add it like so:请务必在您的其余客户端中将其添加为 RequestHeader: Content-Type: multipart/form-data ,或者如果您使用的是表单,则需要像这样添加它:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.