从FTP列出/下载/删除文件

编程入门 行业动态 更新时间:2024-10-24 18:28:19
本文介绍了从FTP列出/下载/删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试生成一个脚本,该脚本可以处理来自FTP的多个文件. 例如,我在FTP上有4个文件,我需要将它们保存在本地,然后将其从FTP中删除. 我正在尝试列出和获取的示例,但它无法正常工作. 这是我所拥有的:

Hi, I am trying to generate a script which handles more than 1 file from FTP. For example I have 4 files on the FTP and I need to have them saved locally and after that to delete them from FTP. I was trying and example for list and get but it is not working correctly. Here is what I have:

# Public Function GetFileAndFileList(ByVal StartsWith As String, ByVal EndsWith As String) As List(Of String) # Dim oFTP As FtpWebRequest = CType(FtpWebRequest.Create("ftp://testftp.host-ed/customer"), FtpWebRequest) # oFTP.Credentials = New NetworkCredential("xxx", "yyy") # oFTP.KeepAlive = False # ''oFTP.EnableSsl = UseSSL # ''If UseSSL Then ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate) # oFTP.Method = WebRequestMethods.Ftp.ListDirectory # Dim response As FtpWebResponse = CType(oFTP.GetResponse, FtpWebResponse) # Dim sr As StreamReader = New StreamReader(response.GetResponseStream) # Dim str As String = sr.ReadLine # Dim oList As New List(Of String) # # Dim str1 As String ''declare an array of string to keep the list of file names # Dim i, n As Integer ''declare variables to loop into the array # n = 1 # While str IsNot Nothing # If str.StartsWith(StartsWith) And str.EndsWith(EndsWith) Then # oList.Add(str.Substring(InStr(str, "/"), str.Length - InStr(str, "/"))) # str = sr.ReadLine # str1 = str.Substring(InStr(str, "/"), str.Length - InStr(str, "/")) # GetFile(str1, "C:\XXX") # n = n + 1 # End If # End While # sr.Close() # response.Close() # oFTP = Nothing # MsgBox(oList.Item(0)) # Return oList # End Function

上面的函数列出了文件夹的内容,并调用下面的函数下载文件:

above function list the content of the folder and calls below function which downloads the files:

<pre lang="vb"># Public Function GetFile(ByVal Name As String, ByVal DestFile As String) As Boolean # Dim oFTP As FtpWebRequest = CType(FtpWebRequest.Create("ftp://testftp.host-ed/customer/" & Name), FtpWebRequest) # oFTP.Credentials = New NetworkCredential("xxx", "yyy") # oFTP.Method = WebRequestMethods.Ftp.DownloadFile # oFTP.KeepAlive = False # ''oFTP.EnableSsl = UseSSL # ''If UseSSL Then ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate) # oFTP.UseBinary = True # Dim response As FtpWebResponse = CType(oFTP.GetResponse, FtpWebResponse) # Dim responseStream As Stream = response.GetResponseStream # Dim fs As New FileStream(DestFile & "\" & Name, FileMode.Create) # Dim buffer(2047) As Byte # Dim read As Integer = 1 # While read <> 0 # read = responseStream.Read(buffer, 0, buffer.Length) # fs.Write(buffer, 0, read) # End While # responseStream.Close() # fs.Flush() # fs.Close() # responseStream.Close() # response.Close() # oFTP = Nothing # Return True # End Function

我有4个档案;对于其中3个来说,它工作正常,但最后一个应用程序崩溃了.

I have 4 files; it is working OK for 3 of them but the application crashes on the last one. Can someone help me, please?

推荐答案

正如曼弗雷德(Manfred)所说,您没有给予太多帮助. 但是查看您的代码 As Manfred put you haven''t given much to go on. BUT looking at your code Dim buffer(2047) As Byte

将缓冲区大小设置为2047,我将检查文件之间的大小差异. 如果即时消息不正常,请扩展错误消息的类型,发生的位置等,这将帮助我们更好地建议您 c#示例,但易于转换为vb [ ^ ]

you set the buffer size to 2047, I would check what the size differences are between the files. If im off the mark please expand on the type of error message, where its happening etc as that will help us advise you better c# example but easy to convert to vb[^]

嗨 My.Computer.Network.UploadFile("d:\reza.txt", "ftp://www.aaaaa.ir/reza.txt", "user name", " Password")

更多推荐

从FTP列出/下载/删除文件

本文发布于:2023-11-07 22:38:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567630.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   FTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!