初始化
This commit is contained in:
161
BLV_Studio/Control/FTP/UtsFtp.vb
Normal file
161
BLV_Studio/Control/FTP/UtsFtp.vb
Normal file
@@ -0,0 +1,161 @@
|
||||
Imports System.Threading
|
||||
Imports FluentFTP
|
||||
|
||||
Namespace UTSModule
|
||||
Public Class CUtsFtp
|
||||
''' <summary>测试器句柄,全局唯一</summary>
|
||||
Private Shared _object As CUtsFtp
|
||||
|
||||
''' <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 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
|
||||
|
||||
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 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
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 从Ftp下载文件至本地
|
||||
''' 从FTP下载压缩包,到本地指定路径
|
||||
''' </summary>
|
||||
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
|
||||
|
||||
352
BLV_Studio/Control/FTP/blvFtpServer.vb
Normal file
352
BLV_Studio/Control/FTP/blvFtpServer.vb
Normal file
@@ -0,0 +1,352 @@
|
||||
|
||||
Imports BLV_Studio.UTSModule
|
||||
Imports FluentFTP
|
||||
Imports MD5Hash
|
||||
Imports System.IO
|
||||
Imports System.Security.Cryptography
|
||||
Imports System.Threading
|
||||
Public Class CblvFtpServer
|
||||
|
||||
|
||||
'Public password As String = MD5Hash.Hash.Content("123")
|
||||
''' <summary>
|
||||
''' 文件同步线程
|
||||
''' </summary>
|
||||
Public _syn_thread As Thread
|
||||
''' <summary>
|
||||
''' 本地同步文件夹路径
|
||||
''' </summary>
|
||||
Public _SyncLoadDirPath As String
|
||||
|
||||
''' <summary>
|
||||
''' 数据库登录
|
||||
''' </summary>
|
||||
Public _dbLoginStr As String
|
||||
|
||||
''' <summary>
|
||||
''' 数据库同步文件集 所在文件夹路径 *文件名
|
||||
''' </summary>
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 需要下载的文件集 相对路径文件名
|
||||
''' </summary>
|
||||
|
||||
''' <summary>
|
||||
''' FTP同步标志
|
||||
''' </summary>
|
||||
Private _startFlag As Integer = 0
|
||||
''' <summary>
|
||||
''' FTP同步间隔
|
||||
''' </summary>
|
||||
Private num As Integer = 30
|
||||
''' <summary>
|
||||
'''
|
||||
''' </summary>
|
||||
''' <param name="loadpath">同步文件夹本地路径</param>
|
||||
''' <param name="serverpath">同步云文件夹路径</param>
|
||||
Sub New(loadpath As String, dbLoginStr As String)
|
||||
_dbLoginStr = dbLoginStr
|
||||
If loadpath.Substring(loadpath.Length - 1, 1).Equals("\") Then
|
||||
_SyncLoadDirPath = loadpath
|
||||
Else
|
||||
_SyncLoadDirPath = loadpath & "\"
|
||||
End If
|
||||
|
||||
|
||||
End Sub
|
||||
Sub New()
|
||||
|
||||
End Sub
|
||||
Public Sub FtpfileMain()
|
||||
|
||||
'开启线程
|
||||
FtpfileThread()
|
||||
|
||||
End Sub
|
||||
|
||||
|
||||
|
||||
Public Sub FtpfileThread()
|
||||
Dim cnt As Integer = 50
|
||||
Dim _FilePath_li As List(Of String)
|
||||
Dim loadFilerow As List(Of Dictionary(Of String, String))
|
||||
Dim FileMysqlRow As List(Of Dictionary(Of String, String))
|
||||
Dim SyncToLoadFile As Dictionary(Of String, String)
|
||||
_startFlag = 0
|
||||
While True
|
||||
|
||||
If cnt > num Then
|
||||
cnt = 0
|
||||
_startFlag = 0
|
||||
RaiseEvent ParseUdpData(0)
|
||||
num = 30
|
||||
_FilePath_li = Enumerationdirectory(_SyncLoadDirPath)
|
||||
'本地文件信息储存
|
||||
loadFilerow = filetosqlfunction(_FilePath_li)
|
||||
' 获取数据库文件数据
|
||||
FileMysqlRow = GetMysqlfiledata(_dbLoginStr)
|
||||
' 文件比对
|
||||
SyncToLoadFile = Proofreadfile(loadFilerow, FileMysqlRow)
|
||||
|
||||
' FTP下载
|
||||
FTPDownloadFile(SyncToLoadFile)
|
||||
|
||||
|
||||
End If
|
||||
cnt += 1
|
||||
Thread.Sleep(60000)
|
||||
End While
|
||||
|
||||
End Sub
|
||||
|
||||
|
||||
Public Function FtpfileDownload(dirpath As String, filename As String, md5 As String) As Boolean
|
||||
|
||||
CUtsFtp.InitConnectParams(FtpPort, FtpUser, FtpPwd)
|
||||
Dim ftp As CUtsFtp = CUtsFtp.CreateObject()
|
||||
ftp.FtpHost = FtpHost
|
||||
|
||||
|
||||
If ftp.FtpDownload(dirpath, filename) = FtpStatus.Success Then
|
||||
If VerifyFileMD5(filename, md5) Then
|
||||
Return True
|
||||
Else
|
||||
Return False
|
||||
End If
|
||||
Else
|
||||
Return False
|
||||
End If
|
||||
|
||||
|
||||
|
||||
End Function
|
||||
|
||||
Public Function VerifyFileMD5(filePath As String, fileMd5 As String) As Boolean
|
||||
|
||||
If IO.File.Exists(filePath) Then
|
||||
Dim loadMD5 As String = GetStringMd5(filePath)
|
||||
fileMd5 = fileMd5.ToUpper
|
||||
loadMD5 = loadMD5.ToUpper
|
||||
If fileMd5.Equals(loadMD5) Then
|
||||
Return True
|
||||
Else
|
||||
Return False
|
||||
End If
|
||||
Else
|
||||
Return False
|
||||
End If
|
||||
End Function
|
||||
|
||||
Public Function GetStringMd5(filePath As String) As String
|
||||
Dim dataFile() As Byte = File.ReadAllBytes(filePath)
|
||||
|
||||
Dim databuff As Byte() = MD5.Create().ComputeHash(dataFile)
|
||||
Dim MD5str As String = BitConverter.ToString(databuff)
|
||||
Return MD5str.Replace("-", "")
|
||||
'Console.WriteLine($"md5-1:{MD5str.Replace("-", "")}")
|
||||
End Function
|
||||
'''' <summary>
|
||||
'''' 获取指定文件夹下所有文件
|
||||
'''' 获取同步文件夹文件
|
||||
'''' </summary>
|
||||
'''' <param name="dirpath"></param>
|
||||
'Public Function Enumerationdirectory(dirpath As String) As List(Of String)
|
||||
' If IO.Directory.Exists(dirpath) Then
|
||||
' Dim filebufF() As String = IO.Directory.GetFiles(dirpath)
|
||||
' If filebufF.Length > 0 Then
|
||||
' _FilePath_li.AddRange(filebufF)
|
||||
' End If
|
||||
|
||||
' Dim dirbuff() As String = IO.Directory.GetDirectories(dirpath)
|
||||
' _DirPah_li.AddRange(dirbuff)
|
||||
' For Each partdir In dirbuff
|
||||
' Enumerationdirectory(partdir)
|
||||
' Next
|
||||
' Else
|
||||
' MessageBox.Show("同步文件夹不存在")
|
||||
' End If
|
||||
|
||||
'End Function
|
||||
|
||||
Public Function Enumerationdirectory(dirpath As String) As List(Of String)
|
||||
Dim filelist As New List(Of String)
|
||||
Dim dirlist As New List(Of String)
|
||||
If IO.Directory.Exists(dirpath) Then
|
||||
|
||||
Dim filebufF() As String = IO.Directory.GetFiles(dirpath)
|
||||
If filebufF.Length > 0 Then
|
||||
filelist.AddRange(filebufF)
|
||||
End If
|
||||
|
||||
Dim dirbuff() As String = IO.Directory.GetDirectories(dirpath)
|
||||
dirlist.AddRange(dirbuff)
|
||||
For Each partdir In dirbuff
|
||||
filelist.AddRange(Enumerationdirectory(partdir))
|
||||
Next
|
||||
Else
|
||||
MessageBox.Show("同步文件夹不存在")
|
||||
End If
|
||||
Return filelist
|
||||
End Function
|
||||
|
||||
|
||||
Public Function filetosqlfunction(_FilePath_li As List(Of String)) As List(Of Dictionary(Of String, String))
|
||||
Dim loadFilerow As New List(Of Dictionary(Of String, String))
|
||||
For Each filestr In _FilePath_li
|
||||
' Dim filedata As New FileToSqlVariable
|
||||
Dim clunm As New Dictionary(Of String, String)
|
||||
Dim md5 As String = Hash.Content(IO.File.ReadAllText(filestr))
|
||||
Dim filedir As String = filestr.Substring(0, filestr.LastIndexOf("\") + 1)
|
||||
|
||||
Dim filename As String = filestr.Substring(filedir.Length, filestr.Length - filedir.Length)
|
||||
clunm.Add("Directory", filedir)
|
||||
clunm.Add("Available", 1)
|
||||
clunm.Add("FileName", filename)
|
||||
clunm.Add("UploadDateTime", Now.ToString("yyyy-MM-dd HH:mm:ss.fff"))
|
||||
clunm.Add("MD5", md5)
|
||||
loadFilerow.Add(clunm)
|
||||
'filedata_li.Add(filedata)
|
||||
Next
|
||||
Return loadFilerow
|
||||
End Function
|
||||
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 获取数据库文件数据
|
||||
''' </summary>
|
||||
''' <param name="DbConnString"></param>
|
||||
''' <returns></returns>
|
||||
|
||||
Public Function GetMysqlfiledata(DbConnString As String) As List(Of Dictionary(Of String, String))
|
||||
'Dim sqlfile As New Dictionary(Of Integer, List(Of String))
|
||||
Dim FileMysqlRow As New List(Of Dictionary(Of String, String))
|
||||
Try
|
||||
Dim dt As DataTable
|
||||
|
||||
Using db As New DbExecutor(DbExecutor.DbTypeEnum.Mysql, DbConnString)
|
||||
db.Open()
|
||||
dt = db.ExecuteDataTable(db.CmdHelper.SearchAll("tbl_model_file_data", $"Available = '1'"))
|
||||
db.Close()
|
||||
End Using
|
||||
For Each dtrow As DataRow In dt.Rows
|
||||
Dim clunm As New Dictionary(Of String, String)
|
||||
For i As Integer = 0 To dt.Columns.Count - 1
|
||||
'Console.WriteLine("argRoomTypeNodeIdx = " & dtrow.Item(i))
|
||||
'If dtrow.Item(i).GetType = "DbNull" Then
|
||||
' clunm.Add(dt.Columns(i).ColumnName, "")
|
||||
' Continue For
|
||||
'End If
|
||||
clunm.Add(dt.Columns(i).ColumnName, dtrow.Item(i).ToString)
|
||||
Next
|
||||
FileMysqlRow.Add(clunm)
|
||||
Next
|
||||
Catch ex As Exception
|
||||
|
||||
End Try
|
||||
Return FileMysqlRow
|
||||
End Function
|
||||
''' <summary>
|
||||
''' 文件比对
|
||||
''' </summary>
|
||||
Public Function Proofreadfile(loadFilerow As List(Of Dictionary(Of String, String)), FileMysqlRow As List(Of Dictionary(Of String, String)))
|
||||
'下载标记 False 下载
|
||||
Dim SyncToLoadFile As New Dictionary(Of String, String)
|
||||
For i As Integer = 0 To FileMysqlRow.Count - 1
|
||||
Dim download_flag As Boolean = False
|
||||
Dim serverRow As Dictionary(Of String, String) = FileMysqlRow.Item(i)
|
||||
For j As Integer = 0 To loadFilerow.Count - 1
|
||||
Dim loadRow As Dictionary(Of String, String) = loadFilerow.Item(j)
|
||||
If Filecomparison(serverRow, loadRow) Then
|
||||
loadFilerow.RemoveAt(j)
|
||||
download_flag = True
|
||||
Exit For
|
||||
Else
|
||||
download_flag = False
|
||||
Continue For
|
||||
End If
|
||||
|
||||
Next
|
||||
|
||||
|
||||
If download_flag Then
|
||||
Continue For
|
||||
Else
|
||||
'Dim syncfile As New Dictionary(Of String, String)
|
||||
''云文件完整路径
|
||||
'Dim Syncpath = _FileMysqlRow.Item(i).Item("Directory") & "\" & _FileMysqlRow.Item(i).Item("FileName")
|
||||
'云文件相对路径
|
||||
Dim filepath = FileMysqlRow.Item(i).Item("Directory") & "\" & FileMysqlRow.Item(i).Item("XML_FileName")
|
||||
'syncfile.Add(Syncpath, RelativePath)
|
||||
Dim Loadfilepath As String = filepath.Replace("\Data\Model\", "")
|
||||
SyncToLoadFile.Add(filepath, _SyncLoadDirPath & Loadfilepath)
|
||||
|
||||
End If
|
||||
|
||||
Next
|
||||
loadFilerow.Clear()
|
||||
FileMysqlRow.Clear()
|
||||
Return SyncToLoadFile
|
||||
End Function
|
||||
|
||||
Public Function Filecomparison(sqlfile As Dictionary(Of String, String), loadfile As Dictionary(Of String, String)) As Boolean
|
||||
|
||||
Dim loadpath = loadfile.Item("Directory").Replace(_SyncLoadDirPath, "_\") & loadfile.Item("FileName")
|
||||
Dim serverpath = sqlfile.Item("Directory").Replace("\Data\Model", "_") & "\" & sqlfile.Item("XML_FileName")
|
||||
|
||||
If loadpath.Equals(serverpath) AndAlso loadfile.Item("MD5").Equals(sqlfile.Item("XLM_MD5")) Then
|
||||
Return True
|
||||
End If
|
||||
Return False
|
||||
End Function
|
||||
Public FtpHost As String = "blv-oa.com"
|
||||
Public FtpPort As Integer = 50
|
||||
Public FtpUser As String = "BLV_Studio"
|
||||
Public FtpPwd As String = "37f5675t6R&5*"
|
||||
''' <summary>
|
||||
''' FTP下载
|
||||
''' </summary>
|
||||
Public Sub FTPDownloadFile(SyncToLoadFile As Dictionary(Of String, String))
|
||||
Try
|
||||
CUtsFtp.InitConnectParams(FtpPort, FtpUser, FtpPwd)
|
||||
Dim ftp As CUtsFtp = CUtsFtp.CreateObject()
|
||||
ftp.FtpHost = FtpHost
|
||||
'For Each filepath In _SyncToLoadFile
|
||||
|
||||
'ftp.FtpDownload("/Data/Model/485Model/3.txt", _SyncLoadDirPath & "3.txt")
|
||||
ftp.FtpDownload(SyncToLoadFile)
|
||||
SyncToLoadFile.Clear()
|
||||
' Next
|
||||
Catch ex As Exception
|
||||
_startFlag = 1
|
||||
num = 10
|
||||
RaiseEvent ParseUdpData(1)
|
||||
'AdminLog.ApplicationLog.WriteErrorLog(ex)
|
||||
End Try
|
||||
_startFlag = 2
|
||||
num = 60
|
||||
RaiseEvent ParseUdpData(2)
|
||||
|
||||
|
||||
|
||||
End Sub
|
||||
|
||||
|
||||
Public Function Utf8ToGB2312(utf8str As String) As String
|
||||
Dim temp() As Byte
|
||||
temp = Text.Encoding.Default.GetBytes(utf8str)
|
||||
Text.Encoding.Convert(Text.Encoding.GetEncoding("utf-8"), Text.Encoding.GetEncoding("gb2312"), temp)
|
||||
Return Text.Encoding.Default.GetString(temp)
|
||||
End Function
|
||||
|
||||
Public Event ParseUdpData(ftpflag As Integer)
|
||||
|
||||
|
||||
|
||||
End Class
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user