SpringMVC 文件上传异常

在使用SpringMVC进行文件上传时遇到两个问题。首先配置了`spring-Servlet.xml`中的`multipartResolver`,设置最大上传大小和内存大小。问题一是出现'file'参数未找到的400错误,尝试修改@RequestParam的value值无效。问题二是类型转换错误,将@RequestParam的required属性设为false解决了500状态码的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringMVC 文件上传异常

首先需要在 【spring-Servlet.xml】中导入:

<bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"/>
        <property name="maxUploadSize" value="10485760"/>
        <property name="maxInMemorySize" value="40960"/>
</bean>

spring 是通过id精准查找一个类的,所以此处的id一定不能变:multipartResolver

问题一:Required CommonsMultipartFile parameter ‘file’ is not present,状态码400

我仔细检查配置文件和导入的后仍然找不到错误,网上建议:@RequestParam(value = “filename”)中的value值不能和参数名相同,经过修改中还是出现了下面问题二的错误。

blog.csdnimg.cn/1064149b16ad4f2ea7493a6d0ae3271b.png)

问题二:Cannot convert value of type ‘java.lang.String’ to required type ‘org.springframework.web.multipart.commons.CommonsMultipartFile’,状态码:500

我看到了别的博客中@RequestParam(value = “filename”,required = false)加上了required = false,我修改后运行成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值