Flex 實現多檔案上傳之二:後台部分(java)

來源:互聯網
上載者:User
http://hi.baidu.com/half_cup_coffee/blog/item/d421001037f02307203f2e7c.html

在《Flex實現多檔案上傳之一:前台部分》裡介紹了使用flex實現多檔案上傳,一直沒時間把後面的放上去。

這裡說一下後台是如何處理上傳檔案的,用的是java。

上傳的功能使用了apache的通用jar包,可以在這裡下載。

java代碼如下,簡單說明下:

1.設定上傳目錄,這個目錄是指web伺服器所在機器的目錄
2.防止亂碼的設定
3.上傳功能寫在action裡,樣本直接使用了預設的方法名doPost,如果你要用struts之類的,可以定義自己的方法名稱 Java 代碼

  1. package com.nstar.orderexpress.web;  
  2.   
  3. import javax.servlet.http.HttpServlet;  
  4. import org.apache.log4j.Logger;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7. import javax.servlet.ServletException;  
  8. import org.apache.commons.fileupload.disk.DiskFileItemFactory;  
  9. import org.apache.commons.fileupload.servlet.ServletFileUpload;  
  10. import java.util.List;  
  11. import org.apache.commons.fileupload.FileUploadException;  
  12. import java.util.Iterator;  
  13. import org.apache.commons.fileupload.FileItem;  
  14. import java.io.File;  
  15. import java.io.UnsupportedEncodingException;  
  16.   
  17. import org.apache.commons.lang.ObjectUtils;  
  18.   
  19. public class FileUploadAction extends HttpServlet{  
  20.     private static Logger logger = Logger.getLogger(FileUploadAction.class.getName());  
  21.   
  22.     private int maxPostSize = 100 * 1024 * 1024;  
  23.     private String uploadPath = "D:\\upload\\";  
  24.   
  25.   
  26.     public FileUploadAction() {  
  27.     }  
  28.   
  29.     //檔案上傳  
  30.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  31.         throws ServletException {  
  32.         logger("begin upload");  
  33.         try {  
  34.     request.setCharacterEncoding("UTF-8");//防止文 件名稱帶有漢字後傳到伺服器亂碼  
  35.    } catch (UnsupportedEncodingException e) {  
  36.     // TODO Auto-generated catch block  
  37.     e.printStackTrace();  
  38.    }  
  39.         response.setContentType("text/html; charset=UTF-8");  
  40.         DiskFileItemFactory factory = new DiskFileItemFactory();  
  41.         factory.setSizeThreshold(1024 * 4);  
  42.         ServletFileUpload upload = new ServletFileUpload(factory);  
  43.         upload.setFileSizeMax(maxPostSize);  
  44.   
  45.         logger("request================"+ObjectUtils.toString(request));  
  46.         List fileItems = null;  
  47.         try {  
  48.             fileItems = upload.parseRequest(request);  
  49.             logger("===================="+ObjectUtils.toString(fileItems));  
  50.             Iterator iter = fileItems.iterator();  
  51.             while (iter.hasNext()) {  
  52.                 FileItem item = (FileItem) iter.next();  
  53.                 log(item.toString());  
  54.                 if (!item.isFormField()) {  
  55.                     String name = item.getName();  
  56.                     logger("file name = "+name);  
  57.                     try {  
  58.                         item.write(new File(uploadPath + name));  
  59.                     }  
  60.                     catch (Exception ex) {  
  61.                         logger(ex.getMessage());  
  62.                     }  
  63.                 }  
  64.             }  
  65.         }  
  66.         catch (FileUploadException ex1) {  
  67.             logger("FileUploadException->"+ex1.getMessage());  
  68.         }  
  69.   
  70.   
  71.     }  
  72.   
  73.     private void logger(String info){  
  74.         System.out.println(info);  
  75.     }  
  76.   
  77. }  
package com.nstar.orderexpress.web;  import javax.servlet.http.HttpServlet; import org.apache.log4j.Logger; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import java.util.List; import org.apache.commons.fileupload.FileUploadException; import java.util.Iterator; import org.apache.commons.fileupload.FileItem; import java.io.File; import java.io.UnsupportedEncodingException;  import org.apache.commons.lang.ObjectUtils;  public class FileUploadAction extends HttpServlet{     private static Logger logger = Logger.getLogger(FileUploadAction.class.getName());      private int maxPostSize = 100 * 1024 * 1024;     private String uploadPath = "D:\\upload\\";       public FileUploadAction() {     }      //檔案上傳     public void doPost(HttpServletRequest request, HttpServletResponse response)         throws ServletException {         logger("begin upload");         try {     request.setCharacterEncoding("UTF-8");//防止檔案名稱帶有漢字後傳到伺服器亂碼    } catch (UnsupportedEncodingException e) {     // TODO Auto-generated catch block     e.printStackTrace();    }         response.setContentType("text/html; charset=UTF-8");         DiskFileItemFactory factory = new DiskFileItemFactory();         factory.setSizeThreshold(1024 * 4);         ServletFileUpload upload = new ServletFileUpload(factory);         upload.setFileSizeMax(maxPostSize);          logger("request================"+ObjectUtils.toString(request));         List fileItems = null;         try {             fileItems = upload.parseRequest(request);             logger("===================="+ObjectUtils.toString(fileItems));             Iterator iter = fileItems.iterator();             while (iter.hasNext()) {                 FileItem item = (FileItem) iter.next();                 log(item.toString());                 if (!item.isFormField()) {                     String name = item.getName();                     logger("file name = "+name);                     try {                         item.write(new File(uploadPath + name));                     }                     catch (Exception ex) {                         logger(ex.getMessage());                     }                 }             }         }         catch (FileUploadException ex1) {             logger("FileUploadException->"+ex1.getMessage());         }       }      private void logger(String info){         System.out.println(info);     }  } 

檔案上傳基本上就這麼多了,可以在此基礎上去擴充你想要的功能。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.