---
title: "【转】flex利用Blazeds上传文件"
author: "zyn010101"
date: "2011-03-29T12:43:53Z"
tags: [".net", "Adobe", "Flex", "Web", "XML"]
categories: ["Web前端"]
slug: "flex-blazeds"
draft: false
lastmod: "2011-03-29T12:43:53Z"
id: 978573
url: "https://zyn010101.iteye.com/blog/978573"
---

Flex 利用 Blazeds上传文件 收藏  
       用Flex做业务经常会碰到各种类型的文件上传，如FTP上传、Http上传或者WEB服务形式上传，本文说的利用Blazeds上传文件有点类似Http上传，其原理就是客户端发送byte流，然后服务器端flex.messaging.MessageBrokerServlet接收byte流并生成文件。阅读本文最好了解Flex RemoteObject的使用，了解Blazeds的service调用。  
       1、新建Blazeds的文件上传服务，其实就是一个普通的Java类

Public class FileUpDownloadService{

    /\**  
     *  
     * &lt;p&gt;Description:文件上传&lt;/p&gt;  
     * @param content，文件内容  
     * @param fileType，文件类型  
     * @return  
     * @throws Exception  
     * @author Marcus  
     * @date 2010-8-31 下午03:57:10  
     \*/  
    public void uploadFile(byte\[] content, String fileType)throws Exception{  
       File file = new File(文件名+ fileType);  
       //write file  
       FileOutputStream stream = new FileOutputStream(file);  
       stream.write(content);  
       stream.close();  
    }  
}  
 

       2、配置remoting-config.xml文件

    &lt;destination id="fileUpDownloadService" channels="my-amf"&gt; 

       &lt;properties&gt;  
    &lt;source&gt;FileUpDownloadService&lt;/source&gt;  
           &lt;scope&gt;application&lt;/scope&gt;  
       &lt;/properties&gt;  
    &lt;/destination&gt;  
 

 

    3、flex客户端使用FileRefrence、remoteobject组件进行文件上传  
    1）FileRefrence用于文件选择；  
    2)remoteobject用户文件上传  
&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;s:Application xmlns:fx="[http://ns.adobe.com/mxml/2009](http://ns.adobe.com/mxml/2009)"  
              xmlns:s="library://ns.adobe.com/flex/spark"  
              xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="800" minHeight="600" creationComplete="init()" width="370" height="180"&gt;  
    &lt;fx:Script&gt;  
       &lt;![CDATA[  
           import mx.controls.Alert;  
           import mx.rpc.events.FaultEvent;  
           import mx.rpc.events.ResultEvent;  
           private var file:FileReference = new FileReference();  
            
           protected function ro\_resultHandler(event:ResultEvent):void  
           {  
              Alert.show("文件上传成功!");  
           }  
            
           protected function init():void{  
              //选择文件后，加载文件  
              file.addEventListener(Event.SELECT, selectHandler);  
           }  
            
           private function selectHandler(event:Event):void {  
              file.load();  //加载文件  
           }  
 

           protected function ro\_faultHandler(event:FaultEvent):void  
           {  
              Alert.show("文件上传失败，"+event.fault.faultString);  
           }  
 

           protected function btnBrows\_clickHandler(event:MouseEvent):void  
           {  
              file.browse();  
           }  
 

           protected function btnUpload\_clickHandler(event:MouseEvent):void  
           {  
              ro.uploadFile(file.data, file.type);  
           }  
       ]]&gt;  
    &lt;/fx:Script&gt;  
    &lt;fx:Declarations&gt;  
       &lt;s:RemoteObject id="ro" destination="fileUpDownloadService"  
                     result="ro\_resultHandler(event)" fault="ro\_faultHandler(event)"/&gt;  
    &lt;/fx:Declarations&gt;  
     
    &lt;s:Button id="btnBrows" click="btnBrows\_clickHandler(event)" label="选择文件" x="57" y="60"/&gt;  
    &lt;s:Button id="btnUpload" click="btnUpload\_clickHandler(event)" label="上传" x="145" y="60"/&gt;  
&lt;/s:Application&gt;  
 

本文来自CSDN博客，转载请标明出处：[http://blog.csdn.net/chuangxin/archive/2010/09/13/5881758.aspx](http://blog.csdn.net/chuangxin/archive/2010/09/13/5881758.aspx)