Spring SpringMVC文件上传错误(二)
来源:互联网 发布:苹果手机游戏推荐 知乎 编辑:程序博客网 时间:2024/05/22 13:01
续上文,
场景:
不上传文件提交表单。
简述一下:表单请求的两种方式
- 同步提交
- 异步提交
笔者之前写过的测试Demo:
HTML:
<form action="upload.do" method="post" enctype="multipart/form-data"> <input type="file" name="fileName" /> <input type="submit" value="Submit" /></form>
控制器:
@RequestMapping(value = "/upload.do", method = RequestMethod.POST)@ResponseBodypublic Map<String, Object> add(@RequestParam(value="fileName") MultipartFile file){ //... return null;}
错误信息:
11:03:07.147 [http-nio-8080-exec-4] DEBUG o.s.w.s.m.a.AnnotationMethodHandlerExceptionResolver - Resolving exception from handler [com.xFee.serverAdmin.controller.ApkInfoController@25a5db]: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile]: no matching editors or conversion strategy found11:03:07.147 [http-nio-8080-exec-4] DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [com.xFee.serverAdmin.controller.ApkInfoController@25a5db]: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile]: no matching editors or conversion strategy found11:03:07.147 [http-nio-8080-exec-4] DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [com.xFee.serverAdmin.controller.ApkInfoController@25a5db]: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile]: no matching editors or conversion strategy found11:03:07.147 [http-nio-8080-exec-4] DEBUG o.s.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'springServlet': assuming HandlerAdapter completed request handling11:03:07.147 [http-nio-8080-exec-4] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
笔者忽略了,在同步提交的时候,Spring把空上传是做null来处理的,而在异步提交的时候,Spring把文件域的值当作空字符串看待的。在Spring做请求转换的时候(request
–>MultipartHttpServletRequest
)底层的TypeConverter
接口实现对null
和""
是做了不同操作的。源码就不贴了,有兴趣的伙伴可以自行从:org.springframework.web.multipart.commons.CommonsMultipartResolver
这个类跟下去,就会发现差异。
解决方案
在配置文件中加入以下配置:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="com.xFee.serverAdmin.utils.SpringMVCFileConverter"></bean> </list> </property> </bean> <mvc:annotation-driven conversion-service="conversionService" />
然后在list下的bean节点的class属性的路径下创建一个SpringMVCFileConverter
:
public class SpringMVCFileConverter implements Converter<String, MultipartFile> { public SpringMVCFileConverter() { super(); } @Override public MultipartFile convert(String source) { return null; }}
注:注意在引入上面配置文件的时候不要忘记引入其定义及约束文件。
学生浅薄,望众师指点wengang.liu
1 0
- Spring SpringMVC文件上传错误(二)
- Spring SpringMVC文件上传错误(一)
- Spring SpringMVC文件上传错误
- Spring SpringMVC文件上传错误
- springmvc文件上传错误
- SpringMVC上传文件错误
- SpringMVC文件上传错误
- SpringMVC文件上传(二)指定文件
- SpringMvc 文件上传错误解决
- Springmvc,Spring MVC文件上传
- spring springmvc实现文件上传
- Spring+Springmvc实现文件上传
- springmvc spring mybatis 文件上传
- SpringMVC中的文件上传 二
- springMVC文件上传——错误(1)
- Spring Framework源码(十):SpringMVC之文件上传
- (文件)图片上传,Spring或SpringMVC框架
- SpringMVC 理论与实用技术(二)文件上传
- java中Comparator比较器的使用
- 【模板】KMP
- POJ3268最短路水题
- stm32 io模拟spi通信
- Dictionary
- Spring SpringMVC文件上传错误(二)
- C++ 动态链接库和静态链接库
- Android ListView单选CheckBox
- JavaScript之严格模式 "use strict";
- 屌丝们如何突破屌丝固有的穷人思维!
- ORACLE触发器详解
- leetcode.307. Range Sum Query - Mutable 树状数组
- 用dir命令显示文件的完整路径及分屏显示
- 网易视频云技术分享:HBase - 建表语句解析