我正在尝试将简单的文本文件上传到ftp,代码可以在PC上运行。 在Android上,该应用程序成功连接到ftp,但是当我尝试上传文件时,我得到“2031:错误#2031:套接字错误”。 有什么想法吗?
代码(来自http://suzhiyam.wordpress.com/2011/04/)是:
import flash.events.ProgressEvent;
import flash.events.Event;
导入flash.net.Socket;
import flash.events.IOErrorEvent;
import flash.errors.IOError;
import flash.filesystem.FileStream;
import flash.filesystem.File;
import flash.utils.ByteArray;
import flash.events.MouseEvent;
msg.text =“等等!连接到ftp服务器!”
//
var ftp_host:String =“ * ”; //原始代码中的FTP设置正确...
var ftp_port:Number = 21;
var ftp_username:String =“ * ”;
var ftp_password:String =“ * ”;
//
//在此演示中,我们将手动输入应从中进行上载或下载的远程服务器文件夹
var remoteFolderStr:String;
var localFolderStr:String;
var remoteFile:String;
//将用于连接到ftp服务器的套接字实例
var s = new Socket(ftp_host,ftp_port);
s.addEventListener(IOErrorEvent.IO_ERROR,onIOERR);
s.addEventListener(ProgressEvent.SOCKET_DATA,onReturnData);
s.addEventListener(Event.CONNECT,onConnectHandler);
//
//将用于连接以接收ftp服务器发送的数据的套接字实例
var r:Socket = new Socket();
r.addEventListener(ProgressEvent.SOCKET_DATA,onServData);
r.addEventListener(Event.CONNECT,onPasvConn);
r.addEventListener(IOErrorEvent.IO_ERROR,onIOERR);
//对于客户端发送到ftp服务器的每个命令,服务器返回带有返回码的消息
函数onReturnData(evt:ProgressEvent)
{
var d = s.readUTFBytes(s.bytesAvailable);
trace(d);
//check here for complete list of return codes and their meaning
//- http://en.wikipedia.org/wiki/List_of_FTP_server_return_codes
// the return message will have a 3 digit return code followed by a space and related message
// if the 3 digit return code is followed by "-" the it will be a multiline message
//-wait until the line with 3 digit code followed by space is delivered
if (d.indexOf("220 ") > -1)
{
msg.text="Logging in to ftp server!"
//connected to ftp server send user name to server
s.writeUTFBytes("USER "+ftp_username+"\n");
s.flush();
}
if (d.indexOf("331 ") > -1)
{
//Username accepted now send password to server
s.writeUTFBytes("PASS "+ftp_password+"\n");
s.flush();
}
if (d.indexOf("230") > -1 && d.indexOf("OK.") > -1)
{
msg.text="Log in successful!"
//Password accepted - lets enetr passive mode and retrive a list of files from a directory
//first enetr passive mode
//s.writeUTFBytes("PASV \n");
//s.flush();
}
var a = d.indexOf('227');
if (a > -1)
{
//Entering passive mode message will be returned along with it details of ip and port address will be returned
//-we have to connect to that address to receive the data
//format of the message will be: 227 Entering Passive Mode (209,190,85,253,148,206)
//the data inside brackets is the ip and port address, first four numbers represent IP and last 2 PORT
//the port value have to be calculated by multiplying the 5th number with 256 and adding the 6th number to it
//here in this example IP is 209.190.85.253 , PORT is (148*256)+206 = 38094
var st = d.indexOf("(",a);
var en = d.indexOf(")",a);
var str;
str = d.substring(st + 1,en);
var a2 = str.split(",");
var p1 = a2.pop();
var p2 = a2.pop();
var ip:String = a2.join(".");
var port:int=(p2*256)+(p1*1);
r.connect(ip, port);
}
if (d.indexOf("226 ") > -1)
{
//Data Transfer completed
//s.writeUTFBytes("QUIT \n");
//s.flush();
if (process=='download')
{
msg.text="DOWNLOAD_COMPLETE"
}
if (process=='upload')
{
msg.text="UPLOAD_COMPLETE"
}
dispatchEvent(new Event("dataReceived"))
}
if (d.indexOf("221 ") > -1)
{
//LOGGED OUT from server
}
//Response code 150 will be sent by server whenever a data connection is established after we send 'PASV' command
if (d.indexOf("150 ") > -1)
{
if (process == 'upload')
{
//Once data connection is established we can start sending the data to the server
startSendingData();
}
}
}
function onConnectHandler(evt:Event)
{
msg.text="CONNECTED TO FTP SERVER!"
trace("CONNECTED TO FTP SERVER");
//Client has connected to ftp server
}
function onPasvConn(evt:Event):void
{
trace("CONNECTED TO DATA PORT");
//s.writeUTFBytes("LIST /your/folder/path\n");
if (process=='upload')
{
//To Upload a file send following command
//STOR is the command followed by a space and path wher to store the file in remote server
//-with the name of the file to be saved as..you can provide any name with extension
s.writeUTFBytes("STOR /test.txt\n");
}
s.flush();
}
function onServData(evt:ProgressEvent):void
{
//DATA RECEIVED FROM SERVER THRO DATA PORT
}
函数onIOERR(evt:IOErrorEvent)
{
trace(evt.errorID+":"+evt.text);
}
//
var process:String =“”; //用于存储正在执行的操作的变量
var writeToStream:Boolean; //此处未使用
var localFile:File; //要上传的本地文件
var localFolder:File; //要下载的文件存储到的本地文件夹
var interval:Number;
var bufferSize:int;
//
BTN_Upload.addEventListener(MouseEvent.CLICK,initUpload);
function initUpload(evt:MouseEvent = null):void
{
//called when upload event is triggered
startUploadProcess();
}
function startUploadProcess():void
{
process = "upload";
//You need to pass this command 'TYPE I' to set data transfer mode as binary
s.writeUTFBytes("TYPE I\n");
s.writeUTFBytes("PASV \n");
s.flush();
}
function startSendingData():void
{
var textToUpload:String = "This is a test";
bufferSize = textToUpload.length;
var ba:ByteArray = new ByteArray();
//Store the info you want to upload in ByteArray HERE
ba.writeMultiByte(textToUpload, "iso-8859-1");
r.writeBytes(ba, 0, ba.bytesAvailable);
r.flush();
}