VB.NET FTP文件上传源码

VB.NET · 2023-10-19 · 337 人浏览

水一篇文章啦,今天给别人写程序遇到的一个。感觉下面的代码挺不错的,能实现向远程服务器上传文件。

Dim ftpServer As String = "ftp://1.1.1.1:21/"
        Dim ftpUsername As String = 账号
        Dim ftpPassword As String = 密码
        Dim localFilePath As String = "1.jpg" '本地目录
        Dim remoteFilePath As String = TextBox7.Text & "合成_" & 全局变量_当前命名 & ".jpg" ' 需要上传的目录
        Dim request As FtpWebRequest = DirectCast(WebRequest.Create(ftpServer + remoteFilePath), FtpWebRequest)
        request.Method = WebRequestMethods.Ftp.UploadFile
        request.Credentials = New NetworkCredential(ftpUsername, ftpPassword)
        Using fileStream As New System.IO.FileStream(localFilePath, System.IO.FileMode.Open)
            Dim requestStream As System.IO.Stream = request.GetRequestStream()
            fileStream.CopyTo(requestStream)
            requestStream.Close()
        End Using
        Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
        response.Close()
VB.NET