如何在Spring Boot中使用CommonsMultipartResolver [英] How to use CommonsMultipartResolver in Spring Boot

查看:628
本文介绍了如何在Spring Boot中使用CommonsMultipartResolver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Boot中将CommonsMultipartResolver用于将旧应用程序(WAR)转换为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;
        }
    }

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

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.

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

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


推荐答案

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

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

这篇关于如何在Spring Boot中使用CommonsMultipartResolver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆