Spring using mutipartfile Upload file Error "Failed to instantiate [Org.springframework.web.multipart.MultipartFile]"

Source: Internet
Author: User

Error Scenario:

Using the SSM framework to implement file upload times wrong "Failed to instantiate [Org.springframework.web.multipart.MultipartFile]" error, controller source code:

@Controller @requestmapping ("/file") Public classFileudcontroller {@RequestMapping (value= "/fileupload", method=requestmethod.post) PublicModelandview fileUpload (multipartfile file,httpservletrequest req) {String path= Req.getsession (). Getservletcontext (). Getrealpath ("Upload"); String FileName=File.getoriginalfilename (); File dir=NewFile (path,filename); if(!dir.exists ())        {Dir.mkdirs (); }                Try{File.transferto (dir); } Catch(IllegalStateException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } modelandview MV=NewModelandview ("Success"); returnMV; }

error message:
1SEVERE:Servlet.service () forservlet [Springdispatcherservlet] in the context with path [/SQLT] threw exception [Request processing failed; nested exceptio N is org.springframework.beans.BeanInstantiationException:Failed to instantiate [ Org.springframework.web.multipart.MultipartFile]: Specifiedclassis anInterface] with root cause2Org.springframework.beans.BeanInstantiationException:Failed to instantiate [ Org.springframework.web.multipart.MultipartFile]: Specifiedclassis anInterface3At Org.springframework.beans.BeanUtils.instantiateClass (beanutils.java:101)4At Org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute ( handlermethodinvoker.java:775)5At Org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments ( handlermethodinvoker.java:368)6At Org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod ( handlermethodinvoker.java:172)7At Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod ( annotationmethodhandleradapter.java:446)8At Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle ( annotationmethodhandleradapter.java:434)9At Org.springframework.web.servlet.DispatcherServlet.doDispatch (dispatcherservlet.java:959)TenAt Org.springframework.web.servlet.DispatcherServlet.doService (dispatcherservlet.java:893) OneAt Org.springframework.web.servlet.FrameworkServlet.processRequest (frameworkservlet.java:967) AAt Org.springframework.web.servlet.FrameworkServlet.doPost (frameworkservlet.java:869) -At Javax.servlet.http.HttpServlet.service (httpservlet.java:661) -At Org.springframework.web.servlet.FrameworkServlet.service (frameworkservlet.java:843) theAt Javax.servlet.http.HttpServlet.service (httpservlet.java:742) -At Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:231) -At Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:166) -At Org.apache.tomcat.websocket.server.WsFilter.doFilter (wsfilter.java:52) +At Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:193) -At Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:166) +At Org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal (characterencodingfilter.java:85) AAt Org.springframework.web.filter.OncePerRequestFilter.doFilter (onceperrequestfilter.java:107) atAt Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:193) -At Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:166) -At Org.apache.catalina.core.StandardWrapperValve.invoke (standardwrappervalve.java:198) -At Org.apache.catalina.core.StandardContextValve.invoke (standardcontextvalve.java:96) -At Org.apache.catalina.authenticator.AuthenticatorBase.invoke (authenticatorbase.java:504) -At Org.apache.catalina.core.StandardHostValve.invoke (standardhostvalve.java:140) inAt Org.apache.catalina.valves.ErrorReportValve.invoke (errorreportvalve.java:81) -At Org.apache.catalina.valves.AbstractAccessLogValve.invoke (abstractaccesslogvalve.java:650) toAt Org.apache.catalina.core.StandardEngineValve.invoke (standardenginevalve.java:87) +At Org.apache.catalina.connector.CoyoteAdapter.service (coyoteadapter.java:342) -At Org.apache.coyote.http11.Http11Processor.service (http11processor.java:803) theAt Org.apache.coyote.AbstractProcessorLight.process (abstractprocessorlight.java:66) *At Org.apache.coyote.abstractprotocol$connectionhandler.process (abstractprotocol.java:790) $At Org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun (nioendpoint.java:1459)Panax NotoginsengAt Org.apache.tomcat.util.net.SocketProcessorBase.run (socketprocessorbase.java:49) - At Java.util.concurrent.ThreadPoolExecutor.runWorker (Unknown Source) the At Java.util.concurrent.threadpoolexecutor$worker.run (Unknown Source) +At Org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run (taskthread.java:61) AAt Java.lang.Thread.run (Unknown Source)
Workaround:

Add annotations before parameter Mutipartfile @requestparam

... @RequestMapping (value= "/fileupload", method=requestmethod.post)      Public Modelandview fileUpload (@RequestParam multipartfile file,httpservletrequest req) {        = Req.getsession (). Getservletcontext (). Getrealpath ("Upload");         = file.getoriginalfilename ();         New File (path,filename); ..

cause of the problem:

It is possible that the direct incoming mutipartfile parameter spring will not be able to automatically map the uploaded file into the parameter, so you need to use the @requestparam designation to map the uploaded file to the parameter.

Spring uses Mutipartfile to upload file error "Failed to instantiate [Org.springframework.web.multipart.MultipartFile]"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.