我是靠谱客的博主 闪闪发带,最近开发中收集的这篇文章主要介绍SpringMVC实现文件上传报错:rejected value [org.springframework.web.multipart.commons.CommonsMultipartFile@*],觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

先看错误信息:

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 文件上传错误

最后

以上就是闪闪发带为你收集整理的SpringMVC实现文件上传报错:rejected value [org.springframework.web.multipart.commons.CommonsMultipartFile@*]的全部内容,希望文章能够帮你解决SpringMVC实现文件上传报错:rejected value [org.springframework.web.multipart.commons.CommonsMultipartFile@*]所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(27)

评论列表共有 0 条评论

立即
投稿
返回
顶部