我试图在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;
}
}
这是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.
有人可以提供任何样品如何使用它吗?感谢。
顺便说一下,它试图设置属性" 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
答案 0 :(得分:2)
首先,org.springframework.boot.autoconfigure.web.MultipartProperties
类中没有启用属性。
如果您使用的是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:/";
}
如果你想使用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;
}
}
当然我们需要添加commons-fileupload依赖。
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
答案 1 :(得分:2)
这是Spring Boot中的bug,将在1.2.5中修复。
答案 2 :(得分:0)
如果您想使用CommonsMultipartFile
上传文件,请在春季启动项目中添加@EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})
。在spring boot中禁用多部分配置。
public RespDataView provisionalMediaUpload(@RequestParam("file") CommonsMultipartFile file,
@RequestParam("type") String type) {}