spring mvc利用MultipartResolver解析Multipart/form-data進行文件上傳


之前的表單數據都是文本數據,現記錄:利用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 >
View Code

 ② 修改表單頁面:

< 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 >
View Code

 ③ 處理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();
    }

 注意到輸入參數增加了@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是有講究的,當DispatcherServlet查找multipart解析器時,會查找id為multipartResolver的Bean。若定義的bean id 不是multipartResolver,DispatcherServlet會對它進行忽略。

可以畫成下圖進行理解:

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM