我曾嘗試使用是CommonsMultipartResolver在引導翻譯我的舊應用程序(WAR)來引導,而現在它得到了下面的代碼:如何使用是CommonsMultipartResolver在春季啓動
@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;
}
}
這是在正確的道路啓動,導致我看到一些屬性在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.
任何人都可以提供任何示例如何使用它?謝謝。
順便說一句,它試圖設置屬性「multipart.enabled =真正的」和我:
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
實際上這個配置是有缺陷的,如果你想與'DispatcherServlet'正確集成,bean的名字應該是'multipartResolver'而不是'comonsMultipartResolver'。此外,過濾器不是強制性的(只有當你將它與Spring Security結合時)。 –
確實沒有該類中的「enabled」屬性,但該屬性應該匹配[this](https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src /main/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.java#L51)啓用/禁用多部分配置。所以基本上我認爲這是Spring Boot的multipart config部分中的一個問題。同樣,並非所有的屬性都需要有相應的類來映射到,還有其他方法可以使用'application.properties'文件中定義的屬性。 –
如果你看Spring Boot的文檔http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html有一個屬性「multipart.enabled = true」,並且有說啓用值。爲什麼它存在,如果它是無效的呢? –