Spring MVC 文件上传

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

 

我在关注点二出了问题: 未添加  ENCTYPE="multipart/form-data"

 

关注点一:文件上传的代码很简单:

1
2
3
4
5
6
@RequestMapping(value = "/attachment/upload", method = RequestMethod.POST)
public String upload(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
	MultipartHttpServletRequest multipartRequest  =  (MultipartHttpServletRequest) request;
	MultipartFile attachmentFile = multipartRequest.getFile("attachment");
	//....
}

关注点二:form表单也指定了enctype:

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

关注点三:上传的时候却抛入如下的异常:

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

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

1
2
3
4
<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>

你可能感兴趣的:(spring mvc)