我的C#程序有问题。我有一个大的zip存档(大约4Gb),它分成小块,实际上每块是100 Mb。因此,当我的程序开始上传文件时,它可以正常工作,直到第14个片段开始上传为止。我的程序仅上传该文件的〜90 Mb,然后收到错误“远程服务器返回错误:(451)处理中的本地错误”。我的C#程序检测文件是否未成功上传并尝试上传,因此出现了永无休止的循环。
private static void SendToFtp(AppConfig cfg, string zipName)
{
Console.WriteLine("Starting FTP upload");
NetworkCredential creds = new NetworkCredential(cfg.FtpUserName, cfg.FtpPassword);
try
{
StringBuilder sb = new StringBuilder();
DirectoryInfo di = new DirectoryInfo(cfg.ZipFolder);
byte[] buffer = null;
foreach (var file in di.EnumerateFiles())
{
using (Stream st = File.OpenRead(file.FullName))
using (BinaryReader br = new BinaryReader(st))
{
bool success = false;
while (!success)
{
br.BaseStream.Seek(0, SeekOrigin.Begin);
buffer = br.ReadBytes((int)file.Length);
sb.Append($"ftp://{cfg.FtpHost}/{file.Name}");
request = (FtpWebRequest)WebRequest.Create(sb.ToString());
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = false;
request.UsePassive = false;
request.KeepAlive = false;
request.ServicePoint.ConnectionLimit = 1000;
request.Credentials = new NetworkCredential(cfg.FtpUserName, cfg.FtpPassword);
using (Stream requestStream = request.GetRequestStream())
{
try
{
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Flush();
requestStream.Close();
var resp = (FtpWebResponse)request.GetResponse();
success = !success;
Console.WriteLine($"======================{file.Name}=======================");
Console.WriteLine($"Message:{resp.StatusDescription}");
Console.WriteLine($" Status code: {resp.StatusCode}");
Console.WriteLine($"========================================================");
}
catch (Exception ex)
{
request.Abort();
Console.WriteLine($"MSG: {ex.Message}");
}
}
sb.Clear();
}
}
}
Console.WriteLine("FTP upload finished");
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}; {ex.Source}");
}
}
所以,有人可以帮我还是指出我错了。 谢谢!