報錯場景:
使用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>