Imports System.IO Imports System.Threading Imports FluentFTP Namespace UTSModule Public Class CUtsFtp ''' 测试器句柄,全局唯一 Private Shared _object As CUtsFtp ''' 初始化测试器线程锁 Private Shared ReadOnly InitLock As New Object() Private Shared FtpPort As Integer Private Shared FtpUser As String Private Shared FtpPwd As String Private _ftpUser As String Private _ftpPwd As String Private _ftpPort As Integer Private _ftpHost As String Private Default_FTP_Host As String = "blv-oa.com" ''' ''' 初始化FTP连接参数 ''' ''' 端口号 ''' 用户名 ''' 用户密码 Public Shared Sub InitConnectParams(port As Integer, user As String, pwd As String) FtpPort = port FtpUser = user FtpPwd = pwd End Sub ''' ''' 创建类单例对象 ''' ''' Public Shared Function CreateObject() As CUtsFtp If _object Is Nothing Then SyncLock InitLock Thread.MemoryBarrier() If _object Is Nothing Then _object = New CUtsFtp("blv-oa.com", FtpPort, FtpUser, FtpPwd) End If End SyncLock End If Return _object End Function Public Function DownloadDir(loaddir As String, ftpDir As String) As Boolean Dim result As FtpListItem() Using ftpClient As FtpClient = New FtpClient(_ftpHost, _ftpPort, _ftpUser, _ftpPwd) AddHandler ftpClient.ValidateCertificate, AddressOf OnValidateCertificate ftpClient.EncryptionMode = FtpEncryptionMode.Auto ftpClient.AutoConnect() Try DownloadFtpDirectory(ftpClient, ftpDir, loaddir) Catch ex As Exception ftpClient.Disconnect() Return False End Try ftpClient.Disconnect() End Using Return True End Function Private Shared Sub DownloadFtpDirectory(ByVal ftpClient As FtpClient, ByVal remotePath As String, ByVal localPath As String) For Each item In ftpClient.GetListing(remotePath) Dim localItemPath As String = Path.Combine(localPath, item.Name) Dim remoteimtePath As String = Path.Combine(remotePath, item.Name) If item.Type = FtpFileSystemObjectType.File Then Dim state As FtpStatus = ftpClient.DownloadFile(localItemPath, remoteimtePath, FtpLocalExists.Overwrite) If Not state = FtpStatus.Success Then MsgBox($"download failed!!{vbCrLf }{localItemPath}") End If ElseIf item.Type = FtpFileSystemObjectType.Directory Then If Not Directory.Exists(localItemPath) Then Directory.CreateDirectory(localItemPath) End If DownloadFtpDirectory(ftpClient, remoteimtePath, localItemPath) End If Next End Sub Private Sub New(host As String, port As Integer, user As String, pwd As String) _ftpHost = host _ftpPort = port _ftpUser = user _ftpPwd = pwd End Sub ''' ''' Ftp服务器地址 ''' ''' Public Property FtpHost As String Get Return _ftpHost End Get Set(value As String) _ftpHost = value End Set End Property Private Sub OnValidateCertificate(control As FtpClient, e As FtpSslValidationEventArgs) e.Accept = True End Sub ''' ''' 判断FTP文件是否存在 ''' ''' ''' Public Function FtpFileExists(path As String) As Boolean Dim result As Boolean Using ftpClient As FtpClient = New FtpClient(_ftpHost, _ftpPort, _ftpUser, _ftpPwd) AddHandler ftpClient.ValidateCertificate, AddressOf OnValidateCertificate ftpClient.EncryptionMode = FtpEncryptionMode.Auto ftpClient.AutoConnect() result = ftpClient.FileExists(path) ftpClient.Disconnect() End Using Return result End Function ''' ''' 创建Ftp文件夹 ''' ''' Ftp文件夹路径 ''' 创建所有不存在的文件夹路径 Public Sub CreateDir(remoteDir As String, Optional force As Boolean = False) Using ftpClient As FtpClient = New FtpClient(FtpHost, _ftpPort, _ftpUser, _ftpPwd) AddHandler ftpClient.ValidateCertificate, AddressOf OnValidateCertificate ftpClient.EncryptionMode = FtpEncryptionMode.Auto ftpClient.AutoConnect() ftpClient.CreateDirectory(remoteDir, force) ftpClient.Disconnect() End Using End Sub ''' ''' 上传本地文件至Ftp ''' 将本地指定路径压缩包上传到FTP服务器上manager文件夹下 ''' Public Sub FtpUpload(remotePath As String, loadPath As String) Using ftpClient As FtpClient = New FtpClient(_ftpHost, _ftpPort, _ftpUser, _ftpPwd) AddHandler ftpClient.ValidateCertificate, AddressOf OnValidateCertificate ftpClient.EncryptionMode = FtpEncryptionMode.Auto ftpClient.AutoConnect() ftpClient.UploadFile(loadPath, remotePath, FtpRemoteExists.Overwrite, True) ftpClient.Disconnect() End Using End Sub ''' ''' 从Ftp下载文件至本地 ''' 从FTP下载压缩包,到本地指定路径 ''' Public Overloads Function FtpDownload(remotePath As String, loadPath As String, Optional existMode As FtpLocalExists = FtpLocalExists.Overwrite) As FtpStatus Dim state As FtpStatus Using ftpClient As FtpClient = New FtpClient(_ftpHost, _ftpPort, _ftpUser, _ftpPwd) AddHandler ftpClient.ValidateCertificate, AddressOf OnValidateCertificate ftpClient.EncryptionMode = FtpEncryptionMode.None ftpClient.ConnectAsync() state = ftpClient.DownloadFile(loadPath, remotePath, existMode) ftpClient.Disconnect() End Using Return state End Function Public Overloads Sub FtpDownload(FtpDownloadfile As Dictionary(Of String, String), Optional existMode As FtpLocalExists = FtpLocalExists.Overwrite) Using ftpClient As FtpClient = New FtpClient(_ftpHost, _ftpPort, _ftpUser, _ftpPwd) AddHandler ftpClient.ValidateCertificate, AddressOf OnValidateCertificate ftpClient.EncryptionMode = FtpEncryptionMode.Auto ftpClient.AutoConnect() For Each loadfile In FtpDownloadfile ftpClient.DownloadFile(loadfile.Value, loadfile.Key, existMode) Next ftpClient.Disconnect() End Using End Sub End Class End Namespace