閱讀571 返回首頁    go 技術社區[雲棲]


FTP上傳文件示例

 首先,你需要一個測試環境,我在自己的機器上搭建了一個FTP Server,搭建Server有一些比較優秀的軟件,例如:Crob FTP Server,不過這是一個收費軟件,雖然提供試用版,但要在公網上使用的話,還是買個注冊號吧!

    下載地址: Crob FTP Server V3.7.0 Build 196 簡體中文版 https://www.skycn.com/soft/11246.html

    中文軟件,大家都是學技術的,怎麼搭建我就不說了。

    相當傻瓜式的,上傳代碼隻有寥寥幾行:

  1. FTPClient fc = new FTPClient();
  2. fc.RemoteHost = "10.6.133.145"// 這裏隻能用IP,而不能用機器名。當然,你可以利用工具類通過機器名獲取到IP地址
  3. fc.RemotePath = "/u01/upload";  // FTP服務器的虛擬目錄
  4. fc.RemotePort = 21;
  5. fc.RemoteUser = "admin";
  6. fc.RemotePass = "123";
  7. fc.Connect();
  8. fc.Put("c:/1.txt");
  9. fc.DisConnect();

    這裏要注意的是,虛擬目錄/u01/upload對應的路徑需要有寫文件操作的權限,你可以在屬性中配置賬號的寫權限(我直接共享了該文件夾,並允許everyone賬號完全控製)。

    這裏提供FTPClient.cs的源碼,來源一時半會兒找不到了:

  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. using System.IO;
  6. namespace TimerServer
  7. {
  8.     /// <summary>
  9.     /// FTPClient 的摘要說明。
  10.     /// </summary>
  11.     public class FTPClient
  12.     {
  13.         #region 構造函數
  14.         /**//// <summary>
  15.         /// 缺省構造函數
  16.         /// </summary>
  17.         public FTPClient()
  18.         {
  19.             strRemoteHost  = "";
  20.             strRemotePath  = "";
  21.             strRemoteUser  = "";
  22.             strRemotePass  = "";
  23.             strRemotePort  = 21;
  24.             bConnected
  25.                 = false;
  26.         }
  27.     /**//// <summary>
  28.     /// 構造函數
  29.     /// </summary>
  30.     /// <param name="remoteHost"></param>
  31.     /// <param name="remotePath"></param>
  32.     /// <param name="remoteUser"></param>
  33.     /// <param name="remotePass"></param>
  34.     /// <param name="remotePort"></param>
  35.     public FTPClient( string remoteHost, string remotePath, string remoteUser, string remotePass, int remotePort )
  36.     {
  37.         strRemoteHost  = remoteHost;
  38.         strRemotePath  = remotePath;
  39.         strRemoteUser  = remoteUser;
  40.         strRemotePass  = remotePass;
  41.         strRemotePort  = remotePort;
  42.         Connect();
  43.     }
  44.     #endregion
  45.     #region 登陸
  46.     /**//// <summary>
  47.     /// FTP服務器IP地址
  48.     /// </summary>
  49.     private string strRemoteHost;
  50.     public string RemoteHost
  51.     {
  52.         get
  53.         {
  54.             return strRemoteHost;
  55.         }
  56.         set
  57.         {
  58.             strRemoteHost = value;
  59.         }
  60.     }
  61.     /**//// <summary>
  62.     /// FTP服務器端口
  63.     /// </summary>
  64.     private int strRemotePort;
  65.     public int RemotePort
  66.     {
  67.         get
  68.         {
  69.             return strRemotePort;
  70.         }
  71.         set
  72.         {
  73.             strRemotePort = value;
  74.         }
  75.     }
  76.     /**//// <summary>
  77.     /// 當前服務器目錄
  78.     /// </summary>
  79.     private string strRemotePath;
  80.     public string RemotePath
  81.     {
  82.         get
  83.         {
  84.             return strRemotePath;
  85.         }
  86.         set
  87.         {
  88.             strRemotePath = value;
  89.         }
  90.     }
  91.     /**//// <summary>
  92.     /// 登錄用戶賬號
  93.     /// </summary>
  94.     private string strRemoteUser;
  95.     public string RemoteUser
  96.     {
  97.         set
  98.         {
  99.             strRemoteUser = value;
  100.         }
  101.     }
  102.     /**//// <summary>
  103.     /// 用戶登錄密碼
  104.     /// </summary>
  105.     private string strRemotePass;
  106.     public string RemotePass
  107.     {
  108.         set
  109.         {
  110.             strRemotePass = value;
  111.         }
  112.     }
  113.     /**//// <summary>
  114.     /// 是否登錄
  115.     /// </summary>
  116.     private Boolean bConnected;
  117.     public bool Connected
  118.     {
  119.         get
  120.         {
  121.             return bConnected;
  122.         }
  123.     }
  124.     #endregion
  125.     #region 鏈接
  126.     /**//// <summary>
  127.     /// 建立連接 
  128.     /// </summary>
  129.     public void Connect()
  130.     {
  131.         socketControl = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
  132.         IPEndPoint ep = new IPEndPoint(IPAddress.Parse(RemoteHost), strRemotePort);
  133.         // 鏈接
  134.         try
  135.         {
  136.             socketControl.Connect(ep);
  137.         }
  138.         catch(Exception)
  139.         {
  140.             throw new IOException("Couldn't connect to remote server");
  141.         }
  142.     // 獲取應答碼
  143.     ReadReply();
  144.     if(iReplyCode != 220)
  145.     {
  146.         DisConnect();
  147.         throw new IOException(strReply.Substring(4));
  148.     }
  149.     // 登陸
  150.     SendCommand("USER "+strRemoteUser);
  151.     if( !(iReplyCode == 331 || iReplyCode == 230) )
  152.     {
  153.         CloseSocketConnect();//關閉連接
  154.         throw new IOException(strReply.Substring(4));
  155.     }
  156.     if( iReplyCode != 230 )
  157.     {
  158.         SendCommand("PASS "+strRemotePass);
  159.         if( !(iReplyCode == 230 || iReplyCode == 202) )
  160.         {
  161.             CloseSocketConnect();//關閉連接
  162.             throw new IOException(strReply.Substring(4));
  163.         }
  164.     }
  165.     bConnected = true;
  166.     // 切換到目錄
  167.     ChDir(strRemotePath);
  168.     }
  169.     
  170.     /**//// <summary>
  171.     /// 關閉連接
  172.     /// </summary>
  173.     public void DisConnect()
  174.     {
  175.         if( socketControl != null )
  176.         {
  177.             SendCommand("QUIT");
  178.         }
  179.         CloseSocketConnect();
  180.     }
  181.     #endregion
  182.     #region 傳輸模式
  183.     /**//// <summary>
  184.     /// 傳輸模式:二進製類型、ASCII類型
  185.     /// </summary>
  186.     public enum TransferType {Binary,ASCII};
  187.     /**//// <summary>
  188.     /// 設置傳輸模式
  189.     /// </summary>
  190.     /// <param name="ttType">傳輸模式</param>
  191.     public void SetTransferType(TransferType ttType)
  192.     {
  193.         if(ttType == TransferType.Binary)
  194.         {
  195.             SendCommand("TYPE I");//binary類型傳輸
  196.         }
  197.         else
  198.         {
  199.             SendCommand("TYPE A");//ASCII類型傳輸
  200.         }
  201.         if (iReplyCode != 200)
  202.         {
  203.             throw new IOException(strReply.Substring(4));
  204.         }
  205.         else
  206.         {
  207.             trType = ttType;
  208.         }
  209.     }
  210.     /**//// <summary>
  211.     /// 獲得傳輸模式
  212.     /// </summary>
  213.     /// <returns>傳輸模式</returns>
  214.     public TransferType GetTransferType()
  215.     {
  216.         return trType;
  217.     }
  218.     
  219.         #endregion
  220.     #region 文件操作
  221.     /**//// <summary>
  222.     /// 獲得文件列表
  223.     /// </summary>
  224.     /// <param name="strMask">文件名的匹配字符串</param>
  225.     /// <returns></returns>
  226.     public string[] Dir(string strMask)
  227.     {
  228.         // 建立鏈接
  229.         if(!bConnected)
  230.         {
  231.             Connect();
  232.         }
  233.     //建立進行數據連接的socket
  234.     Socket socketData = CreateDataSocket();
  235.     //傳送命令
  236.     SendCommand("NLST " + strMask);
  237.     //分析應答代碼
  238.     if(!(iReplyCode == 150 || iReplyCode == 125 || iReplyCode == 226))
  239.     {
  240.         throw new IOException(strReply.Substring(4));
  241.     }
  242.     //獲得結果
  243.     strMsg = "";
  244.     while(true)
  245.     {
  246.         int iBytes = socketData.Receive(buffer, buffer.Length, 0);
  247.         strMsg += ASCII.GetString(buffer, 0, iBytes);
  248.         if(iBytes < buffer.Length)
  249.         {
  250.             break;
  251.         }
  252.     }
  253.     char[] seperator = {'/n'};
  254.     string[] strsFileList = strMsg.Split(seperator);
  255.     socketData.Close();//數據socket關閉時也會有返回碼
  256.     if(iReplyCode != 226)
  257.     {
  258.         ReadReply();
  259.         if(iReplyCode != 226)
  260.         {
  261.             throw new IOException(strReply.Substring(4));
  262.         }
  263.     }
  264.     return strsFileList;
  265.     }
  266.     
  267.     /**//// <summary>
  268.     /// 獲取文件大小
  269.     /// </summary>
  270.     /// <param name="strFileName">文件名</param>
  271.     /// <returns>文件大小</returns>
  272.     private long GetFileSize(string strFileName)
  273.     {
  274.         if(!bConnected)
  275.         {
  276.             Connect();
  277.         }
  278.         SendCommand("SIZE " + Path.GetFileName(strFileName));
  279.         long lSize=0;
  280.         if(iReplyCode == 213)
  281.         {
  282.             lSize = Int64.Parse(strReply.Substring(4));
  283.         }
  284.         else
  285.         {
  286.             throw new IOException(strReply.Substring(4));
  287.         }
  288.         return lSize;
  289.     }
  290.     /**//// <summary>
  291.     /// 刪除
  292.     /// </summary>
  293.     /// <param name="strFileName">待刪除文件名</param>
  294.     public void Delete(string strFileName)
  295.     {
  296.         if(!bConnected)
  297.         {
  298.             Connect();
  299.         }
  300.         SendCommand("DELE "+strFileName);
  301.         if(iReplyCode != 250)
  302.         {
  303.             throw new IOException(strReply.Substring(4));
  304.         }
  305.     }
  306.     
  307.     /**//// <summary>
  308.     /// 重命名(如果新文件名與已有文件重名,將覆蓋已有文件)
  309.     /// </summary>
  310.     /// <param name="strOldFileName">舊文件名</param>
  311.     /// <param name="strNewFileName">新文件名</param>
  312.     public void Rename(string strOldFileName,string strNewFileName)
  313.     {
  314.         if(!bConnected)
  315.         {
  316.             Connect();
  317.         }
  318.         SendCommand("RNFR "+strOldFileName);
  319.         if(iReplyCode != 350)
  320.         {
  321.             throw new IOException(strReply.Substring(4));
  322.         }
  323.         //  如果新文件名與原有文件重名,將覆蓋原有文件
  324.         SendCommand("RNTO "+strNewFileName);
  325.         if(iReplyCode != 250)
  326.         {
  327.             throw new IOException(strReply.Substring(4));
  328.         }
  329.     }
  330.     #endregion
  331.     #region 上傳和下載
  332.     /**//// <summary>
  333.     /// 下載一批文件
  334.     /// </summary>
  335.     /// <param name="strFileNameMask">文件名的匹配字符串</param>
  336.     /// <param name="strFolder">本地目錄(不得以/結束)</param>
  337.     public void Get(string strFileNameMask,string strFolder)
  338.     {
  339.         if(!bConnected)
  340.         {
  341.             Connect();
  342.         }
  343.         string[] strFiles = Dir(strFileNameMask);
  344.         foreach(string strFile in strFiles)
  345.         {
  346.             if(!strFile.Equals(""))//一般來說strFiles的最後一個元素可能是空字符串
  347.             {
  348.                 Get(strFile,strFolder,strFile);
  349.             }
  350.         }
  351.     }
  352.     
  353.     /**//// <summary>
  354.     /// 下載一個文件
  355.     /// </summary>
  356.     /// <param name="strRemoteFileName">要下載的文件名</param>
  357.     /// <param name="strFolder">本地目錄(不得以/結束)</param>
  358.     /// <param name="strLocalFileName">保存在本地時的文件名</param>
  359.     public void Get(string strRemoteFileName,string strFolder,string strLocalFileName)
  360.     {
  361.         if(!bConnected)
  362.         {
  363.             Connect();
  364.         }
  365.         SetTransferType(TransferType.Binary);
  366.         if (strLocalFileName.Equals(""))
  367.         {
  368.             strLocalFileName = strRemoteFileName;
  369.         }
  370.         if(!File.Exists(strFolder + "//" +strLocalFileName))
  371.         {
  372.             Stream st = File.Create(strFolder + "//" +strLocalFileName);
  373.             st.Close();
  374.         }
  375.         FileStream output = new 
  376.             FileStream(strFolder + "//" + strLocalFileName,FileMode.Create);
  377.         Socket socketData = CreateDataSocket();
  378.         SendCommand("RETR " + strRemoteFileName);
  379.         if(!(iReplyCode == 150 || iReplyCode == 125
  380.             || iReplyCode == 226 || iReplyCode == 250))
  381.         {
  382.             throw new IOException(strReply.Substring(4));
  383.         }
  384.         while(true)
  385.         {
  386.             int iBytes = socketData.Receive(buffer, buffer.Length, 0);
  387.             output.Write(buffer,0,iBytes);
  388.             if(iBytes <= 0)
  389.             {
  390.                 break;
  391.             }
  392.         }
  393.         output.Close();
  394.         if (socketData.Connected)
  395.         {
  396.             socketData.Close();
  397.         }
  398.         if(!(iReplyCode == 226 || iReplyCode == 250))
  399.         {
  400.             ReadReply();
  401.             if(!(iReplyCode == 226 || iReplyCode == 250))
  402.             {
  403.                 throw new IOException(strReply.Substring(4));
  404.             }
  405.         }
  406.     }
  407.     
  408.     /**//// <summary>
  409.     /// 上傳一批文件
  410.     /// </summary>
  411.     /// <param name="strFolder">本地目錄(不得以/結束)</param>
  412.     /// <param name="strFileNameMask">文件名匹配字符(可以包含*和?)</param>
  413.     public void Put(string strFolder,string strFileNameMask)
  414.     {
  415.         string[] strFiles = Directory.GetFiles(strFolder,strFileNameMask);
  416.         foreach(string strFile in strFiles)
  417.         {
  418.             //strFile是完整的文件名(包含路徑)
  419.             Put(strFile);
  420.         }
  421.     }
  422.     
  423.     /**//// <summary>
  424.     /// 上傳一個文件
  425.     /// </summary>
  426.     /// <param name="strFileName">本地文件名</param>
  427.     public void Put(string strFileName)
  428.     {
  429.         if(!bConnected)
  430.         {
  431.             Connect();
  432.         }
  433.         Socket socketData = CreateDataSocket();
  434.         SendCommand("STOR "+Path.GetFileName(strFileName));
  435.         if( !(iReplyCode == 125 || iReplyCode == 150) )
  436.         {
  437.             throw new IOException(strReply.Substring(4));
  438.         }
  439.         FileStream input = new 
  440.             FileStream(strFileName,FileMode.Open);
  441.         int iBytes = 0;
  442.         while ((iBytes = input.Read(buffer,0,buffer.Length)) > 0)
  443.         {
  444.             socketData.Send(buffer, iBytes, 0);
  445.         }
  446.         input.Close();
  447.         if (socketData.Connected)
  448.         {
  449.             socketData.Close();
  450.         }
  451.         if(!(iReplyCode == 226 || iReplyCode == 250))
  452.         {
  453.             ReadReply();
  454.             if(!(iReplyCode == 226 || iReplyCode == 250))
  455.             {
  456.                 throw new IOException(strReply.Substring(4));
  457.             }
  458.         }
  459.     }
  460.     
  461.         #endregion
  462.     #region 目錄操作
  463.     /**//// <summary>
  464.     /// 創建目錄
  465.     /// </summary>
  466.     /// <param name="strDirName">目錄名</param>
  467.     public void MkDir(string strDirName)
  468.     {
  469.         if(!bConnected)
  470.         {
  471.             Connect();
  472.         }
  473.         SendCommand("MKD "+strDirName);
  474.         if(iReplyCode != 257)
  475.         {
  476.             throw new IOException(strReply.Substring(4));
  477.         }
  478.     }
  479.     
  480.     /**//// <summary>
  481.     /// 刪除目錄
  482.     /// </summary>
  483.     /// <param name="strDirName">目錄名</param>
  484.     public void RmDir(string strDirName)
  485.     {
  486.         if(!bConnected)
  487.         {
  488.             Connect();
  489.         }
  490.         SendCommand("RMD "+strDirName);
  491.         if(iReplyCode != 250)
  492.         {
  493.             throw new IOException(strReply.Substring(4));
  494.         }
  495.     }
  496.     
  497.     /**//// <summary>
  498.     /// 改變目錄
  499.     /// </summary>
  500.     /// <param name="strDirName">新的工作目錄名</param>
  501.     public void ChDir(string strDirName)
  502.     {
  503.         if(strDirName.Equals(".") || strDirName.Equals(""))
  504.         {
  505.             return;
  506.         }
  507.         if(!bConnected)
  508.         {
  509.             Connect();
  510.         }
  511.         SendCommand("CWD "+strDirName);
  512.         if(iReplyCode != 250)
  513.         {
  514.             throw new IOException(strReply.Substring(4));
  515.         }
  516.         this.strRemotePath = strDirName;
  517.     }
  518.     
  519.         #endregion
  520.     #region 內部變量
  521.     /**//// <summary>
  522.     /// 服務器返回的應答信息(包含應答碼)
  523.     /// </summary>
  524.     private string strMsg;
  525.     /**//// <summary>
  526.     /// 服務器返回的應答信息(包含應答碼)
  527.     /// </summary>
  528.     private string strReply;
  529.     /**//// <summary>
  530.     /// 服務器返回的應答碼
  531.     /// </summary>
  532.     private int iReplyCode;
  533.     /**//// <summary>
  534.     /// 進行控製連接的socket
  535.     /// </summary>
  536.     private Socket socketControl;
  537.     /**//// <summary>
  538.     /// 傳輸模式
  539.     /// </summary>
  540.     private TransferType trType;
  541.     /**//// <summary>
  542.     /// 接收和發送數據的緩衝區
  543.     /// </summary>
  544.     private static int BLOCK_SIZE = 512;
  545.     Byte[] buffer = new Byte[BLOCK_SIZE];
  546.     /**//// <summary>
  547.     /// 編碼方式
  548.     /// </summary>
  549.     Encoding ASCII = Encoding.ASCII;
  550.     #endregion
  551.     #region 內部函數
  552.         /**//// <summary>
  553.         /// 將一行應答字符串記錄在strReply和strMsg
  554.         /// 應答碼記錄在iReplyCode
  555.         /// </summary>
  556.         private void ReadReply()
  557.         {
  558.             strMsg = "";
  559.             strReply = ReadLine();
  560.             iReplyCode = Int32.Parse(strReply.Substring(0,3));
  561.         }
  562.     /**//// <summary>
  563.     /// 建立進行數據連接的socket
  564.     /// </summary>
  565.     /// <returns>數據連接socket</returns>
  566.     private Socket CreateDataSocket()
  567.     {
  568.         SendCommand("PASV");
  569.         if(iReplyCode != 227)
  570.         {
  571.             throw new IOException(strReply.Substring(4));
  572.         }
  573.         int index1 = strReply.IndexOf('(');
  574.         int index2 = strReply.IndexOf(')');
  575.         string ipData = 
  576.             strReply.Substring(index1+1,index2-index1-1);
  577.         int[] parts = new int[6];
  578.         int len = ipData.Length;
  579.         int partCount = 0;
  580.         string buf="";
  581.         for (int i = 0; i < len && partCount <= 6; i++)
  582.         {
  583.             char ch = Char.Parse(ipData.Substring(i,1));
  584.             if (Char.IsDigit(ch))
  585.                 buf+=ch;
  586.             else if (ch != ',')
  587.             {
  588.                 throw new IOException("Malformed PASV strReply: " + 
  589.                     strReply);
  590.             }
  591.             if (ch == ',' || i+1 == len)
  592.             {
  593.                 try
  594.                 {
  595.                     parts[partCount++] = Int32.Parse(buf);
  596.                     buf="";
  597.                 }
  598.                 catch (Exception)
  599.                 {
  600.                     throw new IOException("Malformed PASV strReply: " + 
  601.                         strReply);
  602.                 }
  603.             }
  604.         }
  605.         string ipAddress = parts[0] + "."+ parts[1]+ "." +
  606.             parts[2] + "." + parts[3];
  607.         int port = (parts[4] << 8) + parts[5];
  608.         Socket s = new 
  609.             Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
  610.         IPEndPoint ep = new 
  611.             IPEndPoint(IPAddress.Parse(ipAddress), port);
  612.         try
  613.         {
  614.             s.Connect(ep);
  615.         }
  616.         catch(Exception)
  617.         {
  618.             throw new IOException("Can't connect to remote server");
  619.         }
  620.         return s;
  621.     }
  622.     /**//// <summary>
  623.     /// 關閉socket連接(用於登錄以前)
  624.     /// </summary>
  625.     private void CloseSocketConnect()
  626.     {
  627.         if(socketControl!=null)
  628.         {
  629.             socketControl.Close();
  630.             socketControl = null;
  631.         }
  632.         bConnected = false;
  633.     }
  634.     /**//// <summary>
  635.     /// 讀取Socket返回的所有字符串
  636.     /// </summary>
  637.     /// <returns>包含應答碼的字符串行</returns>
  638.     private string ReadLine()
  639.     {
  640.         while(true)
  641.         {
  642.             int iBytes = socketControl.Receive(buffer, buffer.Length, 0);
  643.             strMsg += ASCII.GetString(buffer, 0, iBytes);
  644.             if(iBytes < buffer.Length)
  645.             {
  646.                 break;
  647.             }
  648.         }
  649.         char[] seperator = {'/n'};
  650.         string[] mess = strMsg.Split(seperator);
  651.         if(strMsg.Length > 2)
  652.         {
  653.             strMsg = mess[mess.Length-2];
  654.             //seperator[0]是10,換行符是由13和0組成的,分隔後10後麵雖沒有字符串,
  655.             //但也會分配為空字符串給後麵(也是最後一個)字符串數組,
  656.             //所以最後一個mess是沒用的空字符串
  657.             //但為什麼不直接取mess[0],因為隻有最後一行字符串應答碼與信息之間有空格
  658.         }
  659.         else
  660.         {
  661.             strMsg = mess[0];
  662.         }
  663.         if(!strMsg.Substring(3,1).Equals(" "))//返回字符串正確的是以應答碼(如220開頭,後麵接一空格,再接問候字符串)
  664.         {
  665.             return ReadLine();
  666.         }
  667.         return strMsg;
  668.     }
  669.     /**//// <summary>
  670.     /// 發送命令並獲取應答碼和最後一行應答字符串
  671.     /// </summary>
  672.     /// <param name="strCommand">命令</param>
  673.     private void SendCommand(String strCommand)
  674.     {
  675.         Byte[] cmdBytes = 
  676.             Encoding.ASCII.GetBytes((strCommand+"/r/n").ToCharArray());
  677.         socketControl.Send(cmdBytes, cmdBytes.Length, 0);
  678.         ReadReply();
  679.     }
  680.     #endregion
  681.     } 
  682. }

該工具類還提供一些文件操作,注釋也比較豐富,再次向這個類的作者致敬。

-------------------------------------------------

這個類有一種無法處理的情況,如果FTP服務器使用SSL或者SSH2安全協議搭建的Secure FTP Server,就無法鏈接上了。在網上找到一個可行的辦法,見文章:https://blog.csdn.net/venus0314/archive/2006/09/21/1262386.aspx

原理是利用psftp.exe工具來上傳文件,上傳過程和通訊協議都被隱藏了,通過流寫DOS命令來文件的複製與粘貼的操作。我寫過測試類,但沒有成功,不過應該是一個可行的辦法,正在想辦法研究,如果有結果將會在博客中放出來。

psftp.exe比較難找,但還是找的到的。google吧!

 

-------------------------------------------------

Updated On 2008-10-21

FTPClient類中沒有包含Append的功能,即將本地文件的內容追加到遠程文件上的功能。這裏,我放上來一個通過了測試的Append方法:

  1. /// <summary>
  2. /// 文件Append操作
  3. /// </summary>
  4. /// <param name="strFileName">被Append的本地文件名,要求與遠程文件名一致</param>
  5. public void Append(string strFileName)
  6. {
  7.     if(!bConnected)
  8.         Connect();
  9.     Socket socketData = CreateDataSocket();
  10.     SetTransferType(TransferType.Binary);
  11.     SendCommand("APPE " + Path.GetFileName(strFileName));       
  12.     if( !(iReplyCode == 125 || iReplyCode == 150) )
  13.     {
  14.         throw new IOException(strReply.Substring(4));
  15.     }
  16.     FileStream input = new FileStream(strFileName,FileMode.Open);
  17.     int iBytes = 0;
  18.     while ((iBytes = input.Read(buffer,0,buffer.Length)) > 0)
  19.     {
  20.         socketData.Send(buffer, iBytes, 0);
  21.     }
  22.     input.Close();
  23.     if (socketData.Connected)
  24.     {
  25.         socketData.Close();
  26.     }
  27.     if(!(iReplyCode == 226 || iReplyCode == 250))
  28.     {
  29.         ReadReply();
  30.         if(!(iReplyCode == 226 || iReplyCode == 250))
  31.         {
  32.             throw new IOException(strReply.Substring(4));
  33.         }
  34.     }
  35. }

    這裏沒有提供指定FTP遠程服務器上文件名的功能,要求你上傳的本地文件名與遠程數據庫文件名一致。事實上,我做過指定遠程文件名的嚐試,即將命令該為“APPE local-file remote-file”的格式,但我發現,結果是,它把本地文件的內容傳上去後,生成了一個文件名為local-file與remote-file拚接後的新文件,令人費解!!

最後更新:2017-04-02 00:06:45

  上一篇:go 逐行處理數據時避免死循環
  下一篇:go PMP:“拍好馬屁”,改變人生! (1)