Flex FTP文件上传
?Flex FTP文件上传原理就是利用Flex Socket组件与FTP服务器进行网络通讯并根据FTP协议进行指令发生、接收,数据的传输和接收。本文指的是Flex web应用的FTP文件上传,具体实现有下述3个工作:
??? 1)上传文件选择、加载,可以使用Flex的FileRefrence组件;
??? 2)socket创建、连接、ftp用户登录信息的验证;
??? 3)文件数据发送;
?????? 先假设要上传的文件名称为:demo.doc, 该上传的文件内容为fileData,下述为FTP文件上传的核心类。
?
package fileupload
{
??? import flash.events.Event;
??? import flash.events.IEventDispatcher;
??? import flash.events.IOErrorEvent;
??? import flash.events.ProgressEvent;
??? import flash.events.SecurityErrorEvent;
??? import flash.net.FileReference;
??? import flash.net.Socket;
??? import flash.utils.ByteArray;
??? import flash.utils.clearInterval;
??? import flash.utils.setInterval;
?
??? public class FtpFileUpDownload
??? {
?????? private var ftpSocket:Socket;
?????? private var ftpResponce:String;
?????? private var dataSocket:Socket;
?????? private var dataResponse:String;
?????? private var clientIP:String;
?????? private var clientPort:uint;
?????? private var canceled:Boolean = false;
?????? private var dispatcher:IEventDispatcher;
?????? private var fileName:String;
?????? private var fileData:ByteArray;
?????? private var _isAnonymous:Boolean = false;
?????? private var _userName:String;
?????? private var _serverIP:String;
?????? private var _userPwd:String;
?????? private var _userDir:String;
?????? private var _serverPort:uint = 21;
?????? private var intervalID:int;
?????? //
?????? public function FtpFileUpDownload(dispatcher:IEventDispatcher)
?????? {
?????????? this.dispatcher = dispatcher;
?????? }
?
?????? /**
?????? ?* isAnonymous, FTP 是否允许 匿名访问,默认为false
?????? ?*/
?????? public function get isAnonymous():Boolean{
?????????? return _isAnonymous;
?????? }
?????? public function set isAnonymous(value:Boolean):void{
?????????? _isAnonymous = value;
?????? }
??????
?????? public function get userName():String{
?????????? return _userName;
?????? }
?????? public function set userName(value:String):void{
?????????? _userName = value;
?????? }
??????
?????? public function get serverIP():String{
?????????? return _serverIP;
?????? }
?????? public function set serverIP(value:String):void{
?????????? _serverIP = value;
?????? }
??????
?????? public function get userPwd():String{
?????????? return _userPwd;
?????? }
?????? public function set userPwd(value:String):void{
?????????? _userPwd = value;
?????? }
??????
?????? /**
?????? ?* userDir, FTP 用户上传目录
?????? ?*/
?????? public function get userDir():String{
?????????? return _userDir;
?????? }
?????? public function set userDir(value:String):void{
?????????? _userDir = value;
?????? }
??????
?????? public function get serverPort():uint{
?????????? return _serverPort;
?????? }
?????? public function set serverPort(value:uint):void{
?????????? _serverPort = value;
?????? }
??????
?????? /**
?????? ?* upload file, data is null is not allowed.
?????? ?*/
?????? public function upload(data:ByteArray, fileName:String):void{
?????????? this.fileName = fileName;
?????????? this.fileData = data;
?????????? if(data==null){
????????????? dispatcher.dispatchEvent(new FileUpDownloadEvent(FileUpDownloadEvent.ERROR,"Data is null is not allowed!"));
????????????? return;
?????????? }
?????????? if(!check()) return;
?????????? connect();
?????? }
??????
?????? private function check():Boolean{
?????????? var blnResult:Boolean = true;
?????????? if(!isAnonymous){
????????????? if(StringUtil.isEmpty(userName) || StringUtil.isEmpty(userPwd)){//
????????????????? dispatcher.dispatchEvent(new FileUpDownloadEvent(FileUpDownloadEvent.ERROR,
???????????????????? "请输入用户名和口令!"));
????????????????? blnResult = false;
????????????? }
?????????? }
?????????? if(StringUtil.isEmpty(serverIP)){
????????????? dispatcher.dispatchEvent(new FileUpDownloadEvent(FileUpDownloadEvent.ERROR,
????????????????? "请输入FTP服务器IP地址!"));
????????????? blnResult = false;
?????????? }
?????????? return blnResult;
?????? }
??????
?????? private function connect():void{
?????????? ftpSocket = new Socket(serverIP, serverPort);
?????????? ftpSocket.addEventListener(ProgressEvent.SOCKET_DATA, ftpSocketDataHandle);? ??? ftpSocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,ftpSocketSecurityErrorHandle);
??? ftpSocket.addEventListener(IOErrorEvent.IO_ERROR,ftpIOErrorHandle);
?????? }
??????
?????? private function ftpIOErrorHandle(evt:IOErrorEvent):void {
?????????? dispatcher.dispatchEvent(new FileUpDownloadEvent(FileUpDownloadEvent.ERROR,evt.text));
?????? }
text-align: left; line-height: