将文件上传到filezilla ftp服务器的多个线程返回错误550文件不可用

时间:2011-12-13 14:55:44

标签: c# .net multithreading exception ftp

Problem:当我ftp一次只上传一个文件时,文件上传得很好,但是当我使用多个后台工作人员将文件上传到ftp服务器时,我得到例外:

  
      
  • ex {“远程服务器返回错误:(550)文件不可用   (例如,找不到文件,没有访问权限。)} 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: 在File Zilla Server中,有一个选项General Settings> Perfomance Settings>我已设置的线程数为20,它没有任何区别。

2 个答案:

答案 0 :(得分:0)

您的代码可能没有任何问题。该错误是权限错误。

答案 1 :(得分:0)

在黑暗中完成刺穿,但上传目标服务器是否有每个IP限制的连接?如果是这样,您可能会因为超过单个IP地址的并发连接限制而违反此规定。