跪求VB.NET 连接FTPImports System.Net
Imports System.IO
Module FtpSample
Sub Main(ByVal args() As String)
If args.Length = 0 OrElse args(0).Equals("/?") Then
DisplayUsage()
ElseIf args.Length = 1 Then
Download(args(0))
ElseIf args.Length = 2 Then
If args(0).Equals("/list") Then
List(args(1))
Else
Upload(args(0), args(1))
End If
Else
Console.WriteLine("Unrecognized argument.")
End If
End Sub
Private Sub DisplayUsage()
Console.WriteLine("USAGE:")
Console.WriteLine("FtpSample [/? | FTP download URL | local file")
Console.WriteLine("FTP upload URL | /list FTP list URL]")
Console.WriteLine()
Console.WriteLine("where")
Console.WriteLine("FTP download URLURL of a file to download from an FTP server.")
Console.WriteLine("FTP upload URLLocation on a FTP server to upload a file to.")
Console.WriteLine("FTP list URLLocation on a FTP server to list the contents of.")
Console.WriteLine("local fileA local file to upload to an FTP server.")
Console.WriteLine()
Console.WriteLine("Options:")
Console.WriteLine("/?Display this help message.")
Console.WriteLine("/listSpecifies the list command.")
Console.WriteLine()
Console.WriteLine("EXAMPLES:")
Console.WriteLine("Download a fileFtpSample ")
Console.WriteLine("Upload a fileFtpSample upload.txt ")
End Sub
Private Sub Download(ByVal downloadUrl As String)
Dim responseStream As Stream = Nothing
Dim fileStream As FileStream = Nothing
Dim reader As StreamReader = Nothing
Try
Dim downloadRequest As FtpWebRequest = _
WebRequest.Create(downloadUrl)
Dim downloadResponse As FtpWebResponse = _
downloadRequest.GetResponse()
responseStream = downloadResponse.GetResponseStream()
Dim fileName As String = _
Path.GetFileName(downloadRequest.RequestUri.AbsolutePath)
If fileName.Length = 0 Then
reader = New StreamReader(responseStream)
Console.WriteLine(reader.ReadToEnd())
Else
fileStream = File.Create(fileName)
Dim buffer(1024) As Byte
Dim bytesRead As Integer
While True
bytesRead = responseStream.Read(buffer, 0, buffer.Length)
If bytesRead = 0 Then
Exit While
End If
fileStream.Write(buffer, 0, bytesRead)
End While
End If
Console.WriteLine("Download complete.")
Catch ex As UriFormatException
Console.WriteLine(ex.Message)
Catch ex As WebException
Console.WriteLine(ex.Message)
Catch ex As IOException
Console.WriteLine(ex.Message)
Finally
If reader IsNot Nothing Then
reader.Close()
ElseIf responseStream IsNot Nothing Then
responseStream.Close()
End If
If fileStream IsNot Nothing Then
fileStream.Close()
End If
End Try
End Sub
Private Sub Upload(ByVal fileName As String, ByVal uploadUrl As String)
Dim requestStream As Stream = Nothing
Dim fileStream As FileStream = Nothing
Dim uploadResponse As FtpWebResponse = Nothing
Try
Dim uploadRequest As FtpWebRequest = WebRequest.Create(uploadUrl)
uploadRequest.Method = WebRequestMethods.
' UploadFile is not supported through an Http proxy
' so we disable the proxy for this request.
uploadRequest.Proxy = Nothing
requestStream = uploadRequest.GetRequestStream()
fileStream = File.Open(fileName, FileMode.Open)
Dim buffer(1024) As Byte
Dim bytesRead As Integer
While True
bytesRead = fileStream.Read(buffer, 0, buffer.Length)
If bytesRead = 0 Then
Exit While
End If
requestStream.Write(buffer, 0, bytesRead)
End While
' The request stream must be closed before getting the response.
requestStream.Close()
uploadResponse = uploadRequest.GetResponse()
Console.WriteLine("Upload complete.")
推荐阅读
- dell和华为服务器比较,dell和华为服务器比较哪个好
- java代码自增序列,java自动生成递增编号
- 开淘宝店如何做推广,开淘宝网店怎么推广
- 宝宝爬抱枕直播软件,我要看小孩抱枕
- vb.net绘图异或笔 vb 异或 运算符
- 职工关注公众号通知,关注公众号的提示语
- 微信的视频号如何看访客量,微信视频号怎样看浏览量
- tiktok直播伴侣无人直播,tiktok无人直播工具obs
- java代码写看看呗 java怎么看代码