2011-12-13 167 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的并发连接数是两个不同的事情连接限制。 –

相关问题