Spring SpringMVC文件上传错误

场景:

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


必须配置的:

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

错误信息:

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] 
 
 
  • 1
  • 1

HTML代码:

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

实体类:

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("新增成功");
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

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

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

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


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值