zoukankan      html  css  js  c++  java
  • Spring SpringMVC文件上传错误(一)

    场景:

    测试一个最基础的关于SpringMVC文件上传的功能。


    必须配置的:

    1. form的enctype=”multipart/form-data” 这个是上传文件必须的 配置文件中
    <bean id=”multipartResolver” class=”org.springframework.web.multipart.commons.CommonsMultipartResolver”/>

    错误信息:

    Field error in object 'xxx' on field 'xxx': rejected value [org.springframework.web.multipart.commons.CommonsMultipartFile@d872e9]; codes [typeMismatch.xxx.xxx,typeMismatch.xxx,typeMismatch.java.lang.String,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [xxx.xxx,xxx]; arguments []; default message [xxx]]; default message [Failed to convert property value of type 'org.springframework.web.multipart.commons.CommonsMultipartFile' to required type 'java.lang.String' for property 'xxx'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.web.multipart.commons.CommonsMultipartFile] to required type [java.lang.String] for property 'xxx': no matching editors or conversion strategy found] 

    HTML代码:

    <form action="/upload" enctype="multipart/form-data">  
        <input type="file" name="apkurl" id="apkurl" />  
    </form>

    实体类:

    package com.xxx.xxx.controller;
    
    import java.util.Map;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.multipart.MultipartFile;
    
    import com.xxx.xxx.bean.ApkInfo;
    import com.xxx.xxx.core.pagination.mybatis.pager.Page;
    import com.xxx.xxx.exception.ServiceException;
    import com.xxx.xxx.service.ApkInfoManager;
    
    /**
     * 包名:     com.xxx.xxx.controller   
     * 类名:     ApkInfoController
     * 描述:     业务调用对象
     * 作者:     wengang.liu
     * 时间:     2016-04-29 15:18:48
     */
    @Controller
    public class ApkInfoController extends BaseController{
        /**
         * 系统日志
         */
        private static final Logger LOGGER = LoggerFactory.getLogger(ApkInfoController.class);
    
        /**
         * service
         */
        @Autowired
        private ApkInfoManager apkInfoManager;
    
        /**
         * 新增
         */
        @RequestMapping(value = "/upload", method = RequestMethod.POST)
        @ResponseBody
        public Map<String, Object> add(@RequestParam(value="apkurl", required=false) MultipartFile apkFile, ApkInfo apkInfo){
            ...
            return super.success("新增成功");
        }

    这里贴实体类的意义在于,Spring注入的是接口,关联的是实现类。这里注入了实现类,所以报异常了

    简单的说:就是Spring把这个与实体类属性同名的参数注入了这个实体,而这个实体属性的类型是String,自然与CommonsMultipartFile类型无法匹配。

    在此设想,假如实体的属性类型是CommonsMultipartFile是不是可以注入成功呢?


    所以这个问题的一种解决方案为:
    修改表单中,文件域的name命名,注意此处的命名必须和value相同。


    学生浅薄,望众师指点
    
    wengang.liu
    学生浅薄 望众师指点
  • 相关阅读:
    小程序学习资料
    tomcat单应用多实例部署报错 应用jar不存在
    nginx windows版本 1024限制
    mysql连接数
    rocketmq
    nginx路径匹配
    war包的压缩解压缩
    IIS访问HTTP Error 400. The request hostname is invalid
    Microsoft 安全扫描程序
    vscode
  • 原文地址:https://www.cnblogs.com/Nihility/p/14695676.html
Copyright © 2011-2022 走看看