Ftp Upload出现异常无法将数据写入传输连接.远程主机已强制关闭现有连接



我有一个windows窗体应用程序,在该应用程序中我使用后台工作人员来ftp上传文件。成功上传CCD_ 1文件后,在CCD_ 2只有7.8kb大小的文件上出现错误。

string uri1;
ftpInfoUpload = LoadHostedSiteData(hs);
ftpInfoUpload[5] = imgRow["Filename"].ToString();
uri1 = String.Format("ftp://{0}/{1}/images/{2}", ftpInfoUpload[1], ftpInfoUpload[2], ftpInfoUpload[5]);
requestUpload = (FtpWebRequest)WebRequest.Create(uri1);
requestUpload.UsePassive = false;
requestUpload.UseBinary = true;
requestUpload.Method = WebRequestMethods.Ftp.UploadFile;
requestUpload.Credentials = new NetworkCredential(ftpInfoUpload[3], ftpInfoUpload[4]);

requestUpload.ContentLength = memStream.Length;
byte[] buff = new byte[bufferSize];
int contentLen;
// Stream to which the file to be upload is written
Stream strm = requestUpload.GetRequestStream();
memStream.Seek(0, SeekOrigin.Begin);
contentLen = memStream.Read(buff, 0, bufferSize);
                            // 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, bufferSize);
}
//Close the file stream and the Request Stream
strm.Close();
strm.Dispose();
ftpStream.Close();
memStream.Close();
//responseUpload.Close();
responseDownload.Close();

想法发生了什么?

我已经设置了ftprequest.KeepAlive=true&设置ftprequest.ConnectionGroupName = "Some Value",这样底层代码就不必新建具有相同ftp服务器的连接。我在这里找到了这个解决方案。我也觉得这很有帮助。此外,请确保每次传输可能导致异常的文件时不要创建新的NetworkCredential对象。我已经测试了我的代码两次,传输了300个文件,似乎工作得完美而快速。设置KeepAlive=false会使传输变慢

最新更新