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;
}
Edit:
在文件服務器吉拉有一個選項常規設置>性能比較設置>主題我已經設置20個它沒有任何區別的數量。
還處理您的Stream對象strm關閉不夠好 – MethodMan
檢查我的答案以下鏈接,這是正確的順序代碼應該在http://stackoverflow.com/questions/8605710/get-my-application-使用c-sharp – PUG