Flex 利用 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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值