Flex 2实现文件上传

Flex 2实现文件上传

                                      

 

1.      环境的安装以及配置就不说了,网上很多地方可以找到。(我的是:JDK1.4.2,Flex Builder 2,Flex 2 SDK,Tomcat 4.1,Eclips3.0.1)。
2.      首先在Eclips中创建一个tomcat工程,例如取名为FileUpload。
3.      找到Flex SDK安装目录,将flex.war拷贝出来更名为flex.rar。解开这个包。将里面的META-INF 以及WEB-INF文件夹拷贝到Eclips的工作目录(我的是:d:workspaces)----即刚才创建的FileUpload目录下。
4.      Flex Builder 2下创建一个新的工程。具体步骤如下图。




5.      工程中引入common-fileupload-1.1.1.jar以及common-io-1.2.jar(没有的话去http://www.apache.org下载)。
6.      编写上传servlet  myUpload.java  代码如下(上传的文件存放在D:\\upload\\,没有的话请创建):

存放在../src/com/fileupload

package com.fileupload;

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 myUpload extends HttpServlet {

     private String uploadPath = "D:\\upload\\";
     private int maxPostSize = 100 * 1024 * 1024;
     
     public void doPost(HttpServletRequest req, HttpServletResponse res)
                 throws ServletException, IOException {
           res.setContentType("text/html; charset=UTF-8");
           
           DiskFileItemFactory factory = new DiskFileItemFactory();
           factory.setSizeThreshold(4096);

           ServletFileUpload upload = new ServletFileUpload(factory);
           upload.setSizeMax(maxPostSize);
           try {
                 List fileItems = upload.parseRequest(req);
                 Iterator iter = fileItems.iterator();
                 while (iter.hasNext()) {
                       FileItem item = (FileItem) iter.next();
                       if (!item.isFormField()) {
                             String name = item.getName();
                             try {
                             item.write(new File(uploadPath + name));
                             } catch (Exception e) {
                                   e.printStackTrace();
                             }
                       }
                 }
           } catch (FileUploadException e) {
                 e.printStackTrace();
           }

     }
}

 
7.      在web.xml中加入如下代码。(用于调用servlet)

   
       myUpload
       File Upload Servlet
       File Servlet Example
       com.fileupload.myUpload
   

   
       myUpload
       /myUpload
   

8.前台的FileUpload.mxml文件代码如下:

http://www.adobe.com/2006/mxml"
     xmlns="*" creationComplete="init()">
     


           private var currentAction:String;
             private var uploadURL:URLRequest;
             private var file:FileReference;
             
             private var fileName:String;

                 private function init() : void{
               file = new FileReference();
           }
           
             public function FileReference_browse() : void {
                  currentAction = "upload";
                 uploadURL = new URLRequest();
                 file = new FileReference();
                 configureListeners(file);
                 file.browse();
             }

             private function configureListeners(dispatcher:IEventDispatcher):void {
                 dispatcher.addEventListener(Event.SELECT, selectHandler);
             }
           
           private function selectHandler(event:Event):void {
                 var file:FileReference = FileReference(event.target);
                 if(currentAction == "upload"){
                      uploadURL.url = "myUpload?path=work&filename=" + file.name;
                      file.upload(uploadURL);
                 }
             }

           ]]>

9.启动Tomcat,发布成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值