国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

一個FTP客戶端的C#代碼

2024-07-21 02:17:44
字體:
來源:轉載
供稿:網友

using system;
using system.net;
using system.io;
using system.text;
using system.net.sockets;

namespace zhangyuk.net.csdn.blog.ftpclient
{
 /// <summary>
 /// ftp client
 /// </summary>
 public class ftpclient
 {
  #region 構造函數
  /// <summary>
  /// 缺省構造函數
  /// </summary>
  public ftpclient()
  {
   strremotehost  = "";
   strremotepath  = "";
   strremoteuser  = "";
   strremotepass  = "";
   strremoteport  = 21;
   bconnected     = false;
  }

  /// <summary>
  /// 構造函數
  /// </summary>
  /// <param name="remotehost"></param>
  /// <param name="remotepath"></param>
  /// <param name="remoteuser"></param>
  /// <param name="remotepass"></param>
  /// <param name="remoteport"></param>
  public ftpclient( string remotehost, string remotepath, string remoteuser, string remotepass, int remoteport )
  {
   strremotehost  = remotehost;
   strremotepath  = remotepath;
   strremoteuser  = remoteuser;
   strremotepass  = remotepass;
   strremoteport  = remoteport;
   connect();
  }
  #endregion

  #region 登陸
  /// <summary>
  /// ftp服務器ip地址
  /// </summary>
  private string strremotehost;
  public string remotehost
  {
   get
   {
    return strremotehost;
   }
   set
   {
    strremotehost = value;
   }
  }
  /// <summary>
  /// ftp服務器端口
  /// </summary>
  private int strremoteport;
  public int remoteport
  {
   get
   {
    return strremoteport;
   }
   set
   {
    strremoteport = value;
   }
  }
  /// <summary>
  /// 當前服務器目錄
  /// </summary>
  private string strremotepath;
  public string remotepath
  {
   get
   {
    return strremotepath;
   }
   set
   {
    strremotepath = value;
   }
  }
  /// <summary>
  /// 登錄用戶賬號
  /// </summary>
  private string strremoteuser;
  public string remoteuser
  {
   set
   {
    strremoteuser = value;
   }
  }
  /// <summary>
  /// 用戶登錄密碼
  /// </summary>
  private string strremotepass;
  public string remotepass
  {
   set
   {
    strremotepass = value;
   }
  }

  /// <summary>
  /// 是否登錄
  /// </summary>
  private boolean bconnected;
  public bool connected
  {
   get
   {
    return bconnected;
   }
  }
  #endregion

  #region 鏈接
  /// <summary>
  /// 建立連接
  /// </summary>
  public void connect()
  {
   socketcontrol = new socket(addressfamily.internetwork,sockettype.stream,protocoltype.tcp);
   ipendpoint ep = new ipendpoint(ipaddress.parse(remotehost), strremoteport);
   // 鏈接
   try
   {
    socketcontrol.connect(ep);
   }
   catch(exception)
   {
    throw new ioexception("couldn't connect to remote server");
   }

   // 獲取應答碼
   readreply();
   if(ireplycode != 220)
   {
    disconnect();
    throw new ioexception(strreply.substring(4));
   }

   // 登陸
   sendcommand("user "+strremoteuser);
   if( !(ireplycode == 331 || ireplycode == 230) )
   {
    closesocketconnect();//關閉連接
    throw new ioexception(strreply.substring(4));
   }
   if( ireplycode != 230 )
   {
    sendcommand("pass "+strremotepass);
    if( !(ireplycode == 230 || ireplycode == 202) )
    {
     closesocketconnect();//關閉連接
     throw new ioexception(strreply.substring(4));
    }
   }
   bconnected = true;

   // 切換到目錄
   chdir(strremotepath);
  }
   

  /// <summary>
  /// 關閉連接
  /// </summary>
  public void disconnect()
  {
   if( socketcontrol != null )
   {
    sendcommand("quit");
   }
   closesocketconnect();
  }

  #endregion

  #region 傳輸模式

  /// <summary>
  /// 傳輸模式:二進制類型、ascii類型
  /// </summary>
  public enum transfertype {binary,ascii};

  /// <summary>
  /// 設置傳輸模式
  /// </summary>
  /// <param name="tttype">傳輸模式</param>
  public void settransfertype(transfertype tttype)
  {
   if(tttype == transfertype.binary)
   {
    sendcommand("type i");//binary類型傳輸
   }
   else
   {
    sendcommand("type a");//ascii類型傳輸
   }
   if (ireplycode != 200)
   {
    throw new ioexception(strreply.substring(4));
   }
   else
   {
    trtype = tttype;
   }
  }


  /// <summary>
  /// 獲得傳輸模式
  /// </summary>
  /// <returns>傳輸模式</returns>
  public transfertype gettransfertype()
  {
   return trtype;
  }
   
  #endregion

  #region 文件操作
  /// <summary>
  /// 獲得文件列表
  /// </summary>
  /// <param name="strmask">文件名的匹配字符串</param>
  /// <returns></returns>
  public string[] dir(string strmask)
  {
   // 建立鏈接
   if(!bconnected)
   {
    connect();
   }

   //建立進行數據連接的socket
   socket socketdata = createdatasocket();
  
   //傳送命令
   sendcommand("nlst " + strmask);

   //分析應答代碼
   if(!(ireplycode == 150 || ireplycode == 125 || ireplycode == 226))
   {
    throw new ioexception(strreply.substring(4));
   }

   //獲得結果
   strmsg = "";
   while(true)
   {
    int ibytes = socketdata.receive(buffer, buffer.length, 0);
    strmsg += ascii.getstring(buffer, 0, ibytes);
    if(ibytes < buffer.length)
    {
     break;
    }
   }
   char[] seperator = {'/n'};
   string[] strsfilelist = strmsg.split(seperator);
   socketdata.close();//數據socket關閉時也會有返回碼
   if(ireplycode != 226)
   {
    readreply();
    if(ireplycode != 226)
    {
     throw new ioexception(strreply.substring(4));
    }
   }
   return strsfilelist;
  }
   

  /// <summary>
  /// 獲取文件大小
  /// </summary>
  /// <param name="strfilename">文件名</param>
  /// <returns>文件大小</returns>
  private long getfilesize(string strfilename)
  {
   if(!bconnected)
   {
    connect();
   }
   sendcommand("size " + path.getfilename(strfilename));
   long lsize=0;
   if(ireplycode == 213)
   {
    lsize = int64.parse(strreply.substring(4));
   }
   else
   {
    throw new ioexception(strreply.substring(4));
   }
   return lsize;
  }


  /// <summary>
  /// 刪除
  /// </summary>
  /// <param name="strfilename">待刪除文件名</param>
  public void delete(string strfilename)
  {
   if(!bconnected)
   {
    connect();
   }
   sendcommand("dele "+strfilename);
   if(ireplycode != 250)
   {
    throw new ioexception(strreply.substring(4));
   }
  }
   

  /// <summary>
  /// 重命名(如果新文件名與已有文件重名,將覆蓋已有文件)
  /// </summary>
  /// <param name="stroldfilename">舊文件名</param>
  /// <param name="strnewfilename">新文件名</param>
  public void rename(string stroldfilename,string strnewfilename)
  {
   if(!bconnected)
   {
    connect();
   }
   sendcommand("rnfr "+stroldfilename);
   if(ireplycode != 350)
   {
    throw new ioexception(strreply.substring(4));
   }
   //  如果新文件名與原有文件重名,將覆蓋原有文件
   sendcommand("rnto "+strnewfilename);
   if(ireplycode != 250)
   {
    throw new ioexception(strreply.substring(4));
   }
  }
  #endregion

  #region 上傳和下載
  /// <summary>
  /// 下載一批文件
  /// </summary>
  /// <param name="strfilenamemask">文件名的匹配字符串</param>
  /// <param name="strfolder">本地目錄(不得以/結束)</param>
  public void get(string strfilenamemask,string strfolder)
  {
   if(!bconnected)
   {
    connect();
   }
   string[] strfiles = dir(strfilenamemask);
   foreach(string strfile in strfiles)
   {
    if(!strfile.equals(""))//一般來說strfiles的最后一個元素可能是空字符串
    {
     get(strfile,strfolder,strfile);
    }
   }
  }
   

  /// <summary>
  /// 下載一個文件
  /// </summary>
  /// <param name="strremotefilename">要下載的文件名</param>
  /// <param name="strfolder">本地目錄(不得以/結束)</param>
  /// <param name="strlocalfilename">保存在本地時的文件名</param>
  public void get(string strremotefilename,string strfolder,string strlocalfilename)
  {
   if(!bconnected)
   {
    connect();
   }
   settransfertype(transfertype.binary);
   if (strlocalfilename.equals(""))
   {
    strlocalfilename = strremotefilename;
   }
   if(!file.exists(strlocalfilename))
   {
    stream st = file.create(strlocalfilename);
    st.close();
   }
   filestream output = new
   filestream(strfolder + "http://" + strlocalfilename,filemode.create);
   socket socketdata = createdatasocket();
   sendcommand("retr " + strremotefilename);
   if(!(ireplycode == 150 || ireplycode == 125
   || ireplycode == 226 || ireplycode == 250))
   {
    throw new ioexception(strreply.substring(4));
   }
   while(true)
   {
    int ibytes = socketdata.receive(buffer, buffer.length, 0);
    output.write(buffer,0,ibytes);
    if(ibytes <= 0)
    {
     break;
    }
   }
   output.close();
   if (socketdata.connected)
   {
    socketdata.close();
   }
   if(!(ireplycode == 226 || ireplycode == 250))
   {
    readreply();
    if(!(ireplycode == 226 || ireplycode == 250))
    {
     throw new ioexception(strreply.substring(4));
    }
   }
  }
   

  /// <summary>
  /// 上傳一批文件
  /// </summary>
  /// <param name="strfolder">本地目錄(不得以/結束)</param>
  /// <param name="strfilenamemask">文件名匹配字符(可以包含*和?)</param>
  public void put(string strfolder,string strfilenamemask)
  {
   string[] strfiles = directory.getfiles(strfolder,strfilenamemask);
   foreach(string strfile in strfiles)
   {
    //strfile是完整的文件名(包含路徑)
    put(strfile);
   }
  }
   

  /// <summary>
  /// 上傳一個文件
  /// </summary>
  /// <param name="strfilename">本地文件名</param>
  public void put(string strfilename)
  {
   if(!bconnected)
   {
    connect();
   }
   socket socketdata = createdatasocket();
   sendcommand("stor "+path.getfilename(strfilename));
   if( !(ireplycode == 125 || ireplycode == 150) )
   {
    throw new ioexception(strreply.substring(4));
   }
   filestream input = new
   filestream(strfilename,filemode.open);
   int ibytes = 0;
   while ((ibytes = input.read(buffer,0,buffer.length)) > 0)
   {
    socketdata.send(buffer, ibytes, 0);
   }
   input.close();
   if (socketdata.connected)
   {
    socketdata.close();
   }
   if(!(ireplycode == 226 || ireplycode == 250))
   {
    readreply();
    if(!(ireplycode == 226 || ireplycode == 250))
    {
     throw new ioexception(strreply.substring(4));
    }
   }
  }
   
  #endregion

  #region 目錄操作
  /// <summary>
  /// 創建目錄
  /// </summary>
  /// <param name="strdirname">目錄名</param>
  public void mkdir(string strdirname)
  {
   if(!bconnected)
   {
    connect();
   }
   sendcommand("mkd "+strdirname);
   if(ireplycode != 257)
   {
    throw new ioexception(strreply.substring(4));
   }
  }
   
 
  /// <summary>
  /// 刪除目錄
  /// </summary>
  /// <param name="strdirname">目錄名</param>
  public void rmdir(string strdirname)
  {
   if(!bconnected)
   {
    connect();
   }
   sendcommand("rmd "+strdirname);
   if(ireplycode != 250)
   {
    throw new ioexception(strreply.substring(4));
   }
  }
   
 
  /// <summary>
  /// 改變目錄
  /// </summary>
  /// <param name="strdirname">新的工作目錄名</param>
  public void chdir(string strdirname)
  {
   if(strdirname.equals(".") || strdirname.equals(""))
   {
    return;
   }
   if(!bconnected)
   {
    connect();
   }
   sendcommand("cwd "+strdirname);
   if(ireplycode != 250)
   {
    throw new ioexception(strreply.substring(4));
   }
   this.strremotepath = strdirname;
  }
   
  #endregion

  #region 內部變量
  /// <summary>
  /// 服務器返回的應答信息(包含應答碼)
  /// </summary>
  private string strmsg;
  /// <summary>
  /// 服務器返回的應答信息(包含應答碼)
  /// </summary>
  private string strreply;
  /// <summary>
  /// 服務器返回的應答碼
  /// </summary>
  private int ireplycode;
  /// <summary>
  /// 進行控制連接的socket
  /// </summary>
  private socket socketcontrol;
  /// <summary>
  /// 傳輸模式
  /// </summary>
  private transfertype trtype;
  /// <summary>
  /// 接收和發送數據的緩沖區
  /// </summary>
  private static int block_size = 512;
  byte[] buffer = new byte[block_size];
  /// <summary>
  /// 編碼方式
  /// </summary>
  encoding ascii = encoding.ascii;
  #endregion

  #region 內部函數
  /// <summary>
  /// 將一行應答字符串記錄在strreply和strmsg
  /// 應答碼記錄在ireplycode
  /// </summary>
  private void readreply()
  {
   strmsg = "";
   strreply = readline();
   ireplycode = int32.parse(strreply.substring(0,3));
  }

  /// <summary>
  /// 建立進行數據連接的socket
  /// </summary>
  /// <returns>數據連接socket</returns>
  private socket createdatasocket()
  {
   sendcommand("pasv");
   if(ireplycode != 227)
   {
    throw new ioexception(strreply.substring(4));
   }
   int index1 = strreply.indexof('(');
   int index2 = strreply.indexof(')');
   string ipdata =
    strreply.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 <= 6; i++)
   {
    char ch = char.parse(ipdata.substring(i,1));
    if (char.isdigit(ch))
     buf+=ch;
    else if (ch != ',')
    {
     throw new ioexception("malformed pasv strreply: " +
      strreply);
    }
    if (ch == ',' || i+1 == len)
    {
     try
     {
      parts[partcount++] = int32.parse(buf);
      buf="";
     }
     catch (exception)
     {
      throw new ioexception("malformed pasv strreply: " +
       strreply);
     }
    }
   }
   string ipaddress = parts[0] + "."+ parts[1]+ "." +
    parts[2] + "." + parts[3];
   int port = (parts[4] << 8) + parts[5];
   socket s = new
    socket(addressfamily.internetwork,sockettype.stream,protocoltype.tcp);
   ipendpoint ep = new
    ipendpoint(ipaddress.parse(ipaddress), port);
   try
   {
    s.connect(ep);
   }
   catch(exception)
   {
    throw new ioexception("can't connect to remote server");
   }
   return s;
  }


  /// <summary>
  /// 關閉socket連接(用于登錄以前)
  /// </summary>
  private void closesocketconnect()
  {
   if(socketcontrol!=null)
   {
    socketcontrol.close();
    socketcontrol = null;
   }
   bconnected = false;
  }
 
  /// <summary>
  /// 讀取socket返回的所有字符串
  /// </summary>
  /// <returns>包含應答碼的字符串行</returns>
  private string readline()
  {
   while(true)
   {
    int ibytes = socketcontrol.receive(buffer, buffer.length, 0);
    strmsg += ascii.getstring(buffer, 0, ibytes);
    if(ibytes < buffer.length)
    {
     break;
    }
   }
   char[] seperator = {'/n'};
   string[] mess = strmsg.split(seperator);
   if(strmsg.length > 2)
   {
    strmsg = mess[mess.length-2];
    //seperator[0]是10,換行符是由13和0組成的,分隔后10后面雖沒有字符串,
    //但也會分配為空字符串給后面(也是最后一個)字符串數組,
    //所以最后一個mess是沒用的空字符串
    //但為什么不直接取mess[0],因為只有最后一行字符串應答碼與信息之間有空格
   }
   else
   {
    strmsg = mess[0];
   }
   if(!strmsg.substring(3,1).equals(" "))//返回字符串正確的是以應答碼(如220開頭,后面接一空格,再接問候字符串)
   {
    return readline();
   }
   return strmsg;
  }


  /// <summary>
  /// 發送命令并獲取應答碼和最后一行應答字符串
  /// </summary>
  /// <param name="strcommand">命令</param>
  private void sendcommand(string strcommand)
  {
   byte[] cmdbytes =
   encoding.ascii.getbytes((strcommand+"/r/n").tochararray());
   socketcontrol.send(cmdbytes, cmdbytes.length, 0);
   readreply();
  }

  #endregion
 }
}


namespace zhangyuk.net.csdn.blog.ftpclient
{
 /// <summary>
 /// ftp client
 /// </summary>
 public class ftpclient
 {
  #region 構造函數
  /// <summary>
  /// 缺省構造函數
  /// </summary>
  public ftpclient()
  {
   strremotehost  = "";
   strremotepath  = "";
   strremoteuser  = "";
   strremotepass  = "";
   strremoteport  = 21;
   bconnected     = false;
  }

  /// <summary>
  /// 構造函數
  /// </summary>
  /// <param name="remotehost"></param>
  /// <param name="remotepath"></param>
  /// <param name="remoteuser"></param>
  /// <param name="remotepass"></param>
  /// <param name="remoteport"></param>
  public ftpclient( string remotehost, string remotepath, string remoteuser, string remotepass, int remoteport )
  {
   strremotehost  = remotehost;
   strremotepath  = remotepath;
   strremoteuser  = remoteuser;
   strremotepass  = remotepass;
   strremoteport  = remoteport;
   connect();
  }
  #endregion

  #region 登陸
  /// <summary>
  /// ftp服務器ip地址
  /// </summary>
  private string strremotehost;
  public string remotehost
  {
   get
   {
    return strremotehost;
   }
   set
   {
    strremotehost = value;
   }
  }
  /// <summary>
  /// ftp服務器端口
  /// </summary>
  private int strremoteport;
  public int remoteport
  {
   get
   {
    return strremoteport;
   }
   set
   {
    strremoteport = value;
   }
  }
  /// <summary>
  /// 當前服務器目錄
  /// </summary>
  private string strremotepath;
  public string remotepath
  {
   get
   {
    return strremotepath;
   }
   set
   {
    strremotepath = value;
   }
  }
  /// <summary>
  /// 登錄用戶賬號
  /// </summary>
  private string strremoteuser;
  public string remoteuser
  {
   set
   {
    strremoteuser = value;
   }
  }
  /// <summary>
  /// 用戶登錄密碼
  /// </summary>
  private string strremotepass;
  public string remotepass
  {
   set
   {
    strremotepass = value;
   }
  }

  /// <summary>
  /// 是否登錄
  /// </summary>
  private boolean bconnected;
  public bool connected
  {
   get
   {
    return bconnected;
   }
  }
  #endregion

  #region 鏈接
  /// <summary>
  /// 建立連接
  /// </summary>
  public void connect()
  {
   socketcontrol = new socket(addressfamily.internetwork,sockettype.stream,protocoltype.tcp);
   ipendpoint ep = new ipendpoint(ipaddress.parse(remotehost), strremoteport);
   // 鏈接
   try
   {
    socketcontrol.connect(ep);
   }
   catch(exception)
   {
    throw new ioexception("couldn't connect to remote server");
   }

   // 獲取應答碼
   readreply();
   if(ireplycode != 220)
   {
    disconnect();
    throw new ioexception(strreply.substring(4));
   }

   // 登陸
   sendcommand("user "+strremoteuser);
   if( !(ireplycode == 331 || ireplycode == 230) )
   {
    closesocketconnect();//關閉連接
    throw new ioexception(strreply.substring(4));
   }
   if( ireplycode != 230 )
   {
    sendcommand("pass "+strremotepass);
    if( !(ireplycode == 230 || ireplycode == 202) )
    {
     closesocketconnect();//關閉連接
     throw new ioexception(strreply.substring(4));
    }
   }
   bconnected = true;

   // 切換到目錄
   chdir(strremotepath);
  }
   

  /// <summary>
  /// 關閉連接
  /// </summary>
  public void disconnect()
  {
   if( socketcontrol != null )
   {
    sendcommand("quit");
   }
   closesocketconnect();
  }

  #endregion

  #region 傳輸模式

  /// <summary>
  /// 傳輸模式:二進制類型、ascii類型
  /// </summary>
  public enum transfertype {binary,ascii};

  /// <summary>
  /// 設置傳輸模式
  /// </summary>
  /// <param name="tttype">傳輸模式</param>
  public void settransfertype(transfertype tttype)
  {
   if(tttype == transfertype.binary)
   {
    sendcommand("type i");//binary類型傳輸
   }
   else
   {
    sendcommand("type a");//ascii類型傳輸
   }
   if (ireplycode != 200)
   {
    throw new ioexception(strreply.substring(4));
   }
   else
   {
    trtype = tttype;
   }
  }


  /// <summary>
  /// 獲得傳輸模式
  /// </summary>
  /// <returns>傳輸模式</returns>
  public transfertype gettransfertype()
  {
   return trtype;
  }
   
  #endregion

  #region 文件操作
  /// <summary>
  /// 獲得文件列表
  /// </summary>
  /// <param name="strmask">文件名的匹配字符串</param>
  /// <returns></returns>
  public string[] dir(string strmask)
  {
   // 建立鏈接
   if(!bconnected)
   {
    connect();
   }

   //建立進行數據連接的socket
   socket socketdata = createdatasocket();
  
   //傳送命令
   sendcommand("nlst " + strmask);

   //分析應答代碼
   if(!(ireplycode == 150 || ireplycode == 125 || ireplycode == 226))
   {
    throw new ioexception(strreply.substring(4));
   }

   //獲得結果
   strmsg = "";
   while(true)
   {
    int ibytes = socketdata.receive(buffer, buffer.length, 0);
    strmsg += ascii.getstring(buffer, 0, ibytes);
    if(ibytes < buffer.length)
    {
     break;
    }
   }
   char[] seperator = {'/n'};
   string[] strsfilelist = strmsg.split(seperator);
   socketdata.close();//數據socket關閉時也會有返回碼
   if(ireplycode != 226)
   {
    readreply();
    if(ireplycode != 226)
    {
     throw new ioexception(strreply.substring(4));
    }
   }
   return strsfilelist;
  }
   

  /// <summary>
  /// 獲取文件大小
  /// </summary>
  /// <param name="strfilename">文件名</param>
  /// <returns>文件大小</returns>
  private long getfilesize(string strfilename)
  {
   if(!bconnected)
   {
    connect();
   }
   sendcommand("size " + path.getfilename(strfilename));
   long lsize=0;
   if(ireplycode == 213)
   {
    lsize = int64.parse(strreply.substring(4));
   }
   else
   {
    throw new ioexception(strreply.substring(4));
   }
   return lsize;
  }


  /// <summary>
  /// 刪除
  /// </summary>
  /// <param name="strfilename">待刪除文件名</param>
  public void delete(string strfilename)
  {
   if(!bconnected)
   {
    connect();
   }
   sendcommand("dele "+strfilename);
   if(ireplycode != 250)
   {
    throw new ioexception(strreply.substring(4));
   }
  }
   

  /// <summary>
  /// 重命名(如果新文件名與已有文件重名,將覆蓋已有文件)
  /// </summary>
  /// <param name="stroldfilename">舊文件名</param>
  /// <param name="strnewfilename">新文件名</param>
  public void rename(string stroldfilename,string strnewfilename)
  {
   if(!bconnected)
   {
    connect();
   }
   sendcommand("rnfr "+stroldfilename);
   if(ireplycode != 350)
   {
    throw new ioexception(strreply.substring(4));
   }
   //  如果新文件名與原有文件重名,將覆蓋原有文件
   sendcommand("rnto "+strnewfilename);
   if(ireplycode != 250)
   {
    throw new ioexception(strreply.substring(4));
   }
  }
  #endregion

  #region 上傳和下載
  /// <summary>
  /// 下載一批文件
  /// </summary>
  /// <param name="strfilenamemask">文件名的匹配字符串</param>
  /// <param name="strfolder">本地目錄(不得以/結束)</param>
  public void get(string strfilenamemask,string strfolder)
  {
   if(!bconnected)
   {
    connect();
   }
   string[] strfiles = dir(strfilenamemask);
   foreach(string strfile in strfiles)
   {
    if(!strfile.equals(""))//一般來說strfiles的最后一個元素可能是空字符串
    {
     get(strfile,strfolder,strfile);
    }
   }
  }
   

  /// <summary>
  /// 下載一個文件
  /// </summary>
  /// <param name="strremotefilename">要下載的文件名</param>
  /// <param name="strfolder">本地目錄(不得以/結束)</param>
  /// <param name="strlocalfilename">保存在本地時的文件名</param>
  public void get(string strremotefilename,string strfolder,string strlocalfilename)
  {
   if(!bconnected)
   {
    connect();
   }
   settransfertype(transfertype.binary);
   if (strlocalfilename.equals(""))
   {
    strlocalfilename = strremotefilename;
   }
   if(!file.exists(strlocalfilename))
   {
    stream st = file.create(strlocalfilename);
    st.close();
   }
   filestream output = new
   filestream(strfolder + "http://" + strlocalfilename,filemode.create);
   socket socketdata = createdatasocket();
   sendcommand("retr " + strremotefilename);
   if(!(ireplycode == 150 || ireplycode == 125
   || ireplycode == 226 || ireplycode == 250))
   {
    throw new ioexception(strreply.substring(4));
   }
   while(true)
   {
    int ibytes = socketdata.receive(buffer, buffer.length, 0);
    output.write(buffer,0,ibytes);
    if(ibytes <= 0)
    {
     break;
    }
   }
   output.close();
   if (socketdata.connected)
   {
    socketdata.close();
   }
   if(!(ireplycode == 226 || ireplycode == 250))
   {
    readreply();
    if(!(ireplycode == 226 || ireplycode == 250))
    {
     throw new ioexception(strreply.substring(4));
    }
   }
  }
   

  /// <summary>
  /// 上傳一批文件
  /// </summary>
  /// <param name="strfolder">本地目錄(不得以/結束)</param>
  /// <param name="strfilenamemask">文件名匹配字符(可以包含*和?)</param>
  public void put(string strfolder,string strfilenamemask)
  {
   string[] strfiles = directory.getfiles(strfolder,strfilenamemask);
   foreach(string strfile in strfiles)
   {
    //strfile是完整的文件名(包含路徑)
    put(strfile);
   }
  }
   

  /// <summary>
  /// 上傳一個文件
  /// </summary>
  /// <param name="strfilename">本地文件名</param>
  public void put(string strfilename)
  {
   if(!bconnected)
   {
    connect();
   }
   socket socketdata = createdatasocket();
   sendcommand("stor "+path.getfilename(strfilename));
   if( !(ireplycode == 125 || ireplycode == 150) )
   {
    throw new ioexception(strreply.substring(4));
   }
   filestream input = new
   filestream(strfilename,filemode.open);
   int ibytes = 0;
   while ((ibytes = input.read(buffer,0,buffer.length)) > 0)
   {
    socketdata.send(buffer, ibytes, 0);
   }
   input.close();
   if (socketdata.connected)
   {
    socketdata.close();
   }
   if(!(ireplycode == 226 || ireplycode == 250))
   {
    readreply();
    if(!(ireplycode == 226 || ireplycode == 250))
    {
     throw new ioexception(strreply.substring(4));
    }
   }
  }
   
  #endregion

  #region 目錄操作
  /// <summary>
  /// 創建目錄
  /// </summary>
  /// <param name="strdirname">目錄名</param>
  public void mkdir(string strdirname)
  {
   if(!bconnected)
   {
    connect();
   }
   sendcommand("mkd "+strdirname);
   if(ireplycode != 257)
   {
    throw new ioexception(strreply.substring(4));
   }
  }
   
 
  /// <summary>
  /// 刪除目錄
  /// </summary>
  /// <param name="strdirname">目錄名</param>
  public void rmdir(string strdirname)
  {
   if(!bconnected)
   {
    connect();
   }
   sendcommand("rmd "+strdirname);
   if(ireplycode != 250)
   {
    throw new ioexception(strreply.substring(4));
   }
  }
   
 
  /// <summary>
  /// 改變目錄
  /// </summary>
  /// <param name="strdirname">新的工作目錄名</param>
  public void chdir(string strdirname)
  {
   if(strdirname.equals(".") || strdirname.equals(""))
   {
    return;
   }
   if(!bconnected)
   {
    connect();
   }
   sendcommand("cwd "+strdirname);
   if(ireplycode != 250)
   {
    throw new ioexception(strreply.substring(4));
   }
   this.strremotepath = strdirname;
  }
   
  #endregion

  #region 內部變量
  /// <summary>
  /// 服務器返回的應答信息(包含應答碼)
  /// </summary>
  private string strmsg;
  /// <summary>
  /// 服務器返回的應答信息(包含應答碼)
  /// </summary>
  private string strreply;
  /// <summary>
  /// 服務器返回的應答碼
  /// </summary>
  private int ireplycode;
  /// <summary>
  /// 進行控制連接的socket
  /// </summary>
  private socket socketcontrol;
  /// <summary>
  /// 傳輸模式
  /// </summary>
  private transfertype trtype;
  /// <summary>
  /// 接收和發送數據的緩沖區
  /// </summary>
  private static int block_size = 512;
  byte[] buffer = new byte[block_size];
  /// <summary>
  /// 編碼方式
  /// </summary>
  encoding ascii = encoding.ascii;
  #endregion

  #region 內部函數
  /// <summary>
  /// 將一行應答字符串記錄在strreply和strmsg
  /// 應答碼記錄在ireplycode
  /// </summary>
  private void readreply()
  {
   strmsg = "";
   strreply = readline();
   ireplycode = int32.parse(strreply.substring(0,3));
  }

  /// <summary>
  /// 建立進行數據連接的socket
  /// </summary>
  /// <returns>數據連接socket</returns>
  private socket createdatasocket()
  {
   sendcommand("pasv");
   if(ireplycode != 227)
   {
    throw new ioexception(strreply.substring(4));
   }
   int index1 = strreply.indexof('(');
   int index2 = strreply.indexof(')');
   string ipdata =
    strreply.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 <= 6; i++)
   {
    char ch = char.parse(ipdata.substring(i,1));
    if (char.isdigit(ch))
     buf+=ch;
    else if (ch != ',')
    {
     throw new ioexception("malformed pasv strreply: " +
      strreply);
    }
    if (ch == ',' || i+1 == len)
    {
     try
     {
      parts[partcount++] = int32.parse(buf);
      buf="";
     }
     catch (exception)
     {
      throw new ioexception("malformed pasv strreply: " +
       strreply);
     }
    }
   }
   string ipaddress = parts[0] + "."+ parts[1]+ "." +
    parts[2] + "." + parts[3];
   int port = (parts[4] << 8) + parts[5];
   socket s = new
    socket(addressfamily.internetwork,sockettype.stream,protocoltype.tcp);
   ipendpoint ep = new
    ipendpoint(ipaddress.parse(ipaddress), port);
   try
   {
    s.connect(ep);
   }
   catch(exception)
   {
    throw new ioexception("can't connect to remote server");
   }
   return s;
  }


  /// <summary>
  /// 關閉socket連接(用于登錄以前)
  /// </summary>
  private void closesocketconnect()
  {
   if(socketcontrol!=null)
   {
    socketcontrol.close();
    socketcontrol = null;
   }
   bconnected = false;
  }
 
  /// <summary>
  /// 讀取socket返回的所有字符串
  /// </summary>
  /// <returns>包含應答碼的字符串行</returns>
  private string readline()
  {
   while(true)
   {
    int ibytes = socketcontrol.receive(buffer, buffer.length, 0);
    strmsg += ascii.getstring(buffer, 0, ibytes);
    if(ibytes < buffer.length)
    {
     break;
    }
   }
   char[] seperator = {'/n'};
   string[] mess = strmsg.split(seperator);
   if(strmsg.length > 2)
   {
    strmsg = mess[mess.length-2];
    //seperator[0]是10,換行符是由13和0組成的,分隔后10后面雖沒有字符串,
    //但也會分配為空字符串給后面(也是最后一個)字符串數組,
    //所以最后一個mess是沒用的空字符串
    //但為什么不直接取mess[0],因為只有最后一行字符串應答碼與信息之間有空格
   }
   else
   {
    strmsg = mess[0];
   }
   if(!strmsg.substring(3,1).equals(" "))//返回字符串正確的是以應答碼(如220開頭,后面接一空格,再接問候字符串)
   {
    return readline();
   }
   return strmsg;
  }


  /// <summary>
  /// 發送命令并獲取應答碼和最后一行應答字符串
  /// </summary>
  /// <param name="strcommand">命令</param>
  private void sendcommand(string strcommand)
  {
   byte[] cmdbytes =
   encoding.ascii.getbytes((strcommand+"/r/n").tochararray());
   socketcontrol.send(cmdbytes, cmdbytes.length, 0);
   readreply();
  }

  #endregion
 }
}


 

最大的網站源碼資源下載站,

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平陆县| 乌海市| 保靖县| 遵义县| 三台县| 陕西省| 盐亭县| 德兴市| 兴文县| 荆州市| 金川县| 陕西省| 临猗县| 当涂县| 洮南市| 延吉市| 防城港市| 鹿邑县| 通榆县| 阳西县| 唐海县| 乐平市| 得荣县| 华亭县| 凤城市| 肇庆市| 富民县| 长白| 青河县| 古交市| 三江| 南丹县| 山丹县| 百色市| 武冈市| 泉州市| 大兴区| 武城县| 大邑县| 康马县| 阆中市|