1. 程式人生 > >spring MVC報錯 Expected MultipartHttpServletRequest: is a MultipartResolver configured?

spring MVC報錯 Expected MultipartHttpServletRequest: is a MultipartResolver configured?

在使用Spring MVC進行圖片上傳的時候,報錯

嚴重: Servlet.service() for servlet [taotao-manager] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?] with root cause
java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
at org.springframework.util.Assert.notNull(Assert.java:112)
.......

這是由於在spring MVC進行檔案上傳的時候,需要在springmvc.xml檔案中配置多部件解析器。注:需要引入檔案上傳的jar包,file-upload和common-io

配置如下:

<!-- 定義檔案上傳解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 設定預設編碼 -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 設定檔案上傳的最大值5MB,5*1024*1024 -->
<property name="maxUploadSize" value="5242880"></property>
</bean>

重啟tomcat,問題解決。