本文使用一个 demo 来演示如何使用 Flex 和 Java servlets 上传一个任意类型的本地文件到 RED5 服务器。使用的是 Flex 的 FileReference 接口,该接口可以对远程服务器上的文件进行上传、下载。FileReference 类提供了一个对话框接口和一个 upload 方法,通过该对话框,选择本地文件,而 upload 方法将会调用远程服务器端的 PHP,ASP 或者 Java 代码来进行上传。
首先创建一个 Flex3 应用程序,该程序获得本地文件,并调用 RED5 服务器 "http://localhost:5080/Red5FileUploadProj/fileupload" 的 servlet。
本文示例资源下载地址
免费下载地址在 http://linux.linuxidc.com/
用户名与密码都是www.linuxidc.com
具体下载目录在 /2012年资料/6月/16日/使用 Flex 和Java servlets 将文件上传到 RED5 服务器的步骤/
UploadToRed5Proj.mxml 源代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:net="flash.net.*" layout="absolute" creationComplete="doInit()">
- <mx:Panel horizontalCenter="0" verticalCenter="0" verticalAlign="middle" horizontalAlign="center" borderColor="#FFFFFF" width="100%" height="100%" backgroundColor="#307CB7">
- <mx:Script>
- <![CDATA[
- import mx.collections.ArrayCollection;
- import mx.controls.Alert;
- [Bindable]private var fileReference:FileReference;
- [Bindable]private var fileSelected:Boolean = false;
- [Bindable]private var serverUrl:String = "http://localhost:5080/Red5FileUploadProj/fileupload";
- private var statusArray:Array = new Array();
- [Bindable]private var statusArrayCollection:ArrayCollection = new ArrayCollection();
- private function doInit():void
- {
- fileReference = new FileReference();
- fileReference.addEventListener(Event.SELECT, onFileSelect);
- fileReference.addEventListener(Event.COMPLETE, onUploadComplete);
- fileReference.addEventListener(ProgressEvent.PROGRESS,onUploadProgress);
- fileReference.addEventListener(IOErrorEvent.IO_ERROR, onUploadError);
- fileReference.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onUploadError);
- }
- private function browseFile(event:MouseEvent):void
- {
- fileReference.browse();
- }
- private function onFileSelect(event:Event):void
- {
- fileSelected = true;
- fileTxt.text = fileReference.name;
- statusArray.push({status:"Ready to upload "+fileTxt.text});
- statusArrayCollection = new ArrayCollection(statusArray);
- }
- private function uploadFile(event:MouseEvent):void
- {
- if (!fileSelected || (urlTxt.text.length == 0))
- {
- Alert.show("Select a file and Enter a URL");
- return;
- }
- var urlRequest:URLRequest = new URLRequest(urlTxt.text);
- fileReference.upload(urlRequest);
- }
- private function onUploadProgress(event:ProgressEvent):void
- {
- statusArray.push({status:"In progress.."+((event.bytesLoaded * 100) / event.bytesTotal).toString()+"%"});
- statusArrayCollection = new ArrayCollection(statusArray);
- }
- private function onUploadComplete(event:Event):void
- {
- statusArray.push({status:"Uploaded successfully!"});
- statusArrayCollection = new ArrayCollection(statusArray);
- }
- private function onUploadError(event:Event):void
- {
- if (event is IOErrorEvent)
- {
- statusArray.push({status:"IO Error: "+(event as IOErrorEvent).text.toString()});
- statusArrayCollection = new ArrayCollection(statusArray);
- }
- else if (event is SecurityErrorEvent)
- {
- statusArray.push({status:"Security Error: "+(event as IOErrorEvent).text.toString()});
- statusArrayCollection = new ArrayCollection(statusArray);
- }
- }
- ]]>
- </mx:Script>
- <mx:VBox height="40%" width="30%" horizontalGap="0" horizontalAlign="left">
- <mx:HBox height="10%" width="100%" horizontalAlign="left">
- <mx:VBox height="100%" width="30%" horizontalAlign="left">
- <mx:Label text="File name"></mx:Label>
- </mx:VBox>
- <mx:VBox height="100%" width="40%" horizontalAlign="left">
- <mx:TextInput id="fileTxt" width="200" editable="false" toolTip="Select File to upload"/>
- </mx:VBox>
- <mx:VBox height="100%" width="30%" horizontalAlign="left">
- <mx:Button id="browseBut" label="Browse" click="browseFile(event)" />
- </mx:VBox>
- </mx:HBox>
- <mx:HBox height="10%" width="100%" horizontalAlign="left">
- <mx:VBox height="100%" width="30%" horizontalAlign="left">
- <mx:Label text="Server URL"></mx:Label>
- </mx:VBox>
- <mx:VBox height="100%" width="70%" horizontalAlign="left">
- <mx:TextInput id="urlTxt" width="290" text="{serverUrl}"/>
- </mx:VBox>
- </mx:HBox>
- <mx:HBox height="10%" width="100%" horizontalAlign="center">
- <mx:Button id="uploadBut" label="Upload file" click="uploadFile(event)" enabled="{fileSelected}" />
- </mx:HBox>
- <mx:HBox height="70%" width="100%" horizontalAlign="center">
- <mx:DataGrid id="statusDG" width="100%" height="100%" dataProvider="{statusArrayCollection}" variableRowHeight="true" wordWrap="true">
- <mx:columns>
- <mx:DataGridColumn dataField="status" headerText="Upload Status" paddingLeft="0" paddingRight="0">
- </mx:DataGridColumn>
- </mx:columns>
- </mx:DataGrid>
- </mx:HBox>
- </mx:VBox>
- </mx:Panel>
- </mx:Application>
