2015-06-10 18 views
9

Tôi đã cố gắng sử dụng CommonsMultipartResolver trong Boot dịch ứng dụng cũ của tôi (WAR) để khởi động, và ngay bây giờ nó có đoạn mã sau:Làm thế nào để sử dụng CommonsMultipartResolver trong mùa xuân 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; 
     } 
    } 

Đây có phải là một cách đúng đắn trong Khởi động, khiến tôi thấy một số thuộc tính được áp dụng trong application.properties. Họ sẽ có cùng một mục đích hơn là xác định một 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. 

Có thể cung cấp bất kỳ mẫu nào làm cách nào để sử dụng không? Cảm ơn.

Bằng cách này, Nó cố gắng để thiết lập thuộc tính "multipart.enabled = true" và tôi đã nhận:

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 

Trả lời

2

Đây là bug trong Spring Boot và sẽ được sửa trong 1.2.5.

1

Thứ nhất, không có kích hoạt tài sản trong org.springframework.boot.autoconfigure.web.MultipartProperties lớp.

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

Nếu bạn đang sử dụng Servlet 3 thùng chứa bạn không cần phải sử dụng cơ chế commons-FileUpload và hỗ trợ Multipart được kích hoạt theo mặc định. Nếu bạn không muốn tùy chỉnh bất kỳ cấu hình mặc định nhiều phần nào, không cần phải thêm bất kỳ cấu hình nào trong 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:/"; 
} 

Nếu bạn muốn sử dụng commons-FileUpload sau đó thêm sau cấu hình đang làm việc tốt:

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; 
    } 
} 

Và dĩ nhiên chúng ta cần thêm sự phụ thuộc commons-FileUpload.

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

Thực tế cấu hình đó là thiếu sót, tên của bean phải là 'multipartResolver' thay vì' comonsMultipartResolver' nếu bạn muốn tích hợp đúng với 'DispatcherServlet'. Ngoài ra bộ lọc không bắt buộc (chỉ khi bạn kết hợp nó với Spring Security). –

+0

Thực sự không phải là thuộc tính 'đã bật' trong lớp đó, tuy nhiên thuộc tính phải khớp với [this] (https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src /main/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.java#L51) để bật/tắt cấu hình nhiều phần. Vì vậy, về cơ bản tôi xem xét rằng một vấn đề trong phần cấu hình nhiều phần của Spring Boot. Ngoài ra không phải tất cả các thuộc tính đều cần phải có một lớp tương ứng để được ánh xạ tới, cũng có các phương tiện khác của việc sử dụng các thuộc tính được định nghĩa trong tệp 'application.properties'. –

+0

Nếu bạn xem tài liệu về Spring Boot http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html có thuộc tính "multipart.enabled = true" và có nội dung giá trị được bật. Tại sao nó tồn tại nếu nó không hợp lệ như vậy? –

Các vấn đề liên quan