我正在尝试使用System.Net.FtpClient
从FTP服务器下载一些文本文件。我有点困惑,我需要如何处理流,然后将其写入文件。
Using ftp = New FtpClient()
ftp.Host = "ip"
ftp.Credentials = New NetworkCredential("user", "passw")
ftp.SetWorkingDirectory("/pathToFolder/")
For Each item In ftp.GetListing(ftp.GetWorkingDirectory())
Select Case item.Type
Case FtpFileSystemObjectType.Directory
MessageBox.Show("Folder: " + item.FullName)
Case FtpFileSystemObjectType.File
MessageBox.Show("File: " + item.FullName)
Using istream As Stream = ftp.OpenRead(item.FullName)
' istream.Position is incremented accordingly to the reads you perform
' istream.Length == file size if the server supports getting the file size
' also note that file size for the same file can vary between ASCII and Binary
' modes and some servers won't even give a file size for ASCII files! It is
' recommended that you stick with Binary and worry about character encodings
' on your end of the connection.
Dim fileoutput As New FileStream("C:\Documents and Settings\jasonb\Desktop\Report1.txt", FileMode.Create, System.IO.FileAccess.Write)
Dim buffer As Byte() = New Byte(8 * 1024 - 1) {}
Dim len As Integer
While (len = istream.Read(buffer, 0, buffer.Length)) > 0
fileoutput.Write(buffer, 0, len)
End While
End Using
End Select
Next
End Using
我必须要对流做一些事情。我可以看到流是在位置0,长度是126,但我应该怎么做才能捕获文件/内容?我一直在输出空白文件。
:编辑: 似乎没有输出,因为len = 0.不确定这意味着什么或我应该做什么......
While (len = istream.Read(buffer, 0, buffer.Length)) > 0
fileoutput.Write(buffer, 0, len)
End While
答案 0 :(得分:2)
创建长度为0的文件的原因是因为它永远不会将任何信息写入输出流(您的fileoutput变量)
正如您在评论中提到的,问题与条件
一致 While (len = istream.Read(buffer, 0, buffer.Length)) > 0
这可以在C#中工作,但在VB.NET中它没有相同的行为。我认为在VB.NET这行实际上做的是不将 istream.Read(buffer,0,buffer.Length)的结果赋给变量 len 然后比较 len 大于0.相反,它比较len大于0但 len 将为0,所以条件永远不会 true
尝试以下方法:
Dim buffer As Byte() = New Byte(8 * 1024 - 1) {}
Dim len As Integer = 0
len = istream.Read(buffer, 0, buf.Length)
While len > 0
fileoutput.Write(buffer, 0, len)
len = istream.Read(buffer, 0, buffer.Length)
End While
这样它实际上会在文件输出流中写入一些内容,而输入流会读取下一个字节块。
我希望它有所帮助。
答案 1 :(得分:1)
而不是使用ftpclient我使用FTPWebRequest对象,就像一个魅力,我更喜欢以2mb块的形式下载下载。 其中targetFullUNC是要放置下载位置的文件的UNC。 ftpFullURI是ftp上下载文件的完整URL
Dim FTPRequest As FtpWebRequest = FtpWebRequest.Create("ftp://" & ftpfullURI)
With FTPRequest
.EnableSsl = False
.Credentials = New NetworkCredential(usn, pwd)
.KeepAlive = False
.UseBinary = True
.UsePassive = True
.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
End With
RaiseEvent trace(Me, "FTPDownload() logging onto ftp")
Using FTPResponse As System.Net.FtpWebResponse = CType(FTPRequest.GetResponse, System.Net.FtpWebResponse)
Using responseStream As IO.Stream = FTPResponse.GetResponseStream
Using fs As New IO.FileStream(targetfullUNC, IO.FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
RaiseEvent trace(Me, "downloading file " & targetfullUNC)
Do
read = responseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0
responseStream.Close()
fs.Flush()
fs.Close()
End Using
responseStream.Close()
End Using
FTPResponse.Close()
End Using