`
wuxiao_v
  • 浏览: 44070 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Flex4 利用 Blazeds上传文件

阅读更多
  用Flex做业务经常会碰到各种类型的文件上传,如FTP上传、Http上传或者WEB服务形式上传,本文说的利用Blazeds上传文件有点类似Http上传,其原理就是客户端发送byte流,然后服务器端flex.messaging.MessageBrokerServlet接收byte流并生成文件。阅读本文最好了解Flex RemoteObject的使
用,了解Blazeds的service调用。
       1、新建Blazeds的文件上传服务,其实就是一个普通的Java类
Public class FileUpDownloadService{
    /**
     *
     * <p>Description:文件上传</p>
     * @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文件
 <destination id="fileUpDownloadService" channels="my-amf"> 
       <properties>
    <source>FileUpDownloadService</source>
           <scope>application</scope>
       </properties>
    </destination>

3、flex客户端使用FileRefrence、remoteobject组件进行文件上传
    1)FileRefrence用于文件选择;
    2)remoteobject用户文件上传
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="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">
    <fx:Script>
       <![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);
           }
       ]]>
    </fx:Script>
    <fx:Declarations>
       <s:RemoteObject id="ro" destination="fileUpDownloadService"
                     result="ro_resultHandler(event)" fault="ro_faultHandler(event)"/>
    </fx:Declarations>
   
    <s:Button id="btnBrows" click="btnBrows_clickHandler(event)" label="选择文件" x="57" y="60"/>
    <s:Button id="btnUpload" click="btnUpload_clickHandler(event)" label="上传" x="145" y="60"/>
</s:Application>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics