2013-04-16 102 views
10

我需要將文件從瀏覽器上傳到服務器。我使用spring 3.2作爲我的web框架。春季上傳文件問題

所以我配置我的應用程序是這樣的。

1 - 我的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 - 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 - 控制器來處理多部分上傳

@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其中i放置我的表格提交檔案

...<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>... 

當我提交我的形式,我得到一個異常

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

我不知道爲什麼。當我刪除@RequestParam註釋時,我得到了我的方法調用,但文件參數= null。 我的問題是什麼?

回答

5

我解決了這個問題,在我的spring配置文件中添加了以下內容:

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

(我得到的錯誤是「org.springframework.web.bind.MissingServletRequestParameterException:必需MultipartFile參數 'MYFILE' 不存在

0

您還需要爲您的web應用程序配置MultipartFilter。根據它的Javadoc,它使用MultipartResolver解決了多部分請求(但你已經配置了那個)。您需要將其映射到處理文件上傳的Controller的請求路徑(的一部分)。

首先,添加MultipartFilter到你的web.xml:

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

接下來,過濾器映射到(的一部分),需要接受上傳文件的網址:

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

我不知道如何配置它。我需要更改我的web.xml嗎? 或者我必須創建自己的Filter擴展MultipartFilter並將其映射到 /admin/library/upload_file? – user2160696

+0

有關配置詳細信息,請參閱更新後的答案。 – mthmulders

+2

可悲的是它沒有幫助。我添加了這兩個部分,但仍然以null作爲參數。 – user2160696

2

我可以做到這一點

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

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

     registration.setMultipartConfig(multipartConfigElement); 

    } 
1

@ user64141是正確的,但如果使用Java的配置,而不是XML的,試試

@Bean 
public MultipartResolver multipartResolver() { 
    return new CommonsMultipartResolver(); 
}