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

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值