0

我编写了一个控制台应用程序来从 FTP 下载文件,然后上传到不同的 FTP 位置。文件下载大约需要 10 秒,但上传大约需要 6 分钟。有 256 个文件,每个文件大小约为 5-30KB。所以非常小。

上传和下载的代码很相似,都是遍历目录下的所有文件然后上传。它相当简单,如下所示,它从 D:\LEV\ 文件夹迭代并上传文件到 ftp。

编辑:这是在 Azure“小型”Windows 虚拟机上运行的,所以我认为带宽不是问题?此外,我正在使用 Windows ftp.exe 上传的另一台虚拟机上执行相同的任务,它比我在同一台机器上的控制台应用程序快 2 倍。

任何线索为什么它这么慢,或者有没有办法提高速度?

static public void Upload(string file1)
{        
    string upftpServerIP = "ftp://ftp.domain.co.uk/lev/";
    string upftpUserID = "username";
    string upftpPassword = "password";

    string uri = upftpServerIP + file1;
    Uri serverUri = new Uri(uri);
    if (serverUri.Scheme != Uri.UriSchemeFtp)
    {
       return;
    }
    FtpWebRequest reqFTP;
    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(upftpServerIP + file1));
    reqFTP.Credentials = new NetworkCredential(upftpUserID, upftpPassword);
    reqFTP.KeepAlive = false;
    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
    reqFTP.UseBinary = true;
    reqFTP.Proxy = null;
    reqFTP.UsePassive = true;

    Console.WriteLine("Uploading " + file1);

    FileStream fs = File.OpenRead(@"D:\LEV\" + file1);
    byte[] buffer = new byte[fs.Length];
    fs.Read(buffer, 0, buffer.Length);
    fs.Close();
    Stream ftpstream = reqFTP.GetRequestStream();
    ftpstream.Write(buffer, 0, buffer.Length);
    ftpstream.Close();
}

static public string[] GetFileListUpload()
{
    string[] uploadFiles = Directory.GetFiles(@"D:\LEV\", "*.*", SearchOption.TopDirectoryOnly);
    return uploadFiles;
}
4

1 回答 1

5

There are several factors to consider here:

  • Your internet connection is not guaranteed to be symmetrical. Most internet connection plans (in my area, at least) offer an upload bandwidth which is 1/8th of the download bandwidth.

  • The FTP server itself may be limiting the bandwidth of incoming connections.

  • The FTP server may also be throttling the maximum bandwidth per upload. In this case, you will benefit greatly from multithreading the upload, uploading many files at once.

于 2012-12-25T14:57:05.307 回答