web-dev-qa-db-fra.com

Comment configurer "CommonsMultipartResolver" au printemps sans xml pour télécharger le fichier

J'utilise xml config spring4 fonctionne bien. Comme ceci:

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
    p:defaultEncoding="utf-8" >
    <!-- one of the properties available; the maximum file size in bytes -->  
    <!-- <property name="maxUploadSize" value="40000000" /> -->  
</bean>

quand je configure spring4 sans xml.like ceci:

@Configuration
@ComponentScan({ "common.user", "service" })
@EnableWebMvc
public class SpringMVCConfig {

@Bean
public ViewResolver createInternalResourceViewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setContentType("text/html");
    viewResolver.setPrefix("/view/");
    //viewResolver.setSuffix(".jsp");
    return viewResolver;
}

@Bean
public DefaultServletHttpRequestHandler createDefaultServletHttpRequestHandler() {
    return new DefaultServletHttpRequestHandler();
}


@Bean
public CommonsMultipartResolver createMultipartResolver() {
    CommonsMultipartResolver resolver=new CommonsMultipartResolver();
    resolver.setDefaultEncoding("utf-8");
    return resolver;
}

}

Il lève une exception:

 九月 30, 2014 6:01:08 下午 org.Apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is Java.lang.IllegalStateException: Current request is not of type [org.springframework.web.multipart.MultipartHttpServletRequest]: org.Apache.catalina.connector.RequestFacade@5ebfad8d] with root cause
Java.lang.IllegalStateException: Current request is not of type [org.springframework.web.multipart.MultipartHttpServletRequest]: org.Apache.catalina.connector.RequestFacade@5ebfad8d
    at org.springframework.web.servlet.mvc.method.annotation.ServletRequestMethodArgumentResolver.resolveArgument(ServletRequestMethodArgumentResolver.Java:97)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.Java:79)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.Java:157)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.Java:124)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.Java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.Java:781)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.Java:721)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.Java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.Java:943)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.Java:877)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.Java:961)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.Java:863)
    at javax.servlet.http.HttpServlet.service(HttpServlet.Java:646)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.Java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.Java:727)
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:303)
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:208)
    at org.Apache.Tomcat.websocket.server.WsFilter.doFilter(WsFilter.Java:52)
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:241)
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:208)
    at common.servlet.RootFilter.doFilter(RootFilter.Java:71)
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:241)
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:208)
    at org.Apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.Java:220)
    at org.Apache.catalina.core.StandardContextValve.invoke(StandardContextValve.Java:122)
    at org.Apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.Java:501)
    at org.Apache.catalina.core.StandardHostValve.invoke(StandardHostValve.Java:171)
    at org.Apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.Java:103)
    at org.Apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.Java:950)
    at org.Apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.Java:116)
    at org.Apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.Java:408)
    at org.Apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.Java:1070)
    at org.Apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.Java:611)
    at org.Apache.Tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.Java:316)
    at Java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at Java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.Apache.Tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.Java:61)
    at Java.lang.Thread.run(Unknown Source)
23
J.K

From MultipartResolver Javadoc: Pour définir une implémentation, créez un bean avec l'ID "multipartResolver" dans le contexte d'application d'un DispatcherServlet.

Dans votre configuration xml, le bean a l'ID correct de multipartResolver, mais pas dans votre configuration actuelle Java. Vous devez écrire:

@Bean
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver resolver=new CommonsMultipartResolver();
    resolver.setDefaultEncoding("utf-8");
    return resolver;
}

ou

@Bean(name = "multipartResolver")
public CommonsMultipartResolver createMultipartResolver() {
    CommonsMultipartResolver resolver=new CommonsMultipartResolver();
    resolver.setDefaultEncoding("utf-8");
    return resolver;
}
49
Serge Ballesta