Spring中MultipartHttpServletRequest實現文件上傳


轉貼自:http://zhupan.iteye.com/blog/26427

實現圖片上傳
  用戶必須能夠上傳圖片,因此需要文件上傳的功能。比較常見的文件上傳組件有Commons FileUpload(http://jakarta.apache.org/commons/fileupload/a>)和COS FileUpload(http://www.servlets.com/cos),Spring已經完全集成了這兩種組件,這里我們選擇Commons FileUpload。
  由於Post一個包含文件上傳的Form會以multipart/form-data請求發送給服務器,必須明確告訴DispatcherServlet如何處理MultipartRequest。首先在dispatcher-servlet.xml中聲明一個MultipartResolver:

xml 代碼
  1. <bean id="multipartResolver"  
  2.     class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  3.     <!-- 設置上傳文件的最大尺寸為1MB -->  
  4.     <property name="maxUploadSize">  
  5.         <value>1048576</value>  
  6.     </property>  
  7. </bean>  

 這樣一旦某個Request是一個MultipartRequest,它就會首先被MultipartResolver處理,然后再轉發相應的Controller。
在UploadImageController中,將HttpServletRequest轉型為MultipartHttpServletRequest,就能非常方便地得到文件名和文件內容:

java 代碼
  1. public ModelAndView handleRequest(HttpServletRequest request,   
  2.             HttpServletResponse response) throws Exception {   
  3.         // 轉型為MultipartHttpRequest:   
  4.         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;   
  5.         // 獲得文件:   
  6.         MultipartFile file = multipartRequest.getFile(" file ");   
  7.         // 獲得文件名:   
  8.         String filename = file.getOriginalFilename();   
  9.         // 獲得輸入流:   
  10.         InputStream input = file.getInputStream();   
  11.         // 寫入文件   
  12.   
  13.         // 或者:   
  14.         File source = new File(localfileName.toString());   
  15.         multipartFile.transferTo(source);   
  16.     }  

生成縮略圖 (目錄)
  當用戶上傳了圖片后,必須生成縮略圖以便用戶能快速瀏覽。我們不需借助第三方軟件,JDK標准庫就包含了圖像處理的API。我們把一張圖片按比例縮放到120X120大小,以下是關鍵代碼:

java 代碼
  1. public static void createPreviewImage(String srcFile, String destFile) {   
  2.         try {   
  3.             File fi = new File(srcFile); // src   
  4.             File fo = new File(destFile); // dest   
  5.             BufferedImage bis = ImageIO.read(fi);   
  6.   
  7.             int w = bis.getWidth();   
  8.             int h = bis.getHeight();   
  9.             double scale = (double) w / h;   
  10.             int nw = IMAGE_SIZE; // final int IMAGE_SIZE = 120;   
  11.             int nh = (nw * h) / w;   
  12.             if (nh > IMAGE_SIZE) {   
  13.                 nh = IMAGE_SIZE;   
  14.                 nw = (nh * w) / h;   
  15.             }   
  16.             double sx = (double) nw / w;   
  17.             double sy = (double) nh / h;   
  18.   
  19.             transform.setToScale(sx, sy);   
  20.             AffineTransformOp ato = new AffineTransformOp(transform, null);   
  21.             BufferedImage bid = new BufferedImage(nw, nh,   
  22.                     BufferedImage.TYPE_3BYTE_BGR);   
  23.             ato.filter(bis, bid);   
  24.             ImageIO.write(bid, " jpeg ", fo);   
  25.         } catch (Exception e) {   
  26.             e.printStackTrace();   
  27.             throw new RuntimeException(   
  28.                     " Failed in create preview image. Error:  "  
  29.                             + e.getMessage());   
  30.         }   
  31.     }  


免責聲明!

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



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