Spring MVC中文件上传时的异常HttpRequest -> MultipartHttpServletRequest ClassCastException

时间:2021-12-31 10:24:51

Spring MVC中文件上传时的异常HttpRequest -> MultipartHttpServletRequest ClassCastException


@RequestMapping(value="/attachment/upload", method = RequestMethod.POST)publicString upload(ModelMap model, HttpServletRequest request, HttpServletResponse response){MultipartHttpServletRequest multipartRequest =(MultipartHttpServletRequest) request;MultipartFile attachmentFile = multipartRequest.getFile("attachment");//....}

form表单也指定了enctype:

<form method="post" action="/attachment/upload.do" ENCTYPE="multipart/form-data">
<input type="file" name="attachment" ><br/>
<input type="submit">
</form>

上传的时候却抛入如下的异常:

java.lang.ClassCastException: com.caucho.server.http.HttpRequest cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest


原因很简单,我没有配置multipartResolver:

 <bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="10240000"/>  
</bean>

转载至 I'm Bitiliu - 《Spring MVC中文件上传时的异常HttpRequest -> MultipartHttpServletRequest ClassCastException》

http://www.bitiliu.com/archives/33