我正在使用c#中的FTP功能来下载某些文件。 我的问题是下载速度很慢。
我对Filezilla没有任何问题,我的连接没问题。
我正在考虑FTP功能的实现,这是我的代码:
public static string DownloadFromList(string strParam,string filePath)
{
string[] FilesToDownload = strParam.Split('\n');
try
{
FtpWebRequest reqFTP;
foreach(string file in FilesToDownload)
{
string tmpfile = file;
if (tmpfile.Contains("\n"))
{
tmpfile = tmpfile.Replace('\n', ' ');
}
FileInfo fileToDownload = new FileInfo(tmpfile);
tmpfile = fileToDownload.Name;
FileStream outputStream = new FileStream(filePath.Trim() + "\\" + tmpfile.Trim(), FileMode.Create);
if (strDirectory.Trim() != "")
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strHost.Trim() + "/" + strDirectory.Trim() + "/" + tmpfile.Trim()));
}
else
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strHost.Trim() + "/" + tmpfile.Trim()));
}
reqFTP.ConnectionGroupName = "MyGroupName";
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = true;
reqFTP.Timeout = -1;
reqFTP.Credentials = new NetworkCredential(strUser.Trim(), strPass.Trim());
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
StreamReader reader = new StreamReader(ftpStream, System.Text.Encoding.UTF8);
long cl = response.ContentLength;
int bufferSize = 8192;
char[] bytesInStream = new char[8192];
int readCount;
StreamWriter sw = new StreamWriter(outputStream);
int len;
while ((len = reader.Read(bytesInStream, 0, bytesInStream.Length)) > 0)
{
sw.Write(bytesInStream, 0, len);
}
sw.Close();
outputStream.Close();
ftpStream.Close();
response.Close();
}
return "ok";
}
catch (Exception ex)
{
return (ex.Message);
}
}
我该如何改善这一点?
非常感谢:)
致以最诚挚的问候,