[英]Spring not accepting list of multipart files: java.lang.NoSuchMethodException: org.springframework.web.multipart.MultipartFile
[英]"Content type 'image/jpeg' not supported for bodyType=org.springframework.web.multipart.MultipartFile" in Spring Boot
我正在使用@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
}
應用程序.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
來自郵遞員的輸入
郵遞員錯誤
{
"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"
}
我懷疑這是問題所在,但您最后在這里缺少一個括號消耗 = {"multipart/form-data"}
在任何情況下,您當前的代碼都應該可以完美運行,我進行了本地測試,因此您的問題可能在於您如何執行請求。
請務必在您的其余客戶端中將其添加為 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.