• 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>
  • 相关阅读:
    机器学习技法笔记:16 Finale
    机器学习技法笔记:15 Matrix Factorization
    机器学习技法笔记:14 Radial Basis Function Network
    机器学习技法笔记:13 Deep Learning
    机器学习技法笔记:Homework #7 Decision Tree&Random Forest相关习题
    [HTML] 条件注释判断浏览器
    [ligerUI] grid行编辑示例
    [ligerUI] grid封装调用方法
    [MVC.NET] Asp.Net MVC3 简单入门第一季
    [HTML5] 飞龙天惊-HTML5学习系列
  • 原文地址:https://www.cnblogs.com/xjs1874704478/p/13719106.html
Copyright © 2020-2023  润新知