-1

我是一个没有经验的 VB 程序员。

我正在创建一个桌面应用程序,用户通过 ftp 将一些文件上传到服务器。

这些文件都在本地的同一个文件夹中,并上传到服务器的同一个文件夹中。

此刻我正在使用此代码:

For Each File As String In My.Computer.FileSystem.GetFiles( _
                                    path, _
                                    FileIO.SearchOption.SearchAllSubDirectories, _
                                    "*.jpg")
    Dim fileInf As IO.FileInfo = New IO.FileInfo(File)
    Dim uri As String = serveruri
    Dim reqFTP As Net.FtpWebRequest
    reqFTP = Net.FtpWebRequest.Create(New Uri(uri))   
    reqFTP.Credentials = New Net.NetworkCredential(user, pass)
    reqFTP.KeepAlive = False
    reqFTP.Method = Net.WebRequestMethods.Ftp.UploadFile    
    reqFTP.UseBinary = True
    reqFTP.ContentLength = fileInf.Length
    Dim buffLength As Integer = 2048
    Dim buff(buffLength) As Byte
    Dim contentLen As Integer  
    Dim fs As IO.FileStream = fileInf.OpenRead()
    Dim strm As IO.Stream = reqFTP.GetRequestStream()
    contentLen = fs.Read(buff, 0, buffLength)
    While Not (contentLen = 0) 
        strm.Write(buff, 0, contentLen)
        contentLen = fs.Read(buff, 0, buffLength)
    End While 
    strm.Close()
    fs.Close()        
Next

使用此代码,我将文件一一上传,但总花费的时间太大。

我怎样才能减少总花费时间?

我曾想过用线程来做这件事或异步上传文件,但我找不到任何关于如何做到这一点的例子。

谢谢!

4

2 回答 2

0

我们使用此代码异步执行类似的工作:

  w = New System.Net.WebClient

        w.Credentials = New Net.NetworkCredential(username, Password)

        w.UploadFileAsync(New System.Uri("ftp://ftp.yourdomain/yourfilename"), bupath)
于 2013-10-09T15:35:03.257 回答
0

使用我的 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
于 2015-02-23T08:27:16.360 回答