加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex 利用 Blazeds上传文件 (转)

发布时间:2020-12-15 05:15:17 所属栏目:百科 来源:网络整理
导读:用Flex做业务经常会碰到各种类型的文件上传,如FTP上传、Http上传或者WEB服务形式上传,本文说的利用Blazeds上传文件有点类似Http上传,其原理就是客户端发送byte流,然后服务器端flex.messaging.MessageBrokerServlet接收byte流并生成文件。阅读本文最好了
用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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读