Flex FTP文件上传
来源:互联网 发布:清华北大碾压知乎 编辑:程序博客网 时间:2024/05/14 06:08
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));
}
private function ftpSocketSecurityErrorHandle(evt:SecurityErrorEvent):void {
dispatcher.dispatchEvent(new FileUpDownloadEvent(FileUpDownloadEvent.ERROR,evt.text));
}
private function dataSocketSecurityErrorHandle(evt:SecurityErrorEvent):void {
dispatcher.dispatchEvent(new FileUpDownloadEvent(FileUpDownloadEvent.ERROR,evt.text));
}
//发送ftpsocket ftp 指令
private function sendCommand(arg:String):void {
arg +="/n";
var content:ByteArray = new ByteArray();
content.writeMultiByte(arg,"gb2312");
ftpSocket.writeBytes(content);
ftpSocket.flush();
}
private function dataIOErrorHandle(evt:IOErrorEvent):void {
dispatcher.dispatchEvent(new FileUpDownloadEvent(FileUpDownloadEvent.ERROR,evt.text));
}
private function ftpSocketDataHandle(evt:ProgressEvent):void{
ftpResponce = ftpSocket.readUTFBytes(ftpSocket.bytesAvailable);
var serverResponse:String =ftpResponce.substr(0, 3);
if(ftpResponce.indexOf('227')>-1){
//取得使用者的ip位置
var temp:Object = ftpResponce.substring(ftpResponce.indexOf("(")+1 ,ftpResponce.indexOf(")"));
var upLoadSocket_temp:Object = temp.split(",");
clientIP = upLoadSocket_temp.slice(0,4).join(".");
clientPort = parseInt(upLoadSocket_temp[4])*256+
int(upLoadSocket_temp[5]);
//创建上传的ftp连接
dataSocket = new Socket(clientIP,clientPort);
dataSocket.addEventListener(ProgressEvent.SOCKET_DATA, receiveData);
dataSocket.addEventListener(IOErrorEvent.IO_ERROR,dataIOErrorHandle);
dataSocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,dataSocketSecurityErrorHandle);
//upload file
sendCommand("STOR "+this.fileName);
}
switch(serverResponse){
case "150": //开始文件传输
curPos = 0;
intervalID = setInterval(sendData,30);
break;
case "220": //FTP连接就绪
sendCommand("USER "+this.userName);
break;
case "331"://用户名正确,请继续输入口令
sendCommand("PASS "+this.userPwd);
break;
case "230"://登入成功
//指定下載文件的類型,I是二進位文件,A是字元文件
sendCommand("TYPE A");//設定TYPE為ASCII
sendCommand("TYPE I");//設定上傳的編碼為8-bit binary
if(!StringUtil.isEmpty(userDir)) //设定FTP 上传目录
sendCommand("CWD "+userDir);
sendCommand("PASV");//passive模式
break;
case "250" ://資料夾切換成功
break;
case "226"://关闭数据连接。请求的文件操作成功(比如,文件传输或文件终止)
ftpSocket.close();
dispatcher.dispatchEvent(new FileUpDownloadEvent(FileUpDownloadEvent.COMPLETED));
break;
case "227" : //Entering Passive Mode (h1,h2,h3,h4,p1,p2).
break;
case "530": //530 Login incorrect
dispatcher.dispatchEvent(new FileUpDownloadEvent(FileUpDownloadEvent.ERROR,"用户名或者密码有误!"));
break;
case "421":
dispatcher.dispatchEvent(new FileUpDownloadEvent(FileUpDownloadEvent.ERROR,"连接超时!"));
break;
default:
}
trace("ftp response: "+ftpResponce);
}
private var curPos:int = 0;
private function sendData():void{
var fileContents:ByteArray =this.fileData;
var fileSize:uint = this.fileData.length;
var chunk:uint = 1024*2;
//var pos:int = 0;
if(curPos+chunk>fileSize){
dataSocket.close();
clearInterval(intervalID);
return;
}
dataSocket.writeBytes(fileContents,curPos,chunk);
dataSocket.flush();
curPos+=chunk;
}
private function receiveData():void{
var responce:String = dataSocket.readUTFBytes(dataSocket.bytesAvailable);
trace("dataSocket response: "+responce);
}
}
}
FTP文件上传事件代码:
package whh.flex.controls.fileupload
{
import flash.events.Event;
public class FileUpDownloadEvent extends Event
{
public static const ERROR:String = "FILE_UPLOAD_ERROR";
public static const COMPLETED:String = "FILE_UPLOAD_COMPLETED";
public static const CANCEL:String = "FILE_UPLOAD_CANCEL";
//
private var _message:String;
private var _data:Object;
//
public function FileUpDownloadEvent(type:String, message:String = null)
{
super(type,true);
this._name = name;
this._message = message;
}
//
public function get message():String{
return _message;
}
public function set message(value:String):void{
_message = value;
}
}
}
则Application中要进行FTP文件上传,可简单codeing为(假设FTPFileUpDownload实例为ftpFile):
ftpFile.upload(fileData, “demo.doc”)
当然为了侦听ftp上传是否出错、完成,需要侦听FileUpDownloadEvent的Error和Complete事件。
- Flex FTP文件上传
- 使用Flex实现FTP文件上传功能
- FLEX FTP上传
- 上传文件到FTP
- FTP上传文件
- FTP上传文件示例
- ftp上传文件
- FTP 文件上传
- FTP 文件上传
- FTP上传文件示例
- FTP上传文件
- ftp文件下载、上传
- FTP文件上传
- FTP上传文件
- ftp上传 下载文件
- Java ftp 文件上传
- struts1 ftp 文件上传
- ftp文件上传
- 互联网企业流程部门价值方向的一些探讨
- UNIX系统
- PHP中$_SERVER的详细用法
- 设计模式一 工厂模式 (2)
- 开源SIP协议栈reSIProcate和SIPX
- Flex FTP文件上传
- 想找一个真心交流的的师父(.net&php)
- 用户sa登录失败,该用户与可信sql server连接无关联
- win7中的系统保护
- 用Java编写的简易计算器
- windows下GSL的配置
- php.ini中文解释
- 谷歌正开发自动驾驶汽车 可减半事故死亡人数
- windows下GSL的配置