vb.net System.Net.FTPClient - 如何正确传输文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22405825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.Net.FTPClient - How to properly stream file
提问by Jason Bayldon
I am attempting to use System.Net.FtpClientto download some text files from an FTP server. I am a bit confused as to how I need to handle the stream and then write it to a file.
我试图用来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
There must be something I need to do with the stream. I can see that the stream is at position 0 and the length is something like 126, but what should I be doing to capture the file/contents? I keep getting blank files output.
我必须对流做些什么。我可以看到流在位置 0 并且长度类似于 126,但是我应该怎么做才能捕获文件/内容?我一直在输出空白文件。
:edit: It seems nothing is being output because len = 0. Not sure what this means or what I should do...
:edit: 由于 len = 0,似乎没有输出任何内容。不确定这意味着什么或我应该做什么......
While (len = istream.Read(buffer, 0, buffer.Length)) > 0
fileoutput.Write(buffer, 0, len)
End While
回答by diegosasw
The reason why it is creating files with length 0 is because it is never writing any information into the output stream (your fileoutput variable)
它创建长度为 0 的文件的原因是因为它从不将任何信息写入输出流(您的 fileoutput 变量)
As you mentioned in your comments the problem is in the line with the condition
正如您在评论中提到的,问题符合条件
While (len = istream.Read(buffer, 0, buffer.Length)) > 0
That could work in C#, but in VB.NET it does not have the same behavior. I think in VB.NET this line what is actually doing is not assigning the result of istream.Read(buffer, 0, buffer.Length)to the variable lenand then comparing that lenis bigger than 0. Instead it is comparing that len is bigger than 0 but lenis gonna be 0, so the condition is never true
这可以在 C# 中工作,但在 VB.NET 中它没有相同的行为。我认为在 VB.NET 中,这一行实际上并没有将istream.Read(buffer, 0, buffer.Length)的结果分配给变量len然后比较len是否大于 0。而是比较那个 len大于 0 但len将是 0,所以条件永远不成立
Try the following:
请尝试以下操作:
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
That way it will actually write something in the file output stream while the input stream reads the next block of bytes.
这样,当输入流读取下一个字节块时,它实际上会在文件输出流中写入一些内容。
I hope it helps.
我希望它有帮助。
回答by Andre Pageot
Rather than use the ftpclient i use the FTPWebRequest object, works like a charm, and i prefer to chunk the download in 2mb chunks. Where targetFullUNC is the UNC of the file to the location you want to put the download. ftpFullURI is the full url to the download file on the ftp
而不是使用 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

