vb.net 使用ftp协议上传、下载文件(Visual Studio 2019)

ftp传输是一种文件传输协议,在很多时候,可以提供数据传输功能。

软件工具:visual studio 2019
win10系统

打开visual studio 2019,新建一个窗体项目。
vb.net 使用ftp协议上传、下载文件(Visual Studio 2019)_第1张图片
从ftp服务器下载文件:

Dim req As FtpWebRequest = CType(WebRequest.Create("ftp://192.168.1.102/" + TextBox1.Text + ".txt"), FtpWebRequest)

        req.Method = WebRequestMethods.Ftp.DownloadFile

        req.Credentials = New NetworkCredential("ftp服务器所在用户名", "用户密码")

        Dim res As FtpWebResponse = CType(req.GetResponse(), FtpWebResponse)
        Dim resstr As Stream = res.GetResponseStream()
        Dim rd As StreamReader = New StreamReader(resstr)

        'Console.WriteLine(rd.ReadToEnd())
        'Console.WriteLine($"download finish,status{res.StatusDescription }")


        Dim myStream As Stream
        Dim saveFileDialog1 As New SaveFileDialog()
        Dim byt As Byte()
        byt = Encoding.UTF8.GetBytes(rd.ReadToEnd())

        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.DefaultExt = ".txt"
        saveFileDialog1.RestoreDirectory = True

        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            myStream = saveFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then
                myStream.Write(byt, 0, byt.Length)                      '将下载的数据流的数据写入到文件并保存到本地
                myStream.Close()
            End If
        End If


        rd.Close()
        res.Close()

上面的程序包含了下载文件及存储到本地文件,使用了vb.net的savefiledialog控件。
下载的文件名可以自己定义,在textbox1中体现。前提是知道ftp服务器中有哪些文件,可以使用目录读取功能:

Dim req As FtpWebRequest = CType(WebRequest.Create("ftp://192.168.1.102/"), FtpWebRequest)
        req.Method = WebRequestMethods.Ftp.ListDirectoryDetails

        req.Credentials = New NetworkCredential("ftp服务器用户名", "ftp服务器密码")

        Dim res As FtpWebResponse = CType(req.GetResponse(), FtpWebResponse)
        Dim resstr As Stream = res.GetResponseStream()
        Dim rd As StreamReader = New StreamReader(resstr)

        Dim rtext As String

        Dim rts As String()



        rtext = rd.ReadToEnd()

        rts = rtext.Split({"     "}, StringSplitOptions.None)



        ' Dim rts1(rts.Length) As String

        ' Dim rts2(rts.Length) As Integer



        rtb2.Clear()

        rtb2.AppendText(rtext + vbCrLf)

将读取的目录显示在超级文本richtext1中。
也可以从本地文件夹中将文档上传到ftp服务器中。

 Dim req As FtpWebRequest = CType(WebRequest.Create(uri), FtpWebRequest)
        req.Method = WebRequestMethods.Ftp.UploadFile
        req.Credentials = New NetworkCredential("ftp服务器用户名", "ftp服务器用户密码")

        Dim filec As Byte()
        Using sources As StreamReader = New StreamReader(fp)
            filec = Encoding.UTF8.GetBytes(sources.ReadToEnd())
        End Using

        req.ContentLength = filec.Length

        Using ress As Stream = req.GetRequestStream()
            ress.Write(filec, 0, filec.Length)
        End Using

        Using res As FtpWebResponse = CType(req.GetResponse(), FtpWebResponse)
            Console.WriteLine($"upload file finish,status{res.StatusDescription }")

        End Using

当然,在上传文档时,可以先选择文档,可以使用vb.net的openfiledialog控件,


        Dim fps As String()
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            fp = OpenFileDialog1.FileName
        End If

        fps = fp.Split({"\"}, StringSplitOptions.None)

        uri = "ftp://192.168.1.102/" + fps(4)

        Label2.Text = uri

完整程序:

Imports System.Net

Imports System.Text
Imports System.IO





Public Class Form1





    Dim uri As String

    Dim fp As String


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub


    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

        Dim req As FtpWebRequest = CType(WebRequest.Create("ftp://192.168.1.102/" + TextBox1.Text + ".txt"), FtpWebRequest)

        req.Method = WebRequestMethods.Ftp.DownloadFile

        req.Credentials = New NetworkCredential("rongjv", "helanyinxue1990")

        Dim res As FtpWebResponse = CType(req.GetResponse(), FtpWebResponse)
        Dim resstr As Stream = res.GetResponseStream()
        Dim rd As StreamReader = New StreamReader(resstr)

        'Console.WriteLine(rd.ReadToEnd())
        'Console.WriteLine($"download finish,status{res.StatusDescription }")


        Dim myStream As Stream
        Dim saveFileDialog1 As New SaveFileDialog()
        Dim byt As Byte()
        byt = Encoding.UTF8.GetBytes(rd.ReadToEnd())

        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.DefaultExt = ".txt"
        saveFileDialog1.RestoreDirectory = True

        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            myStream = saveFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then
                myStream.Write(byt, 0, byt.Length)                      '将下载的数据流的数据写入到文件并保存到本地
                myStream.Close()
            End If
        End If


        rd.Close()
        res.Close()


    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click


        Dim req As FtpWebRequest = CType(WebRequest.Create(uri), FtpWebRequest)
        req.Method = WebRequestMethods.Ftp.UploadFile
        req.Credentials = New NetworkCredential("rongjv", "helanyinxue1990")

        Dim filec As Byte()
        Using sources As StreamReader = New StreamReader(fp)
            filec = Encoding.UTF8.GetBytes(sources.ReadToEnd())
        End Using

        req.ContentLength = filec.Length

        Using ress As Stream = req.GetRequestStream()
            ress.Write(filec, 0, filec.Length)
        End Using

        Using res As FtpWebResponse = CType(req.GetResponse(), FtpWebResponse)
            Console.WriteLine($"upload file finish,status{res.StatusDescription }")

        End Using

    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click

        Dim fps As String()
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            fp = OpenFileDialog1.FileName
        End If

        fps = fp.Split({"\"}, StringSplitOptions.None)

        uri = "ftp://192.168.1.102/" + fps(4)

        Label2.Text = uri
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click

        Dim req As FtpWebRequest = CType(WebRequest.Create("ftp://192.168.1.102/"), FtpWebRequest)
        req.Method = WebRequestMethods.Ftp.ListDirectoryDetails

        req.Credentials = New NetworkCredential("rongjv", "helanyinxue1990")

        Dim res As FtpWebResponse = CType(req.GetResponse(), FtpWebResponse)
        Dim resstr As Stream = res.GetResponseStream()
        Dim rd As StreamReader = New StreamReader(resstr)

        Dim rtext As String

        Dim rts As String()



        rtext = rd.ReadToEnd()

        rts = rtext.Split({"     "}, StringSplitOptions.None)



        ' Dim rts1(rts.Length) As String

        ' Dim rts2(rts.Length) As Integer



        rtb2.Clear()

        rtb2.AppendText(rtext + vbCrLf)


    End Sub
End Class

以上程序是在微软官方示例的基础上,加了些小的改变实现。

实例演示:

你可能感兴趣的:(工业自动化编程,VB.net实例应用,vb.net)