简体   繁体   English

Spring Boot 中的“bodyType=org.springframework.web.multipart.MultipartFile 不支持内容类型‘image/jpeg’”

[英]"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.

相关问题 Spring 不接受多部分文件列表:java.lang.NoSuchMethodException:org.springframework.web.multipart.MultipartFile - Spring not accepting list of multipart files: java.lang.NoSuchMethodException: org.springframework.web.multipart.MultipartFile 我如何将 java java.nio.ByteBuffer 转换为 spring org.springframework.web.multipart.MultipartFile? - How can i convert java java.nio.ByteBuffer to spring org.springframework.web.multipart.MultipartFile? org.springframework.web.multipart.MultipartFile 和 org.springframework.core.io.Resource 之间的转换 - Conversion between org.springframework.web.multipart.MultipartFile and org.springframework.core.io.Resource 无法将[java.lang.String]类型的值转换为属性所需的[org.springframework.web.multipart.MultipartFile]类型 - Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile] for property com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造`org.springframework.web.multipart.MultipartFile`的实例 - com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.web.multipart.MultipartFile` org.springframework.web.reactive.function.UnsupportedMediaTypeException:bodyType 不支持内容类型“text/xml;charset=UTF-8” - org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'text/xml;charset=UTF-8' not supported for bodyType org.springframework.web.reactive.function.UnsupportedMediaTypeException:内容类型'text/html;charset = iso-8859-1'不支持bodyType = - org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'text/html;charset=iso-8859-1' not supported for bodyType= 无法调用“org.springframework.web.multipart.MultipartFile.getOriginalFilename()”,因为“图像”是 null - Cannot invoke "org.springframework.web.multipart.MultipartFile.getOriginalFilename()" because "image" is null Java Spring boot 2.0.5 MultipartFile上传“内容类型不受支持” - Java Spring boot 2.0.5 MultipartFile upload “Content type not supported” Spring web 客户端返回异常“bodyType=[responseObject] 不支持内容类型‘application/json’ - Spring web client return exception "Content type 'application/json' not supported for bodyType=[responseObject]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM