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