简简单单-在Spring MVC中使用FileUpload功能

先让我们来看一段摘自《Spring 2.5 Reference 中文版》( http://www.redsaga.com/spring_ref/2.5/spring-reference.pdf)的一段关于FileUpload的开场描述:
  "Spring支持web应用中的分段文件上传。这种支持是由即插即用的MultipartResolver来实现。这些解析器都定义在org.springframework.web.multipart包里。Sprig提供了现成的MultipartResolver可以支持Commons FileUpload( http://jakarta.apache.org/commons/fileupload)和COS FileUpload( http://www.servlets.ocm/cos)。"

是的,Spring通过配置一个分段上传解析器来完成对文件上传的解析和封装工作,那么Spring是如何完成这一工作的呢:

首先,DispatcherServlet必须找到一个文件上传解析器的实例,使用这个实例来检查本次请求的HttpServletRequest是否是一个分段文件上传的Request,通过下面的Spring 源码可以看到,首先必须保证有一个MultipartResolver的实例,并且由该类的Resolver的isMultipart方法来验证,本次Request是否为文件上传的Request.如果以上条件都满足,那么Spring将其转换为一个继承自HttpServletRequest的MultipartHttpServletRequest返回,这样在你的Controller中就可以使用这个经过转换的request,从中取到MultipartFile信息。
Java代码   收藏代码
  1. protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException {  
  2.     if (this.multipartResolver != null && this.multipartResolver.isMultipart(request)) {  
  3.         if (request instanceof MultipartHttpServletRequest) {  
  4.             logger.debug("Request is already a MultipartHttpServletRequest - if not in a forward, " +  
  5.                     "this typically results from an additional MultipartFilter in web.xml");  
  6.         }  
  7.         else {  
  8.             return this.multipartResolver.resolveMultipart(request);  
  9.         }  
  10.     }  
  11.     // If not returned before: return original request.  
  12.     return request;  
  13. }  

由以上分析可以看出,我们必须配置一个MultipartResolver,在这里我们使用支持Commons FileUpload的CommonsMultipartResolver:
Java代码   收藏代码
  1. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8"/>  

而且我们可以在该Resolver中定义文件上传的最大长度:
Java代码   收藏代码
  1. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8" p:maxUploadSize="100000"/>  

当用户选择的上传文件大于maxUploadSize值的时候,commons fileupload会抛出一个异常MaxUploadSizeExceededException表示用户上传的文件超出了最大限制。

当然,我们可以通过Spring MVC中的ExceptionResolver来针对该异常定义一个显示错误的View,但针对有可能存在的多个文件上传Controller中都会发生文件大小超长这个异常的情况,除了我们自定义一个粒度更细的ExceptionResolver,我们还可以把上传文件合法性判断挪到用户自己的Controller中来做。而且我个人更偏向于后一种做法。

除了Spring Configuration之外,我们还需要准备一个页面上传的jsp文件供View视图使用:
Java代码   收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4.   
  5. <html>  
  6.     <head>  
  7.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8.         <title>Insert title here</title>  
  9.     </head>  
  10.       
  11.     <body style="text-align:left">  
  12.         <% if(request.getAttribute("success") != null) {%>  
  13.             Upload Successfully!!!<br/>  
  14.         <% }%>  
  15.         <form id="loginform" name="loginform" method="POST" enctype="multipart/form-data">  
  16.             <table width="100%" border="0" cellspacing="0" cellpadding="0">  
  17.                 <tr>  
  18.                     <td height="30" align="right">Choose File</td>  
  19.                     <td align="left">  
  20.                         <input name="imageFile" type="file"/>  
  21.                     </td>  
  22.                 </tr>  
  23.                 <tr>  
  24.                     <td align="center" colspan="2">  
  25.                         <input type="submit" value="submit" name="submit" />  
  26.                     </td>  
  27.                 </tr>  
  28.             </table>  
  29.         </form>  
  30.     </body>  
  31. </html>  

注意:在文件上传Form表单中,一定要将enctype设置为"multipart/form-data"因为只有这样才能使Spring知道这是一个文件上传的请求。

细心的读者也许能发现Form表单中action为默认值也就是说post到和上传页面同样的URL,因此我们定义了一个Controller分别来处理这个请求的GET和POST请求。下面让我们来看看这个Controller:

1.我们通过@Controller声明这个类为Spring组件,告知Spring容器在初始化的时候需要加载该类实例到Spring Context Container中。
2.通过@RequestMapping("/sec_upload.do")将sec_upload.do的请求指向该Controller处理。
Java代码   收藏代码
  1. @Controller  
  2. @RequestMapping("/sec_upload.do")  
  3. public class UploadController {  
  4.     //...  
  5. }  

3.定义一个处理GET请求的方法,该方法简单的将选择文件Form表单页展现给用户:
Java代码   收藏代码
  1. @RequestMapping(method = RequestMethod.GET)  
  2.     public String handleUploadShow() {  
  3.         return "uploadView";  
  4.     }  

4.定义一个处理POST请求的方法,该方法进行用户文件上传处理:
Java代码   收藏代码
  1. @RequestMapping(method = RequestMethod.POST)  
  2.     public String handleUploadProcess(  
  3.             @RequestParam("imageFile") MultipartFile file, Model model)  
  4.             throws Exception {  
  5.         //具体的业务逻辑操作。。。  
  6.         model.addAttribute("success""true");  
  7.         return "uploadView";  
  8.     }  

通过@RequestParam("imageFile")注解,Spring会将request请求中的imageFile的文件信息自动绑定到MultipartFile对象。

上面的Controller方法解决的文件绑定的问题,但假设我们的Form表单中除了文件选择框还有其他一些用户填写的信息,那么我们怎么处理呢?仿照上面的方法,我们可以为多个参数提供多个@RequestParam注解来完成数据绑定工作,但我们也可以通过MultipartHttpServletRequest对象来获取这些信息,因为在DispatcherServlet中Spring已经将一个普通的HttpServletRequest转换为了一个MultipartHttpServletRequest:
Java代码   收藏代码
  1. @RequestMapping(method = RequestMethod.POST)  
  2. public String handleAnotherUploadProcess(  
  3.         MultipartHttpServletRequest request, Model model) throws Exception {  
  4.     MultipartFile file = request.getFile("imageFile");  
  5.     //request.getParameter("xxx");  
  6.     //request.getContentType();  
  7.     //request.getContentLength();  
  8.     //some other processing...  
  9.     model.addAttribute("success""true");  
  10.     return "uploadView";  
  11. }  


这种方式还是需要我们不断的通过request.getParameter("xxx")方式来获得参数,了解Spring MVC的同学可能想到了,使用CommandObject绑定-回答正确。假设我们定义了一个POJO对象:
Java代码   收藏代码
  1. public class BoUploadFile {  
  2.     private MultipartFile imageFile;  
  3.   
  4.     public MultipartFile getImageFile() {  
  5.         return imageFile;  
  6.     }  
  7.   
  8.     public void setImageFile(MultipartFile imageFile) {  
  9.         this.imageFile = imageFile;  
  10.     }  
  11.   
  12.     private String name;  
  13.   
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.   
  18.     public void setName(String name) {  
  19.         this.name = name;  
  20.     }  
  21. }  

这个对象中不仅包括需要封装的上传文件信息,还包括其他一些用户输入的普通信息。那么有了这个封装对象,我们的Controller可以变成如下的样子:
Java代码   收藏代码
  1. @RequestMapping(method = RequestMethod.POST)  
  2. public String handleThirdUploadProcess(BoUploadFile uploadFile, Model model) throws Exception{  
  3.     MultipartFile file = uploadFile.getImageFile();  
  4.     //这里你可以通过uploadFile.getName()...等等获取用户输入的其他普通信息了。  
  5.     model.addAttribute("success""true");  
  6.     return "uploadView";  
  7. }  


5.自定义一个文件验证类,来验证文件的合法性。
Java代码   收藏代码
  1. /** 
  2.  * 用户文件上传验证类 
  3.  *  
  4.  * @author Jacky Lau created at 2008-8-27 
  5.  * @since 1.0 
  6.  * @version 1.0 
  7.  */  
  8. public class MultipartFileValidator {  
  9.   
  10.     private final static long MAX_SIZE = 1024 * 1024;  
  11.   
  12.     /** 
  13.      * 文件大小上限 
  14.      */  
  15.     private long maxSize = MAX_SIZE;  
  16.   
  17.     /** 
  18.      * 可接受的文件content-type 
  19.      */  
  20.     private String[] allowedContentTypes;  
  21.   
  22.     @PostConstruct  
  23.     public void afterPropertiesSet() {  
  24.         Assert  
  25.                 .notEmpty(allowedContentTypes,  
  26.                         "The content types allowed to be uploaded must contain one at least!");  
  27.     }  
  28.   
  29.     /** 
  30.      * 验证上传文件是否合法,如果不合法那么会抛出异常 
  31.      *  
  32.      * @param file 
  33.      *            用户上传的文件封装类 
  34.      */  
  35.     public void validate(MultipartFile file) {  
  36.         Assert.notNull(file, "The multipart file is null!");  
  37.         if (file.getSize() > maxSize)  
  38.             throw new FileOutOfMaxLengthException("error.upload.outmaxlen",  
  39.                     new Object[] { maxSize },  
  40.                     "The file uploaded is out of max file size!");  
  41.         if (!ArrayUtils.contains(allowedContentTypes, file.getContentType()))  
  42.             throw new ContentTypeNotSupportException("error.upload.content.notsupported"null,  
  43.                     "The content type '"+file .getContentType()+"' is not a valid content type !");  
  44.     }  
  45.   
  46.     /** 
  47.      * 设置文件上传大小上限 
  48.      *  
  49.      * @param maxSize 
  50.      *            文件上传大小上限 
  51.      */  
  52.     public void setMaxSize(long maxSize) {  
  53.         this.maxSize = maxSize;  
  54.     }  
  55.   
  56.     /** 
  57.      * 设置可接受的文件content-type数组 
  58.      *  
  59.      * @param allowedContentTypes 
  60.      *            可接受的文件content-type数组 
  61.      */  
  62.     public void setAllowedContentTypes(String[] allowedContentTypes) {  
  63.         this.allowedContentTypes = allowedContentTypes;  
  64.     }  
  65.   
  66. }  


这样我们可以通过这个validator判断上传的文件是否超出了最大限制,文件格式是否正确等判断。我们可以通过配置文件配置该验证器,在这里为了方便起见在类中我用以下方式来初始化该验证器:
Java代码   收藏代码
  1. private MultipartFileValidator validator;  
  2.   
  3. @PostConstruct  
  4. public void init() {  
  5.     validator = new MultipartFileValidator();  
  6.     validator.setAllowedContentTypes(new String[] { "image/jpeg",  
  7.             "image/pjpeg" });  
  8. }  


至此,我们已经完成了文件上传的开发,可以看出这和普通的Controller开发没有任何区别,简单而且灵活。以下是该Controller的全部代码:

Java代码   收藏代码
  1. @Controller  
  2. @RequestMapping("/sec_upload.do")  
  3. public class UploadController {  
  4.   
  5.     private MultipartFileValidator validator;  
  6.   
  7.     @PostConstruct  
  8.     public void init() {  
  9.         validator = new MultipartFileValidator();  
  10.         validator.setAllowedContentTypes(new String[] { "image/jpeg",  
  11.                 "image/pjpeg" });  
  12.     }  
  13.   
  14.     @RequestMapping(method = RequestMethod.GET)  
  15.     public String handleUploadShow() {  
  16.         return "uploadView";  
  17.     }  
  18.   
  19.     @RequestMapping(method = RequestMethod.POST)  
  20.     public String handleUploadProcess(  
  21.             @RequestParam("imageFile") MultipartFile file, Model model)  
  22.             throws Exception {  
  23.         validator.validate(file);  
  24.         String path = "d:\\temp\\ftp\\" + file.getOriginalFilename();  
  25.         String resizePath = "d:\\temp\\ftp\\resize\\"  
  26.                 + file.getOriginalFilename();  
  27.         FileHelper.save(path, file.getBytes());  
  28.         if (ImageHelper.isJpg(ImageHelper.getImageType(path)))  
  29.             ImageHelper.resizeJPG(path, resizePath, 120118);  
  30.         model.addAttribute("success""true");  
  31.         return "uploadView";  
  32.     }  
  33. }  


在以后的文章中,我会对Spring进行上传文件特殊处理做一些探究,比如用户上传一个csv的通讯录文件,那么通过Spring的属性编辑器一个custom的Editor来进行数据转换,可以将CSV中的信息转换成其他你所需要的信息:比如从CSV文件中抽取邮件地址放到一个字符串数组中,让你可以进行后续的业务逻辑操作。。。



本文章的第二部分:

在前一篇文章我们通过强大并且易用的Spring MVC中的MultipartResolver简单快速的实现了文件的上传。

而在本文中让我们来讨论一下文件上传的另外一种情况,例如一个支持导入你的本地通讯录列表的Web Mail系统。那么这个Mail System可能不仅仅希望你上传的通讯录文件作为一个副本保存在服务器端,他还希望更进一步的了解你上传通讯录文件的内容,以便能够将这些内容添加到你的Web Mail通讯录中。好的,我们了解了这样一个需求,就让我们来看看Spring MVC是如何做到的。

首先,让我们来看一段Spring 2.5 Reference中文版中的话(详情请参考http://www.redsaga.com/spring_ref/2.5/html/mvc.html#mvc-multipart-forms):

引用
和其它不能自动转为字符串类型或者基本类型(primitive type)的属性一样,为了将上传的二进制数据存成bean的属性,必须通过ServletRequestDatabinder注册一个属性编辑器。 Spring中内置了几个这样的编辑器,它们可以处理文件,然后将结果存成bean的属性。 比如,StringMultipartEditor可以将文件转换成一个字符串(使用用户声明的字符集)。ByteArrayMultipartEditor可以以将文件转换为byte数组。 他们的功能和CustomDateEditor相似。


也就是说,我们可以通过一个特定的属性编辑器来讲得到的二进制数据转换成我们希望的数据类型,比如一个文本字符串或者提取其中的某些属性组成一个字符串列表,比如Email通讯录列表。

为了启用我们的PropertyEditor,那么我们就需要了解另外一个东东-WebDataBinder,通过它去调用PropertyEditor,我们可以将请求数据绑定成我们希望的数据对象。

初始化自定义WebDataBinder的方法有两种(详情请参考 http://www.redsaga.com/spring_ref/2.5/html/mvc.html#mvc-ann-webdatabinder):
1.使用@InitBinder注解;
Java代码   收藏代码
  1. @Controller  
  2. ublic class MyFormController {  
  3.   
  4.    @InitBinder  
  5.    public void initBinder(WebDataBinder binder) {  
  6.        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
  7.        dateFormat.setLenient(false);  
  8.        binder.registerCustomEditor(Date.classnew CustomDateEditor(dateFormat, false));  
  9.    }  
  10.   
  11.    // ...  


2.在配置文件中配置
Java代码   收藏代码
  1. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  2.     <property name="cacheSeconds" value="0" />  
  3.     <property name="webBindingInitializer">  
  4.         <bean class="org.springframework.samples.petclinic.web.ClinicBindingInitializer" />  
  5.     </property>  
  6. </bean>  


让我们继续导入通讯录列表的例子,首先让我们准备一个CSV邮件通讯录列表文件contacts.csv:
Java代码   收藏代码
  1. 名,姓,全名,昵称,邮箱地址  
  2. Jacky,Lau,Jacky Lau,,jacky@gmail.com  
  3. Grace,Fox,Grace Fox,,grace@gmail.com  
  4. Tom,Hanks,Tom Hanks,,tom@gmail.com  
  5. Charly,White,Charly White,,charly@gmail.com  


我们还需要一个类来表示好友邮件信息-Email.class:
Java代码   收藏代码
  1. public class Email {  
  2.     private String name;  
  3.   
  4.     private String email;  
  5.   
  6.     public String getName() {  
  7.         return name;  
  8.     }  
  9.   
  10.     public void setName(String name) {  
  11.         this.name = name;  
  12.     }  
  13.   
  14.     public String getEmail() {  
  15.         return email;  
  16.     }  
  17.   
  18.     public void setEmail(String email) {  
  19.         this.email = email;  
  20.     }  
  21. }  


接下来我们来准备一个自定义的Property Editor: CustomEmailDataEditor
Java代码   收藏代码
  1. public class CustomEmailDataEditor extends PropertyEditorSupport {  
  2.   
  3.     public void setValue(Object value) {  
  4.         if (value instanceof MultipartFile) {//处理上传文件,此处默认上传的是格式正确的CSV文件  
  5.             MultipartFile multipartFile = (MultipartFile) value;  
  6.             System.out.println(multipartFile.getContentType());//打印Content-Type  
  7.             try {  
  8.                 //使用第三方开源类库OpenCSV来读取CSV文件  
  9.                 CSVReader reader = new CSVReader(new InputStreamReader(  
  10.                         multipartFile.getInputStream()));  
  11.                 String[] nextLine;  
  12.                 // 去除第一行header信息  
  13.                 reader.readNext();  
  14.   
  15.                 List<Email> emails = new ArrayList<Email>();  
  16.                 while ((nextLine = reader.readNext()) != null) {  
  17.                     Email email = new Email();  
  18.                     email.setName(nextLine[2]);  
  19.                     email.setEmail(nextLine[4]);  
  20.                     emails.add(email);  
  21.                 }  
  22.                   
  23.                 //绑定数据列表  
  24.                 super.setValue(emails);  
  25.             } catch (IOException ex) {  
  26.                 throw new IllegalArgumentException(  
  27.                         "Cannot read contents of multipart file: "  
  28.                                 + ex.getMessage());  
  29.             }  
  30.         } else if (value instanceof byte[]) {  
  31.             super.setValue(value);  
  32.         } else {  
  33.             super.setValue(value != null ? value.toString().getBytes() : null);  
  34.         }  
  35.     }  
  36.   
  37. }  


在这段代码中使用到了一个开源的第三方类库OpenCSV来读取CSV文件,该类库的下载地址是 http://opencsv.sourceforge.net/

接下来,我们需要将自定义的Property Editor注册。我们通过在Controller类中@InitBinder注解的方式注册:
Java代码   收藏代码
  1. @InitBinder  
  2.     public void initBinder(WebDataBinder binder) {  
  3.         binder.registerCustomEditor(List.classnew CustomEmailDataEditor());  
  4.     }  


接下来让我们来看Controller方法:
Java代码   收藏代码
  1. @RequestMapping(method = RequestMethod.POST)  
  2. public String handleUploadProcess(BoUploadFile upload, Model model)  
  3.         throws Exception {  
  4.     System.out.println("Name is " + upload.getName());  
  5.     for (Email email : upload.getImageFile()) {  
  6.         System.out.println("Name : " + email.getName() + " | email : "  
  7.                 + email.getEmail());  
  8.     }  
  9.     model.addAttribute("success""true");  
  10.     return "uploadView";  
  11. }  


OK,到这里我们所有的工作已经准备完成,此处忽略了一些Spring MVC @Controller的配置,详情请参考上一篇文章 http://dakulaliu.iteye.com/blog/236235,在Eclipse中启动Tomcat 6.0 Server上传刚才我们创建的contacts.csv
文件,完成之后在控制台打印出上传文件的信息:
引用
application/vnd.ms-excel
Name is Contacts List
Name : Jacky Lau | email : jacky@gmail.com
Name : Grace Fox | email : grace@gmail.com
Name : Tom Hanks | email : tom@gmail.com
Name : Charly White | email : charly@gmail.com


好了-搞定-收工!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值