我需要从 FTP 服务器下载文件并对其进行一些更改,然后使用 VB.NET 再次将其上传到同一个 FTP。
请提供任何帮助。谢谢你。
一些链接:
VB.NET:http://www.codeproject.com/KB/IP/FtpClient.aspx _
c#:http ://www.c-sharpcorner.com/uploadfile/neo_matrix/simpleftp01172007082222am/simpleftp.aspx
如果您只想直接重新上传文件,只需将下载流传输到上传流:
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
如果您需要以某种方式处理内容,或者如果您需要监视进度,或两者兼而有之,则必须逐块进行(或者如果是文本文件,则可能逐行处理):
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
也可以看看: