简体   繁体   English

Spring MVC文件上传返回-415不支持的媒体类型

[英]Spring MVC File upload return - 415 Unsupported Media Type

I'm writing a web service to upload a image/video file to the server. 我正在编写一个Web服务,以将图像/视频文件上传到服务器。 Every time I call my upload web service from postman, it return 415 Unsupported Media Type . 每次我从邮递员致电上传网络服务时,都会返回415 Unsupported Media Type Here is what I have done. 这是我所做的。

In my controller: 在我的控制器中:

@POST
@Path("/upload")
@Produces(MediaType.MULTIPART_FORM_DATA)
public Response upload(@RequestParam("file") MultipartFile files) {
    Response.ResponseBuilder rb;

    rb = Response.status(Response.Status.OK);
    rb.entity(new ResponseWrapper<String>(SUCCESS, ""));
    return rb.build();
}

application context: 应用程序上下文:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="10000000"/>
</bean>

I have already added commons-fileupload-1.2.2.jar & mimepull.jar 我已经添加了commons-fileupload-1.2.2.jarmimepull.jar

When I call the web service I get the following console log. 当我调用Web服务时,会收到以下控制台日志。

    com.sun.jersey.spi.container.ContainerRequest getEntity
SEVERE: A message body reader for Java class org.springframework.web.multipart.MultipartFile, and Java type interface org.springframework.web.multipart.MultipartFile, and MIME media type multipart/form-data; boundary=--------------------------410903058421289672087091 was not found.
The registered message body readers compatible with the MIME media type are:
multipart/* ->
  com.sun.jersey.multipart.impl.MultiPartReaderServerSide
*/* ->
  com.sun.jersey.core.impl.provider.entity.FormProvider
  com.sun.jersey.core.impl.provider.entity.StringProvider
  com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
  com.sun.jersey.core.impl.provider.entity.FileProvider
  com.sun.jersey.core.impl.provider.entity.InputStreamProvider
  com.sun.jersey.core.impl.provider.entity.DataSourceProvider
  com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
  com.sun.jersey.core.impl.provider.entity.ReaderProvider
  com.sun.jersey.core.impl.provider.entity.DocumentProvider
  com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
  com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
  com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
  com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
  com.sun.jersey.core.impl.provider.entity.EntityHolderReader
  com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy

I've tried many things digging the internet, still dosnt'e work. 我已经尝试了很多挖掘互联网的方法,但仍然不起作用。 Help is appreciated. 感谢帮助。

try setting the content type using the following format 尝试使用以下格式设置内容类型

rb.type(MediaType.TEXT_PLAIN)

change the media type accordingly to your needs. 根据需要更改媒体类型。 text_plain as provided in the example above should be setting text/plain 上面示例中提供的text_plain应该设置text/plain

There are two changes in suggest: 建议有两个更改:

  1. Change @produces to @consumes as this api is accepting the file which is multipart/form-data . 由于此api接受文件multipart/form-data @produces@consumes更改为@consumes
  2. Change @RequestParam to @RequestPart 将@RequestParam更改为@RequestPart

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM