2010-03-15 48 views
1

我需要从FTP服务器上下载文件,并对其进行一些更改并使用VB.NET将其上传到同一FTP。如何从FTP下载文件并再次上传

请任何帮助。谢谢。

+0

我一直在寻找类似的解决方案并通过惊人的发现了很好的代码。检查下面的链接:http://stackoverflow.com/questions/5938893/using-ftp-to-download-each-file-while-getting-the-file-list – GiorgiTBS 2016-12-12 20:00:19

回答

1
+0

这不是免费的dll。文件。我需要示例访问直接Microsoft库 – 2010-03-15 20:06:27

+0

该代码是公共领域,您应该能够从您的VB.NET应用程序访问类。你还需要什么? – 2010-03-15 20:17:53

+0

我尝试从codeproject.com FTpclient.cs类,但是当我尝试从我的FTP下载文件它给了我错误文件不可用或没有访问,并且我认为它没有访问错误,我如何可以从ftp访问下载文件。 谢谢 – 2010-03-16 16:09:11

0

如果你想干脆直接重新上传文件,只需管下载流上载流:

Dim downloadRequest As FtpWebRequest = 
    WebRequest.Create("ftp://downloadftp.example.com/source/path/file.txt") 
downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile 
downloadRequest.Credentials = New NetworkCredential("username1", "password1") 

Dim uploadRequest As FtpWebRequest = 
    WebRequest.Create("ftp://uploadftp.example.com/target/path/file.txt") 
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile 
uploadRequest.Credentials = New NetworkCredential("username2", "password2") 

Using downloadResponse As FtpWebResponse = downloadRequest.GetResponse(), 
     sourceStream As Stream = downloadResponse.GetResponseStream(), 
     targetStream As Stream = uploadRequest.GetRequestStream() 
    sourceStream.CopyTo(targetStream) 
End Using 

如果你需要处理的内容s ^不知何故,或者如果您需要监测的进展,或两者兼而有之,你必须做它由一块块(由线或可能行,如果它是一个文本文件,你正在处理):

Dim downloadRequest As FtpWebRequest = 
    WebRequest.Create("ftp://downloadftp.example.com/source/path/file.txt") 
downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile 
downloadRequest.Credentials = New NetworkCredential("username1", "password1") 

Dim uploadRequest As FtpWebRequest = 
    WebRequest.Create("ftp://uploadftp.example.com/target/path/file.txt") 
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile 
uploadRequest.Credentials = New NetworkCredential("username2", "password2") 

Using downloadResponse As FtpWebResponse = downloadRequest.GetResponse(), 
     sourceStream As Stream = downloadResponse.GetResponseStream(), 
     targetStream As Stream = uploadRequest.GetRequestStream() 
    Dim buffer As Byte() = New Byte(10240 - 1) {} 
    Dim read As Integer 
    Do 
     read = sourceStream.Read(buffer, 0, buffer.Length) 
     If read > 0 Then 
      ' process "buffer" here 
      targetStream.Write(buffer, 0, read) 
     End If 
    Loop While read > 0 
End Using 

参见: