SpringMVC使用MultipartFile上传文件报错【org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.multipart.MultipartFile]: Specified class is an interface】


报错场景:

  使用SpringMVC(或SSM框架)实现文件上传时报【 Failed to instantiate [org.springframework.web.multipart.MultipartFile]: Specified class is an interface】错,控制器代码:

 1 package com.xjs.controller;
 2 
 3 import org.apache.commons.io.FileUtils;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.ui.Model;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.multipart.MultipartFile;
 8 
 9 import javax.servlet.http.HttpServletResponse;
10 import java.io.File;
11 import java.io.IOException;
12 
13 /**
14  * @ClassName:FileController
15  * @Author:微微亮
16  * @Description:
17  * @Date:2020/9/23 11:37
18  * @Version: 1.0
19  */
20 @Controller
21 @RequestMapping("/myFile")
22 public class FileController {
23 
24     @RequestMapping("/add")
25     public String upload(Model model, MultipartFile xjs) throws IOException {
26         //获取文件的名字
27         String filename = xjs.getOriginalFilename();
28         String newFileName = System.currentTimeMillis()+filename;
29         File file1 = new File("E:\\myImg\\"+newFileName);
30 
31         model.addAttribute("newFileName",newFileName);
32         //把文件写入相应的文件夹
33         xjs.transferTo(file1);
34         return "forward:/show.jsp";
35     }
36 
37 }

解决方法:

在参数MultipartFile前加注解@RequestParam

 1 package com.xjs.controller;
 2 
 3 import org.apache.commons.io.FileUtils;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.ui.Model;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.multipart.MultipartFile;
 8 
 9 import javax.servlet.http.HttpServletResponse;
10 import java.io.File;
11 import java.io.IOException;
12 
13 /**
14  * @ClassName:FileController
15  * @Author:微微亮
16  * @Description:
17  * @Date:2020/9/23 11:37
18  * @Version: 1.0
19  */
20 @Controller
21 @RequestMapping("/myFile")
22 public class FileController {
23 
24     @RequestMapping("/add")
25     public String upload(Model model, @RequestParam("xjs")MultipartFile xjs) throws IOException {
26         //获取文件的名字
27         String filename = xjs.getOriginalFilename();
28         String newFileName = System.currentTimeMillis()+filename;
29         File file1 = new File("E:\\myImg\\"+newFileName);
30 
31         model.addAttribute("newFileName",newFileName);
32         //把文件写入相应的文件夹
33         xjs.transferTo(file1);
34         return "forward:/show.jsp";
35     }
36 }

另一种原因是:

springMVC的配置文件中开启注解:<mvc:annotation-driven/>

可以解决上述报错

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:mvc="http://www.springframework.org/schema/mvc"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
 7 
 8     <!--扫描注解-->
 9     <context:component-scan base-package="com.xjs"/>
10 
11     <!--开启注解-->
12     <mvc:annotation-driven/>
13 
14     <!--bean支持文件上传-->
15     <!--支持文件上传的bean的id是固定的,底层spring会根据这个id值来获取对象-->
16     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
17         <!--设置文件上传的大小,value值的单位是B
18                       默认不能超过2M
19                 -->
20         <property name="maxUploadSize" value="10485760"></property>
21     </bean>
22 
23 </beans>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



猜您在找 springmvc上传文件报错org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.multipart.MultipartFile] Spring使用mutipartFile上传文件报错【Failed to instantiate [org.springframework.web.multipart.MultipartFile]】 关于org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]:Specified class is an interface java org.springframework.web.multipart.MultipartFile 上传文件等 【spring mvc】后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Specified class is an interface org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.jboss.resteasy.plug Java之——java.lang.NoSuchMethodException: [org.springframework.web.multipart.MultipartFile;.() Springboot启动报错Error creating bean with name 'xxx' defined in URL [xxx]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate... org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.hs.model.StudentModel]: No default constructor found; nested exception is java.lang.NoSuchMethodException: c
 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM