• 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>
  • 相关阅读:
    实验-继承&super.doc
    Python库
    Github高级搜索
    代码报错记录
    编程问题解决
    百科
    【Android】添加依赖包
    【Android】导航栏(加图片icon)和不同页面的实现(viewpager+tablayout)
    【Android】Android Studio真机调试的问题统整
    【AD】自己画板的备忘
  • 原文地址:https://www.cnblogs.com/xjs1874704478/p/13719106.html
Copyright © 2020-2023  润新知