没有Spring Boot版本2.3.1的多部分配置。

huangapple go评论59阅读模式
英文:

No Multipart Configuration for Spring boot version 2.3.1

问题

我正在创建一个应用程序,并希望将用户数据与他的个人资料图片一起发送。我在前端使用Reactjs,在后端使用Java 1.8和Spring Boot 2.3.1。我进行了为期3天的研究,无论我在哪里查找,都看到我们可以将用户数据和个人资料图片文件作为多部分表单数据从UI发送,然后在控制器中执行以下操作以获取这两个不同的数据。

public ResponseEntity<String> saveUser(@RequestParam("user") String stringUserRequest, @RequestParam("file") MultipartFile file)

这里的用户数据实际上是一个JSON,但会被转换为字符串并从UI发送。

但是这一直在抛出以下错误:

org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided

我已经搜索了很多,似乎与Servlet版本高于3.0有关。关于同样问题的问题有很多,但它们都是针对Spring Framework而不是特定的Spring Boot,并且我也尝试过它们,但似乎没有任何作用。

有人可以告诉我如何为Spring Boot创建此Multipart配置,或者如何降低Servlet版本吗?

英文:

I am creating an application and want to send user data along with his profile picture. I am using reactjs for frontend and java 1.8 with spring boot 2.3.1. I researched for 3 days and everywhere I see that we can send the user data and profile picture file as multipart form data from UI and then do the following in my controller to get the two different data.

public ResponseEntity&lt;String&gt; saveUser(@RequestParam(&quot;user&quot;) String stringUserRequest, @RequestParam(&quot;file&quot;) MultipartFile file)

Here the user data is actually a JSON but is converted to a string and sent from UI.

But this keeps throwing the below error

org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided

I have searched a lot and it has something to do with the servelet version being higher than 3.0. There are a lot of questions regarding the same but all of them are for spring framework and not for spring boot in particular and have tried them also but nothing seems to work.

Can anyone tell me either how to create this Multipart Config for spring boot or how to degrade the servelet version?

答案1

得分: 1

你需要在你的配置类中配置一个多部分解析器,首先在你的 POM 文件中添加这些依赖项:

<!-- 多部分解析器 -->
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.2.1</version>
</dependency>

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>

然后你需要声明这个 Bean 配置:

@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    return multipartResolver;
}

最后,在发送请求时不要忘记将内容类型指定为 multipart/form-data

编辑:
请求头中的 Content-Type 应为 multipart/form-data;boundary=--------------------------362947062764690924037801

英文:

You need to configure a multipart resolver in your configuration class, first you add these dependencies in your POM:

&lt;!-- multipart resolver--&gt;
&lt;dependency&gt;
    &lt;groupId&gt;commons-fileupload&lt;/groupId&gt;
    &lt;artifactId&gt;commons-fileupload&lt;/artifactId&gt;
    &lt;version&gt;1.2.1&lt;/version&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
    &lt;groupId&gt;commons-io&lt;/groupId&gt;
    &lt;artifactId&gt;commons-io&lt;/artifactId&gt;
    &lt;version&gt;2.5&lt;/version&gt;
&lt;/dependency&gt;

and you need to declare this Bean configuration:

@Bean(name = &quot;multipartResolver&quot;)
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    return multipartResolver;
}

Finally, don't forget to specify the content type as multipart/form-data when sending your request.

EDIT:
the header Content-type should be multipart/form-data;boundary=--------------------------362947062764690924037801

huangapple
  • 本文由 发表于 2020年9月8日 19:23:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63792865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定