Spring使用mutipartFile上传文件报错【Failed to…

2018-08-10 11:15:16来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

报错场景:

使用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
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:JAVAEmail工具错误java.lang.ClassNotFoundException: javax.act

下一篇:java day10(续day9)