java 如何在 Spring Boot 中使用 CommonsMultipartResolver

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30768242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:37:41  来源:igfitidea点击:

How to use CommonsMultipartResolver in Spring Boot

javaspringspring-bootapache-commons

提问by Carlos Alberto

I have tried to use CommonsMultipartResolver in Boot translating my old application (WAR) to Boot, and right now it got the following code:

我尝试在 Boot 中使用 CommonsMultipartResolver 将我的旧应用程序 (WAR) 转换为 Boot,现在它得到了以下代码:

@Configuration
    public class TestConfig {

        @Bean
        public FilterRegistrationBean openEntityManagerFilterRegistrationBean() {
            // Set upload filter
            final MultipartFilter multipartFilter = new MultipartFilter();
            final FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(multipartFilter);
            filterRegistrationBean.addInitParameter("multipartResolverBeanName", "commonsMultipartResolver");

            return filterRegistrationBean;
        }

        @Bean
        public CommonsMultipartResolver commonsMultipartResolver() {
            final CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
            commonsMultipartResolver.setMaxUploadSize(-1);

            return commonsMultipartResolver;
        }
    }

Is this the right way in Boot, cause a I saw some properties to be applied in application.properties. Would they be the same purpose than defining a FilterRegistrationBean?

这是 Boot 中的正确方法吗,导致我看到一些属性要应用到 application.properties 中。它们与定义 FilterRegistrationBean 的目的相同吗?

# MULTIPART (MultipartProperties)
multipart.enabled=true
multipart.file-size-threshold=0 # Threshold after which files will be written to disk.
multipart.location= # Intermediate location of uploaded files.
multipart.max-file-size=1Mb # Max file size.
multipart.max-request-size=10Mb # Max request size.

Could anyone provide any sample how to use it? Thanks.

谁能提供任何示例如何使用它?谢谢。

By the way, It tried to set the property "multipart.enabled=true" and I got:

顺便说一句,它试图设置属性“multipart.enabled=true”,我得到:

Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'enabled' of bean class [org.springframework.boot.autoconfigure.web.MultipartProperties]: Bean property 'enabled' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1076)
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:927)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95)
    at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:749)
    at org.springframework.validation.DataBinder.doBind(DataBinder.java:645)
    at org.springframework.boot.bind.RelaxedDataBinder.doBind(RelaxedDataBinder.java:121)
    at org.springframework.validation.DataBinder.bind(DataBinder.java:630)
    at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:253)
    at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:227)
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:296)
    ... 73 common frames omitted

采纳答案by Phil Webb

This was a bugin Spring Boot and will be fixed in 1.2.5.

这是Spring Boot中的一个错误,将在 1.2.5 中修复。

回答by K. Siva Prasad Reddy

First, there is no enabledproperty in org.springframework.boot.autoconfigure.web.MultipartPropertiesclass.

首先,类中没有启用的属性org.springframework.boot.autoconfigure.web.MultipartProperties

Refer https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartProperties.java

参考https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartProperties.java

If you are using Servlet 3 container you no need to use commons-fileupload mechanism and Multipart support is enabled by default. If you don't want to customize any multipart default config no need to add any config in application.propertiesas well.

如果您使用的是 Servlet 3 容器,则无需使用 commons-fileupload 机制,并且默认情况下启用 Multipart 支持。如果您不想自定义任何多部分默认配置,也无需添加任何配置application.properties

<form method="post" action="upload" enctype="multipart/form-data">
  File: <input type="file" name="file"/>
  <input type="submit" value="Submit"/>
</form>

@RequestMapping(value="/upload", method=RequestMethod.POST)
public String upload(@RequestPart("file") MultipartFile multipartFile)
{
    System.out.println(multipartFile.getOriginalFilename());
    return "redirect:/";
}

If you want to use commons-fileupload then adding following configuration is working fine:

如果你想使用 commons-fileupload 然后添加以下配置工作正常:

package demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.multipart.support.MultipartFilter;

@SpringBootApplication
public class BootDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootDemoApplication.class, args);
    }

    @Bean
    public CommonsMultipartResolver commonsMultipartResolver() {
        final CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        commonsMultipartResolver.setMaxUploadSize(-1);
        return commonsMultipartResolver;
    }

    @Bean
    public FilterRegistrationBean multipartFilterRegistrationBean() {
        final MultipartFilter multipartFilter = new MultipartFilter();
        final FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(multipartFilter);
        filterRegistrationBean.addInitParameter("multipartResolverBeanName", "commonsMultipartResolver");
        return filterRegistrationBean;
    }
}

And of course we need to add commons-fileupload dependency.

当然,我们需要添加 commons-fileupload 依赖项。

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

回答by u9703212

If you want to use CommonsMultipartFileto upload a file, please add @EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})in your spring boot project. Disable the multi-part configuration in spring boot.

如果你想用来CommonsMultipartFile上传文件,请@EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})在你的spring boot项目中添加。在 spring boot 中禁用多部分配置。

   public RespDataView provisionalMediaUpload(@RequestParam("file") CommonsMultipartFile file,
                                               @RequestParam("type") String type) {}