Imports System.Threading Imports FluentFTP Namespace UTSModule Public Class UtsFtp ''' 测试器句柄,全局唯一 Private Shared _object As UtsFtp ''' 初始化测试器线程锁 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 UtsFtp If _object Is Nothing Then SyncLock InitLock '内存护栏 Thread.MemoryBarrier() If _object Is Nothing Then _object = New UtsFtp("blv-oa.com", FtpPort, FtpUser, FtpPwd) End If End SyncLock End If Return _object End Function 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 Function FtpUpload2(remotePath As String, loadPath As String) As FtpStatus Dim ftpstart As FtpStatus Using ftpClient As FtpClient = New FtpClient(_ftpHost, _ftpPort, _ftpUser, _ftpPwd) AddHandler ftpClient.ValidateCertificate, AddressOf OnValidateCertificate ftpClient.EncryptionMode = FtpEncryptionMode.Auto ftpClient.AutoConnect() ftpstart = ftpClient.UploadFile(loadPath, remotePath, FtpRemoteExists.Overwrite, True) ftpClient.Disconnect() End Using Return ftpstart End Function Public Function FtpUpload(remotePath As String, loadPath As String) As FtpStatus Dim ftpstart As FtpStatus Try Dim datS As String = remotePath.Replace(".xml", ".dat") Dim datl As String = loadPath.Replace(".xml", ".dat") Dim ndatS As String = remotePath.Replace(".xml", ".nxml") Dim ndatS1 As String = remotePath.Replace(".xml", ".ndat") Using ftpClient As FtpClient = New FtpClient(_ftpHost, _ftpPort, _ftpUser, _ftpPwd) AddHandler ftpClient.ValidateCertificate, AddressOf OnValidateCertificate ftpClient.EncryptionMode = FtpEncryptionMode.Auto ftpClient.AutoConnect() ftpstart = ftpClient.UploadFile(loadPath, ndatS, FtpRemoteExists.Overwrite, True) ftpClient.Rename(ndatS, remotePath) ftpstart = ftpClient.UploadFile(datl, ndatS1, FtpRemoteExists.Overwrite, True) ftpClient.Rename(ndatS1, datS) ftpClient.Disconnect() End Using Catch ex As Exception MsgBox($"发布错误:{ex.Message }") ftpstart = FtpStatus.Failed End Try Return ftpstart End Function Public Function FtpUploadBlv(remotePath As String, loadPath As String) As FtpStatus Dim ftpstart As FtpStatus Dim datS As String = remotePath.Replace(".blv", ".dat") Dim datl As String = loadPath.Replace(".blv", ".dat") Using ftpClient As FtpClient = New FtpClient(_ftpHost, _ftpPort, _ftpUser, _ftpPwd) AddHandler ftpClient.ValidateCertificate, AddressOf OnValidateCertificate ftpClient.EncryptionMode = FtpEncryptionMode.Auto ftpClient.AutoConnect() ftpstart = ftpClient.UploadFile(loadPath, remotePath, FtpRemoteExists.Overwrite, True) ftpstart = ftpClient.UploadFile(datl, datS, FtpRemoteExists.Overwrite, True) ftpClient.Disconnect() End Using Return ftpstart End Function ''' ''' 从Ftp下载文件至本地 ''' 从FTP下载压缩包,到本地指定路径 ''' Public Overloads Function FtpDownload(remotePath As String, loadPath As String, Optional existMode As FtpLocalExists = FtpLocalExists.Overwrite) As FtpStatus Dim ftpstart As FtpStatus Using ftpClient As FtpClient = New FtpClient(_ftpHost, _ftpPort, _ftpUser, _ftpPwd) AddHandler ftpClient.ValidateCertificate, AddressOf OnValidateCertificate ftpClient.EncryptionMode = FtpEncryptionMode.Auto ftpClient.AutoConnect() ftpstart = ftpClient.DownloadFile(loadPath, remotePath, existMode) ftpClient.Disconnect() End Using Return ftpstart End Function Public Overloads Function FtpDownload(FtpDownloadfile As Dictionary(Of String, String), Optional flag As Boolean = False, Optional existMode As FtpLocalExists = FtpLocalExists.Overwrite) As FtpStatus Dim ftpstart As FtpStatus = FtpStatus.Success 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 ftpstart = ftpClient.DownloadFile(loadfile.Value, loadfile.Key, existMode) If flag Then ftpstart = ftpClient.DownloadFile(loadfile.Value.Replace(".xml", ".dat"), loadfile.Key.Replace(".xml", ".dat"), existMode) End If If ftpstart = 1 Then Continue For Else Exit For End If Next ftpClient.Disconnect() End Using Return ftpstart End Function End Class End Namespace