通过FTP快速下载许多小文件

时间:2018-09-09 14:02:03

标签: vb.net multithreading ftp webclient

我有一个工作代码可以从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),也是一样。

1 个答案:

答案 0 :(得分:0)

  

在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