2013-04-16 103 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(); 
}