5

我们需要使用 vb.net 从远程 FTP 服务器获取大约 100 个非常小的文件。我们公司不会让我们购买(或安装)任何 3rd 方 ftp 库......所以我们被迫使用 FtpWebRequest 之类的东西。(或者是否有更好的免费选择,它已经是 Visual Studio 的一部分?)

这种方法有效,但速度非常慢。(我假设是因为不断登录/注销。)

使用用户名和密码登录。
从远程服务器获取文件列表。
登出
使用该文件列表单独获取每个文件:
登录,获取文件,注销。
再登录 99 次,获取每个文件,每次注销。

相反,我们可能应该这样做,但它永远不会起作用:

使用用户名和密码登录。一次。
获取文件名列表。
下载每个文件。
注销一次。

我们在网上找到了无数关于“获取 FTP 文件列表”和后来的“如何使用 FTP 下载 1 个文件”的示例……但我们从未看到“获取每个文件名,并立即下载”。

将 fwr 变暗为 Net.FtpWebRequest = Net.FtpWebRequest.Create(ftpSite)
fwr.Credentials = 新的 NetworkCredential(用户名,密码)
fwr.KeepAlive = True
fwr.Method = WebRequestMethods.Ftp.ListDirectory

   Dim sr As IO.StreamReader = 无
   尝试
      sr = 新 IO.StreamReader(fwr.GetResponse().GetResponseStream())
      直到 (sr.EndOfStream())
         文件名 = sr.ReadLine()

         fwr.Method = WebRequestMethods.Ftp.DownloadFile

         输出 = ""
         Dim sr2 As IO.StreamReader = 无
         尝试
            sr2 = 新 IO.StreamReader(fwr.GetResponse().GetResponseStream())
            输出 = sr2.ReadToEnd()

         抓住前任作为例外

         结束尝试

         If (Not sr2 Is Nothing) Then sr2.Close() : sr2 = Nothing

         调用 MsgBox("Got" & fileName & LF & output)
        环形

   抓住前任作为例外

   结束尝试

   If (Not sr Is Nothing) Then sr.Close() : sr = Nothing
   If (Not fwr Is Nothing) Then fwr = Nothing
4

2 回答 2

3

看看我的 FTP 课程,它可能正是您所需要的。

Public Class FTP
        '-------------------------[BroCode]--------------------------
        '----------------------------FTP-----------------------------
        Private _credentials As System.Net.NetworkCredential
        Sub New(ByVal _FTPUser As String, ByVal _FTPPass As String)
            setCredentials(_FTPUser, _FTPPass)
        End Sub
        Public Sub UploadFile(ByVal _FileName As String, ByVal _UploadPath As String)
            Dim _FileInfo As New System.IO.FileInfo(_FileName)
            Dim _FtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(_UploadPath)), System.Net.FtpWebRequest)
            _FtpWebRequest.Credentials = _credentials
            _FtpWebRequest.KeepAlive = False
            _FtpWebRequest.Timeout = 20000
            _FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
            _FtpWebRequest.UseBinary = True
            _FtpWebRequest.ContentLength = _FileInfo.Length
            Dim buffLength As Integer = 2048
            Dim buff(buffLength - 1) As Byte
            Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead()
            Try
                Dim _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream()
                Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength)
                Do While contentLen <> 0
                    _Stream.Write(buff, 0, contentLen)
                    contentLen = _FileStream.Read(buff, 0, buffLength)
                Loop
                _Stream.Close()
                _Stream.Dispose()
                _FileStream.Close()
                _FileStream.Dispose()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Upload Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
        Public Sub DownloadFile(ByVal _FileName As String, ByVal _ftpDownloadPath As String)
            Try
                Dim _request As System.Net.FtpWebRequest = System.Net.WebRequest.Create(_ftpDownloadPath)
                _request.KeepAlive = False
                _request.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
                _request.Credentials = _credentials
                Dim _response As System.Net.FtpWebResponse = _request.GetResponse()
                Dim responseStream As System.IO.Stream = _response.GetResponseStream()
                Dim fs As New System.IO.FileStream(_FileName, System.IO.FileMode.Create)
                responseStream.CopyTo(fs)
                responseStream.Close()
                _response.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Download Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
        Public Function GetDirectory(ByVal _ftpPath As String) As List(Of String)
            Dim ret As New List(Of String)
            Try
                Dim _request As System.Net.FtpWebRequest = System.Net.WebRequest.Create(_ftpPath)
                _request.KeepAlive = False
                _request.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails
                _request.Credentials = _credentials
                Dim _response As System.Net.FtpWebResponse = _request.GetResponse()
                Dim responseStream As System.IO.Stream = _response.GetResponseStream()
                Dim _reader As System.IO.StreamReader = New System.IO.StreamReader(responseStream)
                Dim FileData As String = _reader.ReadToEnd
                Dim Lines() As String = FileData.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
                For Each l As String In Lines
                    ret.Add(l)
                Next
                _reader.Close()
                _response.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Directory Fetch Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
            Return ret
        End Function

        Private Sub setCredentials(ByVal _FTPUser As String, ByVal _FTPPass As String)
            _credentials = New System.Net.NetworkCredential(_FTPUser, _FTPPass)
        End Sub
    End Class

初始化:

Dim ftp As New FORM.FTP("username", "password")

ftp.UploadFile("c:\file.jpeg", "ftp://domain/file.jpeg")

ftp.DownloadFile("c:\file.jpeg", "ftp://ftp://domain/file.jpeg")

Dim directory As List(Of String) = ftp.GetDirectory("ftp://ftp.domain.net/")
        ListBox1.Items.Clear()
        For Each item As String In directory
            ListBox1.Items.Add(item)
        Next
于 2015-02-23T08:43:20.190 回答
1

我刚刚放在一起的东西,重要的部分是 fwr.Proxy = Nothing,否则它会尝试自动获取代理设置,这会导致巨大的延迟,因此将其设置为 none 会迫使它不使用它。

如果您显然使用代理,则需要将其设置为实际代理。

Dim fwr As Net.FtpWebRequest = Net.FtpWebRequest.Create(ftpAddress)
fwr.Credentials = New NetworkCredential(userName, password)
fwr.KeepAlive = True
fwr.Method = WebRequestMethods.Ftp.ListDirectory
fwr.Proxy = Nothing

Try
    Dim sr As New IO.StreamReader(fwr.GetResponse().GetResponseStream())
    Dim lst = sr.ReadToEnd().Split(vbNewLine)
    For Each file As String In lst
        file = file.Trim() 'remove any whitespace
        If file = ".." OrElse file = "." Then Continue For
        Dim fwr2 As Net.FtpWebRequest = Net.FtpWebRequest.Create(ftpAddress & file)
        fwr2.Credentials = fwr.Credentials
        fwr2.KeepAlive = True
        fwr2.Method = WebRequestMethods.Ftp.DownloadFile
        fwr2.Proxy = Nothing

        Dim fileSR As New IO.StreamReader(fwr2.GetResponse().GetResponseStream())
        Dim fileData = fileSR.ReadToEnd()
        fileSR.Close()
    Next

    sr.Close()
Catch ex As Exception
End Try

我知道它有点晚了,但希望能有所帮助

于 2011-06-05T21:54:52.090 回答