2013-04-16 36 views
10

Tôi cần tải lên tệp từ trình duyệt đến máy chủ. Tôi sử dụng mùa xuân 3.2 làm khung web của tôi.Sự cố tải lên tệp mùa xuân

Vì vậy, tôi đã định cấu hình ứng dụng của mình như thế này.

1 - Tôi đã nhận web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

    <context-param> 
     <param-name>contextClass</param-name> 
     <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> 
    </context-param> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>racoonsoft.chaos.settings</param-value> 
    </context-param> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <servlet> 
     <servlet-name>MyServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value></param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>MyServlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <welcome-file-list> 
     <welcome-file>admin/library</welcome-file> 
    </welcome-file-list> 

</web-app> 

2 - lớp MainConfig

@Configuration 
@Import({WebConfig.class }) 
public class MainConfig { 
    @Bean 
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { 
     return new PropertySourcesPlaceholderConfigurer(); 
    } 

    @Bean 
    public static ScheduledAnnotationBeanPostProcessor scheduledAnnotationBeanPostProcessor() { 
     return new ScheduledAnnotationBeanPostProcessor(); 
    } 

    @Bean 
    public static StandardServletMultipartResolver multipartResolver() { 
     StandardServletMultipartResolver resolver = new StandardServletMultipartResolver(); 
     return resolver; 
    } 
} 

3 - Bộ điều khiển để xử lý cập nhật nhiều phần dữ liệu

@Controller 
@MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB 
     maxFileSize=1024*1024*10,  // 10MB 
     maxRequestSize=1024*1024*50) 
public class FileUpload 
{ 
    public static final int UPLOAD_RESULT_OK = 100000; 
    @Autowired 
    BookDao book_dao; 

    @RequestMapping(value = "/admin/library/upload_file", method = RequestMethod.POST) 
    public String saveFiles(@RequestParam("file-file") MultipartFile file) throws IOException 
    { 
     if (!file.isEmpty()) 
     { 
      byte[] bytes = file.getBytes(); 
      return "redirect:caps/total_fail"; 
     } 
     else 
     { 
      return "redirect:caps/total_fail"; 
     } 
    } 
} 

4 - jsp nơi mà tôi đặt của tôi biểu mẫu để gửi tệp

...<form method="post" action="/admin/library/upload_file" enctype="multipart/form-data"> 
       <input type="text" name="name"/> 
       <input type="file" name="file-file"/> 
       <input type="submit"/> 
      </form>... 

Khi tôi nộp mẫu đơn của tôi có một ngoại lệ

org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'file-file' is not present 
    org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:202) 

tôi không biết tại sao. Khi tôi loại bỏ chú thích @RequestParam, tôi đã nhận được phương thức được gọi nhưng tham số tệp = null. vấn đề của tôi là gì?

Trả lời

5

Tôi đã khắc phục sự cố này bằng cách thêm phần sau vào tệp cấu hình mùa xuân của mình:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /> 

(Các lỗi tôi đã nhận được là "org.springframework.web.bind.MissingServletRequestParameterException: Yêu cầu MultipartFile tham số 'myfile' không có mặt

0

Bạn cũng cần cấu hình MultipartFilter cho ứng dụng web của mình. Theo Javadoc của nó, nó giải quyết các yêu cầu nhiều phần bằng cách sử dụng MultipartResolver (nhưng bạn đã có một cấu hình đã được cấu hình). Bạn sẽ cần ánh xạ nó tới (một phần) đường dẫn yêu cầu cho Bộ điều khiển xử lý các tệp tải lên.

tiên, bổ sung MultipartFilter để web.xml của bạn:

<filter> 
    <filter-name>multipartFilter</filter-name> 
    <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class> 
</filter> 

Tiếp theo, lập bản đồ các bộ lọc để (một phần của) URL mà cần phải chấp nhận tập tin tải lên:

<filter-mapping> 
    <filter-name>multipartFilter</filter-name> 
    <url-pattern>/admin/library/upload_file</url-pattern> 
</filter-mapping> 
+1

Tôi không chắc cách cấu hình. Tôi có cần thực hiện các thay đổi đối với tệp web.xml của mình không? Hoặc tôi phải tạo bộ lọc MultipartFilter mở rộng của riêng mình và ánh xạ nó tới /admin/library/upload_file ?? – user2160696

+0

Xem câu trả lời được cập nhật để biết chi tiết cấu hình. – mthmulders

+2

Đáng buồn thay, nó không giúp ích gì. Tôi đã thêm hai phần đó, nhưng vẫn có tham số null. – user2160696

2

i có thể làm điều đó với

@Override 
    protected void customizeRegistration(ServletRegistration.Dynamic registration) { 

     MultipartConfigElement multipartConfigElement = new MultipartConfigElement("/",100000, 200000, 50000); 

     registration.setMultipartConfig(multipartConfigElement); 

    } 
1

@ user64141 là đúng, nhưng nếu sử dụng Java cấu hình thay vì xml , hãy thử

@Bean 
public MultipartResolver multipartResolver() { 
    return new CommonsMultipartResolver(); 
} 
Các vấn đề liên quan