Spring使用mutipartFile上传文件报错【Failed to…
2018-08-10 11:15:16来源:博客园 阅读 ()
报错场景:
使用SSM框架实现文件上传时报“Failed to instantiate [org.springframework.web.multipart.MultipartFile]”错,控制器源代码:
@Controller @RequestMapping("/file") public class FileUDController { @RequestMapping(value="/fileUpload",method=RequestMethod.POST) public ModelAndView fileUpload(MultipartFile file,HttpServletRequest req) { String path = req.getSession().getServletContext().getRealPath("upload"); String fileName = file.getOriginalFilename(); File dir = new File(path,fileName); if(!dir.exists()) { dir.mkdirs(); } try { file.transferTo(dir); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ModelAndView mv = new ModelAndView("success"); return mv; }
报错信息:
1 SEVERE: Servlet.service() for servlet [springDispatcherServlet] in context with path [/sqlt] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.multipart.MultipartFile]: Specified class is an interface] with root cause 2 org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.multipart.MultipartFile]: Specified class is an interface 3 at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:101) 4 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:775) 5 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:368) 6 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:172) 7 at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446) 8 at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434) 9 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) 10 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) 11 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) 12 at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869) 13 at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) 14 at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) 15 at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 16 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) 17 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 18 at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 19 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 20 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 21 at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85) 22 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 23 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 24 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 25 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) 26 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) 27 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504) 28 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) 29 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) 30 at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650) 31 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) 32 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) 33 at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) 34 at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) 35 at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) 36 at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) 37 at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 38 at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 39 at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 40 at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 41 at java.lang.Thread.run(Unknown Source)
解决方法:
在参数 MutipartFile 前添加注解@RequestParam
... @RequestMapping(value="/fileUpload",method=RequestMethod.POST) public ModelAndView fileUpload(@RequestParam MultipartFile file,HttpServletRequest req) { String path = req.getSession().getServletContext().getRealPath("upload"); String fileName = file.getOriginalFilename(); File dir = new File(path,fileName); ...
问题原因:
可能是直接传入mutipartFile参数Spring将无法自动映射上传文件到参数中,因此需要使用@RequestParam指定,将上传的文件映射到参数中。
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Spring系列.ApplicationContext接口 2020-06-11
- springboot2配置JavaMelody与springMVC配置JavaMelody 2020-06-11
- 给你一份超详细 Spring Boot 知识清单 2020-06-11
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- 掌握SpringBoot-2.3的容器探针:实战篇 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash