2011-12-13 166 views
1

Problem:當我一次只上傳一個文件時,文件上傳正常,但是當我使用多個後臺工作人員上傳文件到FTP服務器時,我得到異常:多個線程上傳文件到FileZilla FTP服務器返回錯誤550文件不可用

  • ex {"The remote server returned an error: (550) File unavailable (e.g., file not found, no access)."} System.Exception {System.Net.WebException}

而且只有一些文件被上傳。我非常肯定文件在該位置退出,事實上在另一個運行中,它抱怨的文件不存在被下載,但錯誤在另一個文件上移動。

Code Description: 在下面的代碼我下載從一個FTP服務器上的文件,並把它放在另一個。此代碼位於BackgroundsWorker_DoWork方法內。後臺工作人員正在循環內部創建。

void imageDownloadWorker_DoWork(object sender, DoWorkEventArgs e) 
     { 
      string[] ftpInfo = (string[])e.Argument; 
      try 
      { 

       ///////////////////////////Downloading/////////////////////////////////////// 
       string uri = String.Format("ftp://{0}/{1}/images/{2}", ftpInfo[1], ftpInfo[2], ftpInfo[5]); 

      FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri); 
      request.Method = WebRequestMethods.Ftp.DownloadFile; 
      request.UseBinary = true; 
      request.Credentials = new NetworkCredential(ftpInfo[3], ftpInfo[4]); 

      FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 
      Stream ftpStream = response.GetResponseStream(); 

      long cl = response.ContentLength; 
      int bufferSize = 4096; 
      int readCount = 0; 
      byte[] buffer = new byte[bufferSize]; 
      MemoryStream memStream = new MemoryStream(); 
      readCount = ftpStream.Read(buffer, 0, bufferSize); 
      while (readCount > 0) 
      { 
       memStream.Write(buffer, 0, readCount); 
       readCount = ftpStream.Read(buffer, 0, bufferSize); 
      } 
      response.Close(); 
      ///////////////////////////Uploading/////////////////////////////////////// 
      string uri1 = String.Format("ftp://{0}/{1}/{2}", "127.0.0.1", string.Empty, ftpInfo[5]); 
      FtpWebRequest request1 = (FtpWebRequest)WebRequest.Create(uri1); 
      request1.Credentials = new NetworkCredential("user", "password"); 
      request1.KeepAlive = false; 
      request1.Method = WebRequestMethods.Ftp.UploadFile; 
      request1.UseBinary = true; 
      request1.ContentLength = memStream.Length; 
      int buffLength = 4096; 
      byte[] buff = new byte[buffLength]; 
      int contentLen; 

      // Stream to which the file to be upload is written 
      Stream strm = request1.GetRequestStream(); 
      memStream.Seek(0, SeekOrigin.Begin); 
      contentLen = memStream.Read(buff, 0, buffLength); 
      // Till Stream content ends 
      while (contentLen != 0) 
      { 
       // Write Content from the file stream to the FTP Upload Stream 
       strm.Write(buff, 0, contentLen); 
       contentLen = memStream.Read(buff, 0, buffLength); 
      } 

      // Close the file stream and the Request Stream 
      strm.Close(); 
      ftpStream.Close(); 
      memStream.Close(); 

     } 
     catch(Exception ex) 
     { 
      MessageBox.Show("While Downloading File " + ftpInfo[5] + " " + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      e.Result = null; 
      return; 
     } 

Related Thread

Edit: 在文件服務器吉拉有一個選項常規設置>性能比較設置>主題我已經設置20個它沒有任何區別的數量。

+0

還處理您的Stream對象strm關閉不夠好 – MethodMan

+0

檢查我的答案以下鏈接,這是正確的順序代碼應該在http://stackoverflow.com/questions/8605710/get-my-application-使用c-sharp – PUG

回答

0

你的代碼可能沒有問題。該錯誤是一個權限錯誤。

+0

這不是一個真正的權限錯誤,但是你說得對,我的代碼沒有錯。 – PUG

+0

酷至少你可以相信你寫的代碼很好..對不起,我無法爲你提供正確的答案。謝謝 – MethodMan

0

在黑暗中完成刺穿,但上傳目標服務器是否具有每IP限制的連接?如果是這樣,您可能會因爲超過單個IP地址的併發連接限制而受到影響。

+0

在文件Zill服務器中有一個選項'常規設置>性能設置>線程數'我已經把它設置爲20它沒有任何區別。 – PUG

+0

我對File Zill(a?)服務器不熟悉,但假設File Zill甚至有一些併發數,服務器運行的線程數和允許的每個客戶端IP的併發連接數是兩個不同的事情連接限制。 –

相關問題