2014-09-30 49 views
20

我使用XML配置spring4是well.like這項工作:如何配置 「是CommonsMultipartResolver」 在spring4沒有XML上傳文件

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

當我配置spring4沒有xml.like這樣的:

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

}

它拋出一個異常:

九月 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]: [email protected]] with root cause 
java.lang.IllegalStateException: Current request is not of type [org.springframework.web.multipart.MultipartHttpServletRequest]: [email protected] 
    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) 

回答

46

From MultipartResolver Javadoc:要定義一個實現,請在DispatcherServlet的應用程序上下文中創建一個ID爲「multipartResolver」的bean。

在您的xml配置中,該bean具有正確的標識multipartResolver,但不在您當前的java配置中。你應該寫:

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

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

非常感謝你~~我知道了〜 – 2014-10-02 16:15:36

+1

認真!我爲此失去了2個小時!爲什麼Spring不採用CommonsMultipartResolver類型的唯一bean。而且我不會談論錯誤消息是說我的@RequestParam不存在,而我的輸入文件名稱正確。很高興,春天可以在那裏做得更好。至少,一個有意義的錯誤消息。謝謝Serge Ballesta。 – Comencau 2016-09-01 15:43:34

+0

我已經完成了這個工作,但是我一直收到關於不支持的內容類型的錯誤。任何想法爲什麼[這是不工作](http://stackoverflow.com/questions/42044639/file-upload-wont-work/42044768#42044768)? – displayname 2017-02-04 19:47:38

相關問題