I am using @RequestPart
annotation to upload some parameters and an image file.
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. 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
## 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
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"}
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:
<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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.