1. 程式人生 > >springmvc 單檔案和多檔案上傳,控制上傳檔案的型別

springmvc 單檔案和多檔案上傳,控制上傳檔案的型別

package com.xiangshuai.controller;

import java.io.File; import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile;

/***  *   * @author xiangshuai  * springmvc 單和多檔案上傳  * 所需額外JAR 包 : commons-fileupload-1.2.2.jar 和  commons-io-1.3.2.jar  * dispatcherServlet-servlet.xml 中需配置如下bean:  *     <!-- 配置MultipartResolver 用於檔案上傳 使用spring的CommosMultipartResolver -->   <bean id="multipartResolver"           class="org.springframework.web.multipart.commons.CommonsMultipartResolver">           <!-- 上傳檔案大小上限,單位為位元組(10MB) -->         <property name="maxUploadSize">               <value>10485760</value>           </property>           <!-- 請求的編碼格式,必須和jSP的pageEncoding屬性一致,以便正確讀取表單的內容,預設為ISO-8859-1 -->         <property name="defaultEncoding">             <value>UTF-8</value>         </property>     </bean>  * 專案所在地: E:\學習文件子目錄壓縮\框架\springmvc\檔案上傳\springmvc2.zip 和  * 我的網盤\我的筆記\學習文件子目錄壓縮\框架\springmvc\檔案上傳\springmvc2.zip  */ @Controller @RequestMapping("/upLoad") public class FileUpload {     @RequestMapping("/dFileLoad")     //用 MultipartFile 來接受上傳的檔案     public String dFileLoad(@RequestParam("dUploadFile") MultipartFile file,HttpServletRequest request){         try {             if(file.getSize()>0){                 String filename = file.getOriginalFilename();                 //限制檔案上傳的型別                 if(filename.endsWith("jpg")||filename.endsWith(".doc")||filename.endsWith(".docx")){                     File dir = new File("D:"+File.separatorChar+"temp");                     if(!dir.exists()){                         dir.mkdir();                     }                     System.out.println(dir.getAbsolutePath());                     File mbfile= new File(dir.getAbsolutePath()+File.separatorChar+filename);                     //將 file 複製給 mbfile                     file.transferTo(mbfile);                 }else {                     request.setAttribute("filename", file.getOriginalFilename());                     request.setAttribute("message1", "檔案必須以 jpg或doc或docx開頭");                     return "fileNull";                 }             }else{                 request.setAttribute("filename", file.getOriginalFilename());                 return "fileNull";             }                      } catch (IllegalStateException e) {             // TODO Auto-generated catch block             e.printStackTrace();         } catch (IOException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }          return "model";     }      /**      *       * @param loginName  : 用來建立在temp目錄下 的子目錄名       * @param files  :接受檔案陣列      * @param request      * @return      */     @RequestMapping("/duoFilesLoad")     //用 MultipartFile[]  來接受上傳的多個檔案     public String duoFilesLoade(@RequestParam("loginName") String loginName,@RequestParam("dUploadFile") MultipartFile[] files,HttpServletRequest request){         try {                 File dir = new File("D:"+File.separatorChar+"temp"+File.separatorChar+loginName);                 if(!dir.exists()){                     dir.mkdirs();                 }                 boolean flag=true;                 for (int i=0;i<files.length; i++) {                     MultipartFile file=files[i];                     String filename = file.getOriginalFilename();                     if(filename!=null && !"".equals(filename.trim())){                         flag=false;                         if(filename.endsWith("jpg")||filename.endsWith(".doc")||filename.endsWith(".docx")){                             System.out.println(dir.getAbsolutePath());                             File mbfile= new File(dir.getAbsolutePath()+File.separatorChar+filename);                             //將 file 複製給 mbfile                             file.transferTo(mbfile);                         }else {                             request.setAttribute("filename", file.getOriginalFilename());                             request.setAttribute("message1", "檔案必須以 jpg或doc或docx開頭");                             return "fileNull";                         }                     }                                      }                 // 如果一個檔案也沒有上傳                 if(flag){                     return "fileNull";                 }         } catch (IllegalStateException e) {             // TODO Auto-generated catch block             e.printStackTrace();         } catch (IOException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }         return "model";     }  }