flex結合servlet檔案上傳

modun1986發表於2009-06-01
客戶端------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="addListener()">
	<mx:Script>
		<![CDATA[
			private var file:FileReference = new FileReference();
			private function addListener():void{
				file.addEventListener(Event.SELECT,opption);
				file.addEventListener(Event.COMPLETE,opption);
				file.addEventListener(ProgressEvent.PROGRESS,opption);
				file.addEventListener(IOErrorEvent.IO_ERROR,opption);
				
			}
			private function opption(e:Event):void{
				if(e.type == Event.SELECT){
					filename.text = "選擇了檔案:"+file.name;
				}else if(e.type == Event.COMPLETE){
					filename.text = "上傳完畢";
				}else if(e.type == ProgressEvent.PROGRESS){
					filename.text = "已上傳: "+Math.round(100*(ProgressEvent(e).bytesLoaded/ProgressEvent(e).bytesTotal))+"%";
				}else if(e.type == IOErrorEvent.IO_ERROR){
					filename.text = "遠端伺服器未開啟或出現異常,請稍後重試";
					
				}
			}
			private function myUpload():void{
				var url:String = "http://localhost:8080/flexLogin/servlet/FileUpload";
				var urlRequest:URLRequest = new URLRequest(url);
				file.upload(urlRequest);
			}
		]]>
	</mx:Script>
	<mx:Canvas width="436" height="200" backgroundColor="#FFFFFF">
		<mx:Text x="10" y="10" text="請選擇要上傳的檔案" fontWeight="bold" fontSize="12" id="filename"/>
		<mx:Button x="10" y="38" label="瀏覽檔案" fontSize="12" id="search" click="file.browse()"/>
		<mx:Button x="96" y="40" label="上傳" id="upload" click="myUpload()"/>
	</mx:Canvas>
	
</mx:Application>

 

 

伺服器端---------------------------------------------------------------
package cn.zhangxuemin.Login;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileUpload extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public FileUpload() {
		super();
	}
	public void destroy() {
		super.destroy(); 
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String path = this.getServletConfig().getServletContext().getRealPath("/")+"upload\\";
		
		int maxPostSize = 100 * 1024 * 1024;
		DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(4096);
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setSizeMax(maxPostSize);
        upload.setHeaderEncoding("utf-8");

        try {
        	@SuppressWarnings("unchecked")
            List<FileItem> fileItems = upload.parseRequest(request);
            Iterator<FileItem> iter = fileItems.iterator();
            while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();
                if (!item.isFormField()) {
                    String name = item.getName();
                    System.out.println(path+name);
                    try {
                        item.write(new File(path + name));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
            System.out.println(e.getMessage() + "結束");
        }
    }


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

	public void init() throws ServletException {
		
	}

}

 

相關文章