Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use CommonsMultipartResolver in Spring Boot

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

@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?

# 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:

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
like image 544
Carlos Alberto Avatar asked Jun 10 '15 22:06

Carlos Alberto


2 Answers

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

like image 181
Phil Webb Avatar answered Oct 20 '22 22:10

Phil Webb


First, there is no enabled property in org.springframework.boot.autoconfigure.web.MultipartProperties class.

Refer 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.properties as well.

<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:

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.

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>
like image 38
K. Siva Prasad Reddy Avatar answered Oct 20 '22 20:10

K. Siva Prasad Reddy