我将大约10GB的大型tar文件上传到ftp服务器。这需要花费大量时间并引发以下错误:无法将数据写入传输连接:远程主机强行关闭现有连接。
我提到了以下内容:
Why is my ftp upload method so slow?
https://www.codeproject.com/Questions/204070/how-to-write-c-code-to-increase-the-ftp-file-uploa
以下是代码:
FileStream fs = null;
Stream rs = null;
try
{
string file = args[0].Replace("---"," ");
string ftpServer = args[1].ToString();
string uploadFileName = new FileInfo(file).Name;
string uploadUrl = ftpServer;
Console.WriteLine("Start Time: {0}", DateTime.Now);
Console.WriteLine("File Name: {0}",file);
fs = new FileStream(file, FileMode.Open, FileAccess.Read);
string ftpUrl = string.Format("{0}/{1}", uploadUrl, uploadFileName);
FtpWebRequest requestObj = FtpWebRequest.Create(ftpUrl) as FtpWebRequest;
requestObj.Timeout = -1; // <---- -1 is Infinite
requestObj.Method = WebRequestMethods.Ftp.UploadFile;
rs = requestObj.GetRequestStream();
byte[] buffer = new byte[4096];
//byte[] buffer = new byte[16000]; // <--- 16k
int read = 0;
while ((read = fs.Read(buffer, 0, buffer.Length)) != 0)
{
rs.Write(buffer, 0, read);
}
rs.Flush();
}
catch (Exception ex)
{
Console.WriteLine("File upload/transfer Failed.\r\nError Message:\r\n" + ex.Message);
}
finally
{
if (fs != null)
{
fs.Close();
fs.Dispose();
}
if (rs != null)
{
rs.Close();
rs.Dispose();
}
}
Console.WriteLine("End Time: {0}", DateTime.Now);
Console.WriteLine("Exiting the application.. press any key to continue");
Console.ReadLine();
更新:我将文件上传到FTP DropBox并且无法访问日志。在尝试通过像FileZilla这样的客户端上传时,速度更快。保管箱的限制为300 GB。是否有可能找到转移率?
由于我是初学者,请解释解决方案以及我的理解。 提前致谢。