今天遇到一个问题,使用Spring的Controller中实现上传文件处理的时候,报错:
[ERROR] Failed to parse multipart servlet request; nested exception is java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been providedController中接收文件的代码如下:
@RequestMapping(method = RequestMethod.POST, value = "/file/upload") public @ResponseBody String uploadManageRule(@RequestParam(value = "file") MultipartFile file) { // TODO 把MultipartFile file file.getBytes();或者file.getInputStream();后保存文件处理 return systemService.processUploadFile(file); }
解决办法:
spring-servlet.xml增加CommonsMultipartResolver:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!--增加CommonsMultipartResolver--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8" p:maxUploadSize="10485760000" p:maxInMemorySize="40960" /> </beans>
文章评论