多个线程向filezilla上传文件,ftp server返回错误550文件不可用
本文关键字:文件 返回 server 错误 ftp 线程 filezilla | 更新日期: 2023-09-27 18:17:56
Problem:
当我ftp一次只上传一个文件时,文件上传得很好,但是当我使用多个后台工作人员上传文件到ftp服务器时,我得到异常:
- ex{"远程服务器返回错误:(550)文件不可用(例如,未找到文件,无法访问)。"}系统。异常{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;
}
相关的线程
Edit:
在File Zilla Server中有一个选项:常规设置>性能设置>线程数。我把它设置为20,没有任何区别。
也许你的代码没有问题。
完全是在黑暗中捅刀子,但是上传目标服务器有每个IP连接限制吗?如果是这样,您可能会因为超过单个IP地址的并发连接限制而违反此规定。