SpringMVC实现文件上传报错:rejected value [org.springframework.web.multipart.commons.CommonsMultipartFile@*]

  • Post author:
  • Post category:其他


先看错误信息:

Field error in object 'gene' on field 'geneFarmImg': rejected value [org.springframework.web.multipart.commons.CommonsMultipartFile@477c7a2f]; codes [typeMismatch.gene.geneFarmImg,typeMismatch.geneFarmImg,typeMismatch.java.lang.String,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [gene.geneFarmImg,geneFarmImg]; arguments []; default message [geneFarmImg]]; default message [Failed to convert property value of type 'org.springframework.web.multipart.commons.CommonsMultipartFile' to required type 'java.lang.String' for property 'geneFarmImg'; 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 'geneFarmImg': no matching editors or conversion strategy found]]
17:35:03.453 [http-nio-1210-exec-7] DEBUG o.s.w.m.c.CommonsMultipartResolver - Cleaning up part 'atlas', filename ''
17:35:03.453 [http-nio-1210-exec-7] DEBUG o.s.w.m.c.CommonsMultipartResolver - Cleaning up part 'callus', filename ''
17:35:03.453 [http-nio-1210-exec-7] DEBUG o.s.w.m.c.CommonsMultipartResolver - Cleaning up part 'emergence', filename ''
17:35:03.453 [http-nio-1210-exec-7] DEBUG o.s.w.m.c.CommonsMultipartResolver - Cleaning up part 'geneFarmImg', filename ''
17:35:03.453 [http-nio-1210-exec-7] DEBUG o.s.web.servlet.DispatcherServlet - Completed 400 BAD_REQUEST

主要报错点:


rejected value [org.springframework.web.multipart.commons.CommonsMultipartFile@3105ca2a]



原因

先要了解

Spring注入的是接口,关联的是实体类

。这里 file 用的是实体类的变量名,所以就注入了实体类。而实体类体中该属性是String类型,自然报错。就是

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



解决

form 表单中的type为file的标签

name另取

,后台获取这个另取的name。



实例

  1. 实体类
        @NoArgsConstructor
		@AllArgsConstructor
		@Setter
		@Getter
		@ToString
		public class Gene implements Serializable {
		    private String geneId;
		    private String geneName;
		    private String geneFarmImg;
		}

2.form表单

   <form id="ff"   method="post" enctype="multipart/form-data">
                <div class="m-b">
                    <label>基因编号:</label>
                    <input type="text" name="geneId" placeholder="" class="bg-white text">
                </div>

                <div>
                    <label>基因名称:</label>
                    <input type="text" name="geneName" placeholder="" class="bg-white text">
                </div>
                <div>
                    <label>田间检测图:</label>
                    <input name="farm" class="easyui-filebox"  multiple="true" style="width: 100%; height:40px;">
                </div>
                <div class="foot_btn">

                    <a  href="#" class="easyui-linkbutton" style="height: 40px;width: 80px;"
                        data-options="iconCls:'icon-ok',toggle:false" onclick="submitBtn()">提交</a> &nbsp;
                    <a href="#" class="easyui-linkbutton" style="height: 40px;width: 80px;"
                       data-options="iconCls:'icon-cancel',toggle:false" onclick="cancelBtn()">取消</a>
                </div>

            </form> 

3.后台Controller

    @RequestMapping("/add")
    @ResponseBody
    public Map<String,Object> addGene(Gene gene,
                 @RequestParam( value = "farm",required = false)  CommonsMultipartFile[] farm,
                 HttpServletRequest request ){
        try{
            FileUpload upload = new FileUpload();
            gene.setGeneFarmImg(upload.uploadFile(farm,request));
            geneService.saveGene(gene);
            map.put("msg","ok");
        } catch (Exception e){
            e.printStackTrace();
            map.put("msg","error");
        }
        return map;
    }



补充

上传文件不成功还可能是如下情况:

  1. form表单未添加:

    enctype="multipart/form-data"
  2. 缺少相应的jar包。

    commons-io.xx.jar

    ,

    commons-fileupload.xx.jar



    3.spring MVC的配置文件中没有配置:
	<!-- 配置文件上传 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
			<property name="maxUploadSize">
			<value>-1</value>    	<!-- -1表示大小无限制 -->
		</property>
		<property name="defaultEncoding">
			<value>UTF-8</value>
		</property>
	</bean>


参考文章:

Spring SpringMVC 文件上传错误



版权声明:本文为weixin_43815050原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。