---
title: "Flex上传下载"
author: "星海孤舟"
date: "2011-04-27T09:18:33Z"
tags: [".net", "Cache", "Flash", "Flex", "Windows"]
categories: ["Web前端"]
slug: "flex"
draft: false
lastmod: "2011-04-27T09:18:33Z"
id: 1019253
url: "https://keith3321.iteye.com/blog/1019253"
---

在项目开发过程中，经常会遇到文件上传下载的问题。但在flex中由于安全沙箱的原因，flex程序不能直接访问本地文件。但是flex SDK中提供了FileReference和FileReferenceList两个类，可以通过这两个类来实现，这两个类位于flash.net包中。  
一、 文件上传  
上传文件flex端处理：  
1、上传文件第一步——打开文件。  
首先需要创建一个FileReference对象：  
private var myFileReference:FileReference = new FileReference();  
在FileReference中有一个browse()函数，这个函数的作用就是打开一个windows文件选择框，可以选择需要上传的文件。在打开的文件选择框中可以指定选择文件的类型，这是通过FileFilter来实现的。创建一个FileFilter对象，作为browse函数的参数，这样就可以指定选择文件的类型了：  
var imagesFilter:FileFilter = new FileFilter("Images(\*.jpg,\*.gif,\*.png)", "\*.jpg;\*.gif;\*.png");  
myFileReference.browse(\[imagesFilter]);  
2、上传文件第二步——上传文件  
文件选择了之后会触发Event.SELECT事件，所以需要在myFileReference对象上注册SELECT事件的监听：  
myFileReference.addEventListener(Event.SELECT, onSelect);  
onSelect是一个监听函数，在这个函数中可以得到选择的文件的名字。由于安全沙箱的原因，只能得到选择的文件名，不能得到完整的文件路径。选择了需要上传的文件后，需要调用upload()函数上传选择的文件：  
myFileReference.upload(uploadURL,myFileReference.name);  
upload的参数uploadURL是一个URLRequest对象，这个对象指定了上传文件用的URL，myFileReference.name就是选择的文件的文件名。  
3、上传文件第三步——上传进度  
一般情况下，在上传文件的过程中需要显示一个表示上传进度的进度条，这样可以有一个很好的用户体验。要想显示上传进度，可以有两种方式。第一种就是监听FileReference对象的process事件，在process事件的监听函数中可以得到上传的进度，然后可以设置进度条的进度。第二种方式就是把FileReference对象与进度条对象进行绑定，进度条会自动监听FileReference对象的process事件，来显示上传进度。  
4、上传文件第四步——上传结束  
有的时候需要在文件上传结束后给用户一个提示，告诉用户文件已经上传结束了。这就必须得对FileReference对象的COMPLETE事件进行监听，当文件上传结束后，会触发COMPLETE事件。用户提示或者文件上传结束后的处理需要在这个事件的监听函数中实现。  
文件上传服务器端处理：  
FileReference对象的upload函数上传到服务器端的格式如下：  
POST /handler.cfm HTTP/1.1  
Accept: text/*  
Content-Type: multipart/form-data;  
boundary=----------Ij5ae0ae0KM7GI3KM7ei4cH2ei4gL6  
User-Agent: Shockwave Flash  
Host: www.example.com  
Content-Length: 421  
Connection: Keep-Alive  
Cache-Control: no-cache  
\------------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7  
Content-Disposition: form-data; name="Filename"  
MyFile.jpg  
\------------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7  
Content-Disposition: form-data; name="photo"; filename="MyFile.jpg"  
Content-Type: application/octet-stream  
上传文件的内容  
\------------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7  
Content-Disposition: form-data; name="Upload"  
Submit Query  
\------------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7—  
服务器端只需要解析这个结构，把“上传文件内容”部分解析出来，然后保存成文件就可以了。文件名就是MyFile.jpg。  
服务器端java代码：  
view plaincopy to clipboardprint?  
private boolean upLoad(HttpServletRequest request) throws Exception {    
   
    int len = -1;    
    byte\[] bt = new byte\[4096];    
    String sperator = null;    
    String fileName = null;    
    String savePath = "/";    
    //控制上传文件的大小    
    long fileSize = -1;    
   
    request.setCharacterEncoding("utf-8");    
    ServletInputStream in = request.getInputStream();    
        
    len = in.readLine(bt, 0, bt.length);    
    if (len != -1) {    
        sperator = new String(bt, 0, len);    
        System.out.println(sperator);    
        speratorsperator = sperator.substring(0, 28);    
        System.out.println(sperator);    
        len = -1;    
    }    
   
    do {    
        len = in.readLine(bt, 0, bt.length);    
        String s = new String(bt, 0, len);    
        System.out.println(s);    
        int index = s.indexOf("filename=\\"");    
        if (index != -1) {    
            ss = s.substring(index + 10);    
            index = s.indexOf("\\"");    
            ss = s.substring(0, index);    
            fileName = s;    
            len = -1;    
        }    
    } while (len != -1);    
   
    FileOutputStream out = new FileOutputStream(savePath + fileName);    
   
    len = in.readLine(bt, 0, bt.length);    
    String s = new String(bt, 0, len);    
    System.out.println(s);    
    int i = s.indexOf("Content-Type:");    
    if (i == -1) {    
        System.out.print("is not file");    
        return false;    
    } else {    
        in.readLine(bt, 0, bt.length); // delete one line space    
        len = -1;    
    }    
   
    long trancsize = 0;    
    try {    
        len = in.readLine(bt, 0, bt.length);    
        while (len != -1) {    
            s = new String(bt, 0, len);    
            if (s.length() &gt; 28) {    
                ss = s.substring(0, 28);    
                if (s.equals(sperator)) {    
                    System.out.println(s);    
                    break;    
                }    
            }    
   
            if (fileSize != -1) {    
                if (trancsize &gt;= fileSize) {    
                    throw new Exception("file is too big");    
                }    
            }    
            out.write(bt, 0, len);    
            trancsize += len;    
            len = in.readLine(bt, 0, bt.length);    
        }    
   
        out.close();    
        return true;    
    } catch (Exception e) {    
        out.close();    
        File tmp = new File(savePath, fileName);    
        if (!tmp.delete()) {    
            System.out.println(e.getMessage() + ",clear error");    
        }    
        return false;    
    }    
}   
private boolean upLoad(HttpServletRequest request) throws Exception {  
 int len = -1;  
 byte\[] bt = new byte\[4096];  
 String sperator = null;  
 String fileName = null;  
 String savePath = "/";  
 //控制上传文件的大小  
 long fileSize = -1;  
 request.setCharacterEncoding("utf-8");  
 ServletInputStream in = request.getInputStream();  
   
 len = in.readLine(bt, 0, bt.length);  
 if (len != -1) {  
  sperator = new String(bt, 0, len);  
  System.out.println(sperator);  
  sperator = sperator.substring(0, 28);  
  System.out.println(sperator);  
  len = -1;  
 }  
 do {  
  len = in.readLine(bt, 0, bt.length);  
  String s = new String(bt, 0, len);  
  System.out.println(s);  
  int index = s.indexOf("filename=\\"");  
  if (index != -1) {  
   s = s.substring(index + 10);  
   index = s.indexOf("\\"");  
   s = s.substring(0, index);  
   fileName = s;  
   len = -1;  
  }  
 } while (len != -1);  
 FileOutputStream out = new FileOutputStream(savePath + fileName);  
 len = in.readLine(bt, 0, bt.length);  
 String s = new String(bt, 0, len);  
 System.out.println(s);  
 int i = s.indexOf("Content-Type:");  
 if (i == -1) {  
  System.out.print("is not file");  
  return false;  
 } else {  
  in.readLine(bt, 0, bt.length); // delete one line space  
  len = -1;  
 }  
 long trancsize = 0;  
 try {  
  len = in.readLine(bt, 0, bt.length);  
  while (len != -1) {  
   s = new String(bt, 0, len);  
   if (s.length() &gt; 28) {  
    s = s.substring(0, 28);  
    if (s.equals(sperator)) {  
     System.out.println(s);  
     break;  
    }  
   }  
   if (fileSize != -1) {  
    if (trancsize &gt;= fileSize) {  
     throw new Exception("file is too big");  
    }  
   }  
   out.write(bt, 0, len);  
   trancsize += len;  
   len = in.readLine(bt, 0, bt.length);  
  }  
  out.close();  
  return true;  
 } catch (Exception e) {  
  out.close();  
  File tmp = new File(savePath, fileName);  
  if (!tmp.delete()) {  
   System.out.println(e.getMessage() + ",clear error");  
  }  
  return false;  
 }  
}  
   
代码补充说明：  
解析request内容，把文件内容解析出来，然后保存成文件，文件名可以从request内容中解析出来。上传文件时如果想控制上传文件的大小的话就设定fileSize变量的值来进行控制。  
二、文件下载  
文件下载相对来说是比较简单的。只需三步就可以完成。  
1、创建一个URLRequest对象  
var downLoadURL:URLRequest = new URLRequest();  
2、指定下载文件的URL  
downLoadURL.url = "http://localhost:8080/FlexRemotObject/download";  
3、执行FileReference对象的download函数。download函数的第一个参数就是在1中创建的URLRequest对象，第二个参数是下载的文件的默认文件名。  
myFileReference.download(downLoadURL,"image.png");  
以上三步执行完毕后，FileReference对象会打开一个文件保存对话框，指定一个路径后，点击保存按钮，文件就下载完成了。  
文件下载Java端代码：  
view plaincopy to clipboardprint?  
private void downLoad(HttpServletResponse response) throws Exception {    
   
    BufferedOutputStream bos = null;    
    BufferedInputStream  bis = null;    
    try {    
            bis = new BufferedInputStream(new FileInputStream("/head.PNG"));                
            bos = new BufferedOutputStream(response.getOutputStream());    
                
            byte\[] buff = new byte\[2048];    
            int bytesRead;    
   
            while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {    
                bos.write(buff,0,bytesRead);    
            }    
        } catch(final IOException e) {    
            e.printStackTrace();    
        } catch(Exception e) {    
            e.printStackTrace();    
        }finally {    
            if (bis != null)    
                bis.close();    
            if (bos != null)    
            {    
                bos.flush();    
                bos.close();    
                bos=null;    
            }    
        }    
        response.flushBuffer();    
}   
private void downLoad(HttpServletResponse response) throws Exception {  
 BufferedOutputStream bos = null;  
 BufferedInputStream  bis = null;  
 try {  
            bis = new BufferedInputStream(new FileInputStream("/head.PNG"));           
            bos = new BufferedOutputStream(response.getOutputStream());  
             
            byte\[] buff = new byte\[2048];  
            int bytesRead;  
            while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
                bos.write(buff,0,bytesRead);  
            }  
        } catch(final IOException e) {  
         e.printStackTrace();  
        } catch(Exception e) {  
         e.printStackTrace();  
        }finally {  
            if (bis != null)  
                bis.close();  
            if (bos != null)  
            {  
                bos.flush();  
                bos.close();  
                bos=null;  
            }  
        }  
        response.flushBuffer();  
}  
结束语：如果想实现同时上传多个文件，那么可以使用FileReferenceList类来实现。  
完整代码下载地址：http://download.csdn.net/source/1815266  
本文来自CSDN博客，转载请标明出处：http://blog.csdn.net/To9m/archive/2009/11/10/4796105.aspx