【问题标题】:Download many small files with FTP quickly使用FTP快速下载许多小文件
【发布时间】:2018-09-09 14:02:03
【问题描述】:

我有一个工作代码可以从 FTP 服务器下载许多文件(数百个),但它非常慢并且经常出现超时错误。

这是我目前的下载方式:

Using ftpClient As New WebClient()
    ftpClient.Credentials = New System.Net.NetworkCredential(ftpuser, ftppassword)
    For i As Integer = 0 To directoriesDownload.Count - 1
        If directoriesDownload(i).Contains(".") Then
            If Sync_BackgroundWorker.CancellationPending = True Then
                Exit Sub
            End If
            Dim path As String = "ftp://" & ftpserver & "Datenbank/" + directoriesDownload(i).ToString()
            Dim trnsfrpth As String = config.rootpath & "ServerDownload\" + directoriesDownload(i).ToString()
            ftpClient.DownloadFile(path, trnsfrpth)
            filenametodownload = directoriesDownload(i).ToString()
            filesdownloaded += 1
            Sync_BackgroundWorker.ReportProgress(filesdownloaded)
        End If
    Next
    ftpClient.Dispose()
End Using

有没有更快的方法从 VB.NET 中的 FTP 服务器下载数百个小文件(最大 10 KB)?

如果可以选择只登录一次 FTP,而不是为每个文件登录和注销,那将是最好的。

我发现其他人有同样的问题,但没有工作结果: Using FTP to download each file *WHILE* getting the file list

我还尝试使用Parallel.For 循环进行多线程,但WebClient 不适用于多线程。如果我尝试使用ftpClient.DownloadFileAsync(New Uri(path), trnsfrpth),也是一样。

【问题讨论】:

  • 超时是由 FTP 服务器还是由您显示的代码中的某些内容生成的?您的代码所花费的时间与 WinSCP 执行相同工作所花费的时间相比如何?
  • 您是否尝试过使用Parallel.ForEach 并在每个任务中创建一个新的WebClient

标签: vb.net multithreading ftp webclient


【解决方案1】:

有没有更快的方法从 VB.NET 中的 FTP 服务器下载数百个小文件(最大 10 KB)?

...

我也尝试使用Parallel.For 循环进行多线程,但WebClient 不适用于多线程。如果我尝试使用 ftpClient.DownloadFileAsync(New Uri(path), trnsfrpth),也是一样。

多线程是要走的路。 WebClient 不支持多线程是不正确的。为什么不呢?

如果您在实现多线程 FTP 传输时遇到问题,您应该就此提出问题,而不是就其他(并且可能不存在)方式提出问题。


如果可以选择只登录一次 FTP,而不是为每个文件登录和注销,那将是最好的。

您的代码只登录 FTP 一次

参见C# - FtpWebRequest - Multiple requests over the same connection/login - 那里写的关于FtpWebRequest 的内容同样适用于WebClient,因为WebClient 在内部使用FtpWebRequest

【讨论】:

    猜你喜欢
    • 2022-08-15
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 2012-03-04
    • 2011-06-01
    相关资源
    最近更新 更多