背景:
SpringMVC项目需要上传文件。于是按照下面的步骤操作了。但是还是遇到了问题。
第一步,在application-context.xml添加了如下的代码
<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"/>
第二步,然后在Controller里面添加接收file上传的代码如下
@RequestMapping(value = "/file/upload",method = RequestMethod.POST)
@Response
public BaseResult fileUpload(@RequestPart("file") MultipartFile file , HttpServletRequest request) throws IOException {
File serverFile = UploadUtils.saveFile(file);
return new DataResult(result);
}
第三步,用POST发上传文件的请求

但是。Web服务抛错。异常如下。注意看异常。
multipart servlet request; nested exception is java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided
[dart-backend]15:36:31 506 ERROR (com.ximalaya.mobile.common.exception.resolver.BaseHandlerExceptionResolver:70) - resolve exception message:Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: Unable to process parts as no

在SpringMVC项目中进行文件上传时遇到错误。通过在`application-context.xml`配置multipartResolver,及在Controller中编写接收文件代码后,Web服务仍抛出异常。解决办法是在DispatcherServlet中添加Multipart-config,指定location和文件大小限制,以此避免异常。本文旨在提供快速解决问题的方法,避免开发者走弯路。
最低0.47元/天 解锁文章
1212

被折叠的 条评论
为什么被折叠?



