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 代碼
- 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);
- }
-
- }
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); } }
檔案上傳基本上就這麼多了,可以在此基礎上去擴充你想要的功能。