System.Net.FTPClient - 如何正确传输文件 [英] System.Net.FTPClient - How to properly stream file

查看:28
本文介绍了System.Net.FTPClient - 如何正确传输文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 System.Net.FtpClient 从 FTP 服务器下载一些文本文件.我对如何处理流然后将其写入文件感到有些困惑.

I am attempting to use System.Net.FtpClient to 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.

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,但是我应该怎么做才能捕获文件/内容?我一直在输出空白文件.

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.

:由于 len = 0,似乎没有输出任何内容.不确定这意味着什么或我应该做什么...

:edit: It seems nothing is being output because len = 0. Not sure what this means or what I should do...

                        While (len = istream.Read(buffer, 0, buffer.Length)) > 0
                            fileoutput.Write(buffer, 0, len)
                        End While

推荐答案

我使用 FTPWebRequest 对象,而不是使用 ftpclient,它就像一个魅力,我更喜欢将下载分成 2mb 块.其中 targetFullUNC 是文件的 UNC 到要放置下载的位置.ftpFullURI 是 ftp 上下载文件的完整 url

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

 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

这篇关于System.Net.FTPClient - 如何正确传输文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆