我正在使用以下代码从FTP下载文件:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(downloadPath);
request.UsePassive = false;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(_ftpUser, _ftpPass);
using (var response = (FtpWebResponse)request.GetResponse())
using (var responseStream = response.GetResponseStream())
using (var writer = new FileStream(savePath, FileMode.Create))
{
long length = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[2048];
readCount = responseStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
writer.Write(buffer, 0, readCount);
readCount = responseStream.Read(buffer, 0, bufferSize);
}
}
它可以工作,但是文件很大,当我使用Filezilla客户端下载文件时,速度要快得多。
这与缓冲区大小有关吗?有什么方法可以使速度更快?