|
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Net.Sockets;
using System.Collections;
namespace FtpLib
{
public class FTPFactory
{
private string remoteHost,remotePath,remoteUser,remotePass,mes;
private int remotePort,bytes;
private Socket clientSocket;
private int retValue;
private Boolean debug;
private Boolean logined;
private string reply;
private static int BLOCK_SIZE = 512;
Byte[] buffer = new Byte[BLOCK_SIZE];
Encoding ASCII = Encoding.ASCII;
///
/// 构造函数
///
/// 远程主机地址
/// 远程端口
/// 远程路径
/// 远程用户
/// 远程用户密码
/// 调试标志
public FTPFactory(string remHost, int remPort, string remPath, string remUser, string remPass, bool debugFlag)
{
remoteHost = remHost;
remotePort = remPort;
remotePath = remPath;
remoteUser = remUser;
remotePass = remPass;
debug = debugFlag;
logined = false;
}
///
/// 远程主机地址
///
public string RemoteHost
{
get
{
return this.remoteHost;
}
set
{
this.remoteHost = value;
}
}
///
/// 远程端口
///
public int RemotePort
{
get
{
return this.remotePort;
}
set
{
this.remotePort = value;
}
}
///
/// 远程主机路径
///
public string RemotePath
{
get
{
return this.remotePath;
}
set
{
this.remotePath = value;
}
}
///
/// 远程用户名
///
public string RemoteUser
{
get
{
return this.remoteUser;
}
set
{
this.remoteUser = value;
}
}
///
/// 远程用户密码
///
public string RemotePass
{
set
{
this.remotePass = value;
}
}
///
/// 获得目录列表
///
/// 目录
/// 目录集合
public string[] getDirList(string mask)
{
if(!logined)
{
login();
}
Socket cSocket = createDataSocket();
sendCommand("LIST " + mask);
if(!(retValue == 150 || retValue == 125 || retValue == 226))
{
throw new IOException(reply.Substring(4));
}
mes = "";
while(true)
{
int bytes = cSocket.Receive(buffer, buffer.Length, 0);
mes += ASCII.GetString(buffer, 0, bytes);
if(bytes < buffer.Length)
{
break;
}
}
char[] seperator = {'\n'};
string[] mess = mes.Split(seperator);
cSocket.Close();
ArrayList list = new ArrayList();
foreach(string m in mess)
{
if (m.StartsWith("d") && !m.EndsWith(".\r") && !m.EndsWith("..\r"))
{
if (!"".Equals(this.getDirName(m)))
list.Add(this.getDirName(m));
}
}
string[] dirs = new string[list.Count];
for(int i=0;i 0)
{
if(debug)
{
Console.WriteLine("seeking to " + offset);
}
long npos = output.Seek(offset,SeekOrigin.Begin);
Console.WriteLine("new pos="+npos);
}
}
sendCommand("RETR " + remFileName);
if(!(retValue == 150 || retValue == 125 || retValue == 226))
{
throw new IOException(reply.Substring(4));
}
while(true)
{
bytes = cSocket.Receive(buffer, buffer.Length, 0);
output.Write(buffer,0,bytes);
if(bytes 0 )
{
sendCommand("REST " + offset);
if(retValue != 350)
{
//throw new IOException(reply.Substring(4));
//Remote server may not support resuming.
offset = 0;
}
}
sendCommand("STOR "+Path.GetFileName(fileName));
if( !(retValue == 125 || retValue == 150) )
{
throw new IOException(reply.Substring(4));
}
// open input stream to read source file
FileStream input = new
FileStream(fileName,FileMode.Open);
if(offset != 0)
{
if(debug)
{
Console.WriteLine("seeking to " + offset);
}
input.Seek(offset,SeekOrigin.Begin);
}
Console.WriteLine("Uploading file "+fileName+" to "+remotePath);
while ((bytes = input.Read(buffer,0,buffer.Length)) > 0)
{
cSocket.Send(buffer, bytes, 0);
}
input.Close();
Console.WriteLine("");
if (cSocket.Connected)
{
cSocket.Close();
}
readReply();
if( !(retValue == 226 || retValue == 250) )
{
throw new IOException(reply.Substring(4));
}
}
///
/// 删除远程文件
///
/// 文件名
public void deleteRemoteFile(string fileName)
{
if(!logined)
{
login();
}
sendCommand("DELE "+fileName);
if(retValue != 250)
{
throw new IOException(reply.Substring(4));
}
}
///
/// 重命名远程文件
///
/// 原始文件名
/// 新文件名
public void renameRemoteFile(string oldFileName,string newFileName)
{
if(!logined)
{
login();
}
sendCommand("RNFR "+oldFileName);
if(retValue != 350)
{
throw new IOException(reply.Substring(4));
}
// known problem
// rnto will not take care of existing file.
// i.e. It will overwrite if newFileName exist
sendCommand("RNTO "+newFileName);
if(retValue != 250)
{
throw new IOException(reply.Substring(4));
}
}
///
/// 创建目录
///
/// 目录名
public void mkdir(string dirName)
{
if(!logined)
{
login();
}
sendCommand("MKD "+dirName);
if(retValue != 250)
{
throw new IOException(reply.Substring(4));
}
}
///
/// 删除目录
///
/// 目录名
public void rmdir(string dirName)
{
if(!logined)
{
login();
}
sendCommand("RMD "+dirName);
if(retValue != 250)
{
throw new IOException(reply.Substring(4));
}
}
///
/// 改变当前工作路径
///
/// 目录名称
public void chdir(string dirName)
{
if(dirName.Equals("."))
{
return;
}
if(!logined)
{
login();
}
sendCommand("CWD "+dirName);
if(retValue != 250)
{
throw new IOException(reply.Substring(4));
}
this.remotePath = dirName;
Console.WriteLine("Current directory is "+remotePath);
}
///
/// 关闭Ftp连接
///
public void close()
{
if( clientSocket != null )
{
sendCommand("QUIT");
}
cleanup();
Console.WriteLine("Closing");
}
///
/// 设置调试模式
///
/// 调式模式
public void setDebug(Boolean debug)
{
this.debug = debug;
}
private void readReply()
{
mes = "";
reply = readLine();
retValue = Int32.Parse(reply.Substring(0,3));
}
private void cleanup()
{
if(clientSocket!=null)
{
clientSocket.Close();
clientSocket = null;
}
logined = false;
}
private string readLine()
{
while(true)
{
bytes = clientSocket.Receive(buffer, buffer.Length, 0);
mes += ASCII.GetString(buffer, 0, bytes);
if(bytes < buffer.Length)
{
break;
}
}
char[] seperator = {'\n'};
string[] mess = mes.Split(seperator);
if(mes.Length > 2)
{
mes = mess[mess.Length-2];
}
else
{
mes = mess[0];
}
if(!mes.Substring(3,1).Equals(" "))
{
return readLine();
}
if(debug)
{
for(int k=0;k < mess.Length-1;k++)
{
Console.WriteLine(mess[k]);
}
}
return mes;
}
private void sendCommand(String command)
{
Byte[] cmdBytes =
Encoding.ASCII.GetBytes((command+"\r\n").ToCharArray());
clientSocket.Send(cmdBytes, cmdBytes.Length, 0);
readReply();
}
private Socket createDataSocket()
{
sendCommand("PASV");
if(retValue != 227)
{
throw new IOException(reply.Substring(4));
}
int index1 = reply.IndexOf('(');
int index2 = reply.IndexOf(')');
string ipData =
reply.Substring(index1+1,index2-index1-1);
int[] parts = new int[6];
int len = ipData.Length;
int partCount = 0;
string buf="";
for (int i = 0; i < len && partCount |
|
|