场景:
测试一个最基础的关于SpringMVC文件上传的功能。
必须配置的:
- 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相同。