之前的表单数据都是文本数据,现记录:利用MultipartResolver进行文件上传。
①首先,需引入commons-fileUpload和commons-io jar包,pom.xml文件的坐标:

<
properties
>
< spring.version >3.2.1.RELEASE </ spring.version >
</ properties >
< dependencies >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >spring-webmvc </ artifactId >
< version >${spring.version} </ version >
</ dependency >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >spring-web </ artifactId >
< version >${spring.version} </ version >
</ dependency >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >spring-context </ artifactId >
< version >${spring.version} </ version >
</ dependency >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >spring-core </ artifactId >
< version >${spring.version} </ version >
</ dependency >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >spring-beans </ artifactId >
< version >${spring.version} </ version >
</ dependency >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >spring-aop </ artifactId >
< version >${spring.version} </ version >
</ dependency >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >spring-tx </ artifactId >
< version >${spring.version} </ version >
</ dependency >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >spring-jdbc </ artifactId >
< version >${spring.version} </ version >
</ dependency >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >spring-test </ artifactId >
< version >${spring.version} </ version >
</ dependency >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >spring-context-support </ artifactId >
< version >${spring.version} </ version >
</ dependency >
< dependency >
< groupId >log4j </ groupId >
< artifactId >log4j </ artifactId >
< version >1.2.16 </ version >
</ dependency >
< dependency >
< groupId >junit </ groupId >
< artifactId >junit </ artifactId >
< version >4.8.2 </ version >
</ dependency >
< dependency >
< groupId >org.apache.velocity </ groupId >
< artifactId >velocity </ artifactId >
< version >1.7 </ version >
</ dependency >
< dependency >
< groupId >org.apache.velocity </ groupId >
< artifactId >velocity-tools </ artifactId >
< version >2.0 </ version >
< exclusions >
< exclusion >
< groupId >org.apache.struts </ groupId >
< artifactId >struts-core </ artifactId >
</ exclusion >
< exclusion >
< groupId >org.apache.struts </ groupId >
< artifactId >struts-taglib </ artifactId >
</ exclusion >
< exclusion >
< groupId >org.apache.struts </ groupId >
< artifactId >struts-tiles </ artifactId >
</ exclusion >
< exclusion >
< groupId >sslext </ groupId >
< artifactId >sslext </ artifactId >
</ exclusion >
</ exclusions >
</ dependency >
< dependency >
< groupId >servletapi </ groupId >
< artifactId >servlet-api </ artifactId >
< version >2.4 </ version >
< optional >true </ optional >
</ dependency >
< dependency >
< groupId >javax.servlet </ groupId >
< artifactId >jstl </ artifactId >
< version >1.2 </ version >
</ dependency >
< dependency >
< groupId >jspapi </ groupId >
< artifactId >jsp-api </ artifactId >
< version >2.0 </ version >
< optional >true </ optional >
</ dependency >
<!-- if you enable JSR303 (Bean Validation) in spring. -->
< dependency >
< groupId >org.hibernate </ groupId >
< artifactId >hibernate-validator </ artifactId >
< version >4.2.0.Final </ version >
<!-- <optional>true</optional> -->
</ dependency >
< dependency >
< groupId >commons-fileupload </ groupId >
< artifactId >commons-fileupload </ artifactId >
< version >1.3.1 </ version >
</ dependency >
< dependency >
< groupId >commons-io </ groupId >
< artifactId >commons-io </ artifactId >
< version >2.2 </ version >
</ dependency >
</ dependencies >
< spring.version >3.2.1.RELEASE </ spring.version >
</ properties >
< dependencies >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >spring-webmvc </ artifactId >
< version >${spring.version} </ version >
</ dependency >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >spring-web </ artifactId >
< version >${spring.version} </ version >
</ dependency >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >spring-context </ artifactId >
< version >${spring.version} </ version >
</ dependency >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >spring-core </ artifactId >
< version >${spring.version} </ version >
</ dependency >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >spring-beans </ artifactId >
< version >${spring.version} </ version >
</ dependency >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >spring-aop </ artifactId >
< version >${spring.version} </ version >
</ dependency >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >spring-tx </ artifactId >
< version >${spring.version} </ version >
</ dependency >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >spring-jdbc </ artifactId >
< version >${spring.version} </ version >
</ dependency >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >spring-test </ artifactId >
< version >${spring.version} </ version >
</ dependency >
< dependency >
< groupId >org.springframework </ groupId >
< artifactId >spring-context-support </ artifactId >
< version >${spring.version} </ version >
</ dependency >
< dependency >
< groupId >log4j </ groupId >
< artifactId >log4j </ artifactId >
< version >1.2.16 </ version >
</ dependency >
< dependency >
< groupId >junit </ groupId >
< artifactId >junit </ artifactId >
< version >4.8.2 </ version >
</ dependency >
< dependency >
< groupId >org.apache.velocity </ groupId >
< artifactId >velocity </ artifactId >
< version >1.7 </ version >
</ dependency >
< dependency >
< groupId >org.apache.velocity </ groupId >
< artifactId >velocity-tools </ artifactId >
< version >2.0 </ version >
< exclusions >
< exclusion >
< groupId >org.apache.struts </ groupId >
< artifactId >struts-core </ artifactId >
</ exclusion >
< exclusion >
< groupId >org.apache.struts </ groupId >
< artifactId >struts-taglib </ artifactId >
</ exclusion >
< exclusion >
< groupId >org.apache.struts </ groupId >
< artifactId >struts-tiles </ artifactId >
</ exclusion >
< exclusion >
< groupId >sslext </ groupId >
< artifactId >sslext </ artifactId >
</ exclusion >
</ exclusions >
</ dependency >
< dependency >
< groupId >servletapi </ groupId >
< artifactId >servlet-api </ artifactId >
< version >2.4 </ version >
< optional >true </ optional >
</ dependency >
< dependency >
< groupId >javax.servlet </ groupId >
< artifactId >jstl </ artifactId >
< version >1.2 </ version >
</ dependency >
< dependency >
< groupId >jspapi </ groupId >
< artifactId >jsp-api </ artifactId >
< version >2.0 </ version >
< optional >true </ optional >
</ dependency >
<!-- if you enable JSR303 (Bean Validation) in spring. -->
< dependency >
< groupId >org.hibernate </ groupId >
< artifactId >hibernate-validator </ artifactId >
< version >4.2.0.Final </ version >
<!-- <optional>true</optional> -->
</ dependency >
< dependency >
< groupId >commons-fileupload </ groupId >
< artifactId >commons-fileupload </ artifactId >
< version >1.3.1 </ version >
</ dependency >
< dependency >
< groupId >commons-io </ groupId >
< artifactId >commons-io </ artifactId >
< version >2.2 </ version >
</ dependency >
</ dependencies >
② 修改表单页面:

<
form
method
="post"
enctype
="multipart/form-data"
>
< div >
< label for ="userName" >userName: </ label >
< input name ="userName" value ="$!user.userName" >
< span >$!br.getFieldError("userName").getDefaultMessage() </ span >
</ div >
< div >
< label for ="email" >Email: </ label >
< input name ="email" value ="$!user.email" >
</ div >
< div >
< label for ="age" >age: </ label >
< input name ="age" value ="$!user.age" >
</ div >
< div >
< label for ="image" >image: </ label >
< input type ="file" name ="image" >
</ div >
< button type ="submit" >Submit </ button >
</ form >
< div >
< label for ="userName" >userName: </ label >
< input name ="userName" value ="$!user.userName" >
< span >$!br.getFieldError("userName").getDefaultMessage() </ span >
</ div >
< div >
< label for ="email" >Email: </ label >
< input name ="email" value ="$!user.email" >
</ div >
< div >
< label for ="age" >age: </ label >
< input name ="age" value ="$!user.age" >
</ div >
< div >
< label for ="image" >image: </ label >
< input type ="file" name ="image" >
</ div >
< button type ="submit" >Submit </ button >
</ form >
③ 处理multipart 表单的方法:
//
处理表单输入
@RequestMapping(method=RequestMethod.POST)
// @Valid是对user参数的验证
public String addForm(@Valid User user,BindingResult br,@RequestParam(value="image",required= false)MultipartFile image,Model model){
if(br.hasErrors()){
model.addAttribute("br",br);
return "/user/edit";
}
LOG.info("Image:"+image.getName()+" Image Size:"+image.getSize());
// saveImage();
userService.saveUser(user);
return "redirect:/home/"+user.getUserName();
}
@RequestMapping(method=RequestMethod.POST)
// @Valid是对user参数的验证
public String addForm(@Valid User user,BindingResult br,@RequestParam(value="image",required= false)MultipartFile image,Model model){
if(br.hasErrors()){
model.addAttribute("br",br);
return "/user/edit";
}
LOG.info("Image:"+image.getName()+" Image Size:"+image.getSize());
// saveImage();
userService.saveUser(user);
return "redirect:/home/"+user.getUserName();
}
注意到输入参数增加了@RequestParam(value="image",required=false)MultipartFile image,这是将上传的图片文件赋给MultipartFile。但DispatcherServlet并不知道如何处理multipart的表单数据,需要一个multipart解析器将post请求的数据抽取处理,然后再将数据通过DispatcherServlet传递给Controller。
③ spring提供了一个Multipart解析器,需要在spring上下文中注册:
<!--
文件上传解析器
-->
< bean id ="multipartResolver" class ="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
< property name ="maxUploadSize" value ="100000" ></ property >
</ bean >
< bean id ="multipartResolver" class ="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
< property name ="maxUploadSize" value ="100000" ></ property >
</ bean >
这个bean的id是有讲究的,当DispatcherServlet查找multipart解析器时,会查找id为multipartResolver的Bean。若定义的bean id 不是multipartResolver,DispatcherServlet会对它进行忽略。
可以画成下图进行理解: