Spring上传文件报错的解决(CommonsMultipartResolver)

2021-04-17 2639点热度 0人点赞 0条评论

今天遇到一个问题,使用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 provided

Controller中接收文件的代码如下:

@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>

 

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论