这是我将文件上传到FTP服务器的代码:
Dim _FileStream As System.IO.FileStream
Dim _Stream As System.IO.Stream
Dim _FileName As String = LocalPath + "/" + Name
Dim _FileInfo As New System.IO.FileInfo(_FileName)
Dim _FtpWebRequest As System.Net.FtpWebRequest
' Create FtpWebRequest object from the Uri provided
_FtpWebRequest = CType(WebRequest.Create("ftp://" + hostLocal + ":" + portLocal.ToString + "/" + Name), FtpWebRequest)
' Provide the WebPermission Credintials
_FtpWebRequest.Credentials = New System.Net.NetworkCredential(userNameLocal, passwordLocal)
' By default KeepAlive is true, where the control connection is not closed
' after a command is executed.
_FtpWebRequest.KeepAlive = False
' set timeout for 20 seconds
_FtpWebRequest.Timeout = 20000
' Specify the command to be executed.
_FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
' Specify the data transfer type.
_FtpWebRequest.UseBinary = True
' Notify the server about the size of the uploaded file
_FtpWebRequest.ContentLength = _FileInfo.Length
' The buffer size is set to 32kb
Dim buffLength As Integer = 32768
Dim buff(buffLength - 1) As Byte
我使用32Kb缓冲区大小,因为如果我尝试使用2Kb,它的工作速度太慢
' Opens a file stream (System.IO.FileStream) to read the file to be uploaded
_FileStream = _FileInfo.OpenRead()
' Stream to which the file to be upload is written
_Stream = _FtpWebRequest.GetRequestStream()
' Read from the file stream 32kb at a time
Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength)
Do While contentLen <> 0
' Write Content from the file stream to the FTP Upload Stream
_Stream.Write(buff, 0, contentLen)
contentLen = _FileStream.Read(buff, 0, buffLength)
Loop
' Close the file stream and the Request Stream
_FileStream.Close()
_FileStream.Dispose()
_Stream.Close()
_Stream.Dispose()
我的这个功能有问题。如果我尝试将zip文件上传到FTP服务器,则会收到损坏的文件。 请帮我。我使用visual studio 2008和.net framework 2.0