208 lines
8.2 KiB
VB.net
208 lines
8.2 KiB
VB.net
Imports System.Threading
|
||
Imports FluentFTP
|
||
|
||
Namespace UTSModule
|
||
Public Class UtsFtp
|
||
''' <summary>测试器句柄,全局唯一</summary>
|
||
Private Shared _object As UtsFtp
|
||
|
||
''' <summary>初始化测试器线程锁</summary>
|
||
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"
|
||
|
||
''' <summary>
|
||
''' 初始化FTP连接参数
|
||
''' </summary>
|
||
''' <param name="port">端口号</param>
|
||
''' <param name="user">用户名</param>
|
||
''' <param name="pwd">用户密码</param>
|
||
Public Shared Sub InitConnectParams(port As Integer, user As String, pwd As String)
|
||
FtpPort = port
|
||
FtpUser = user
|
||
FtpPwd = pwd
|
||
End Sub
|
||
|
||
''' <summary>
|
||
''' 创建类单例对象
|
||
''' </summary>
|
||
''' <returns></returns>
|
||
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
|
||
|
||
|
||
''' <summary>
|
||
''' Ftp服务器地址
|
||
''' </summary>
|
||
''' <returns></returns>
|
||
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
|
||
|
||
''' <summary>
|
||
''' 判断FTP文件是否存在
|
||
''' </summary>
|
||
''' <param name="path"></param>
|
||
''' <returns></returns>
|
||
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
|
||
|
||
''' <summary>
|
||
''' 创建Ftp文件夹
|
||
''' </summary>
|
||
''' <param name="remoteDir">Ftp文件夹路径</param>
|
||
''' <param name="force">创建所有不存在的文件夹路径</param>
|
||
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
|
||
|
||
|
||
''' <summary>
|
||
''' 上传本地文件至Ftp
|
||
''' 将本地指定路径压缩包上传到FTP服务器上manager文件夹下
|
||
''' </summary>
|
||
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
|
||
Dim datS As String = remotePath.Replace(".xml", ".dat")
|
||
Dim datl As String = loadPath.Replace(".xml", ".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
|
||
|
||
''' <summary>
|
||
''' 从Ftp下载文件至本地
|
||
''' 从FTP下载压缩包,到本地指定路径
|
||
''' </summary>
|
||
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
|
||
|
||
|
||
|
||
''' <summary>
|
||
''' 从获取FTP指定目录下的所有文件名
|
||
''' 从FTP下载压缩包,到本地指定路径
|
||
''' </summary>
|
||
Public Overloads Function GeTFtpDirFilelist(remotePath As String, loadPath As String, Optional existMode As FtpLocalExists = FtpLocalExists.Overwrite) As FtpListItem()
|
||
Dim ftpstart As FtpListItem()
|
||
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)
|
||
|
||
ftpstart = ftpClient.GetListing()
|
||
ftpClient.Disconnect()
|
||
End Using
|
||
Return ftpstart
|
||
End Function
|
||
|
||
End Class
|
||
End Namespace
|
||
|