Imports BLV_Studio.UTSModule Imports MD5Hash Imports System.IO Imports System.Security.Cryptography Imports System.Security.Policy Imports System.Text Imports System.Threading Public Class blvFtpServer ''' ''' 单独下载控制开关 和 separateMap 一起使用 ''' Public separateSart As Boolean = False ''' ''' 单独下载 文件集合 <云文件路径本地文件路径> ''' Public separateMap As New Dictionary(Of String, String) 'Public password As String = MD5Hash.Hash.Content("123") ''' ''' 文件同步线程 ''' Public _syn_thread As Thread ''' ''' 本地同步文件夹路径 ''' Public _SyncLoadDirPath As String ''' ''' 数据库登录 ''' Public _dbLoginStr As String ''' ''' 数据库同步文件集 所在文件夹路径 *文件名 ''' ''' ''' 需要下载的文件集 相对路径文件名 ''' ''' ''' FTP同步标志 ''' Public _startFlag As Integer = 0 ''' ''' FTP同步间隔 ''' Public num As Integer = 30 Public IsRuning As Boolean ''' ''' ''' ''' 同步文件夹本地路径 ''' 同步云文件夹路径 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 Public Sub FtpfileMain() '开启线程 IsRuning = True FtpfileThread() End Sub Public Sub FtpfileDownload(dirpath As String, filename As String) Try UtsFtp.InitConnectParams(FtpPort, FtpUser, FtpPwd) Dim ftp As UtsFtp = UtsFtp.CreateObject() ftp.FtpHost = FtpHost 'For Each filepath In _SyncToLoadFile 'ftp.FtpDownload("/Data/Model/485Model/3.txt", _SyncLoadDirPath & "3.txt") ftp.FtpDownload(dirpath, filename) ' Next Catch ex As Exception End Try 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 IsRuning If cnt > num Then cnt = 0 _startFlag = 0 RaiseEvent updateIcon(0) num = 1800 _FilePath_li = Enumerationdirectory(_SyncLoadDirPath) '本地文件信息储存 loadFilerow = filetosqlfunction(_FilePath_li) ' 获取数据库文件数据 FileMysqlRow = GetMysqlfiledata(_dbLoginStr) ' 文件比对 SyncToLoadFile = Proofreadfile(loadFilerow, FileMysqlRow) ' FTP下载 FTPDownloadFile(SyncToLoadFile, FileMysqlRow) FileMysqlRow.Clear() SyncNumber.Clear() SyncToLoadFile.Clear() End If If separateSart Then _startFlag = 0 RaiseEvent updateIcon(0) Dim filename As String = separateMap.Keys(0) filename = filename.Substring(filename.LastIndexOf("\") + 1) filename = $" `CONFIG_XML` = '{filename}' " FileMysqlRow = GetMysqlfiledata(_dbLoginStr, filename) If FileMysqlRow.Count > 0 Then SyncNumber.Add(0) End If FTPDownloadFile(separateMap, FileMysqlRow.Item(0)) FileMysqlRow.Clear() separateMap.Clear() SyncNumber.Clear() separateSart = False End If cnt += 1 Thread.Sleep(1000) End While End Sub '''' '''' 获取指定文件夹下所有文件 '''' 获取同步文件夹文件 '''' '''' '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(ByRef _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 = GetStringMd5(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 _FilePath_li.Clear() Return loadFilerow End Function ''' ''' 获取数据库文件数据 ''' ''' ''' 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 ''' ''' ''' ''' ''' ''' Public Function GetMysqlfiledata(DbConnString As String, where 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_room_type_list", where)) 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 Public SyncNumber As New List(Of Integer) ''' ''' 文件比对 ''' Public Function Proofreadfile(ByRef loadFilerow As List(Of Dictionary(Of String, String)), ByRef 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\", "") SyncNumber.Add(i) '储存节点,便于枚举云MD5值 SyncToLoadFile.Add(filepath, _SyncLoadDirPath & Loadfilepath) End If Next loadFilerow.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*" ''' ''' FTP下载 ''' Public Sub FTPDownloadFile(SyncToLoadFile As Dictionary(Of String, String), dic As Dictionary(Of String, String)) Try UtsFtp.InitConnectParams(FtpPort, FtpUser, FtpPwd) Dim ftp As UtsFtp = UtsFtp.CreateObject() ftp.FtpHost = FtpHost 'For Each filepath In _SyncToLoadFile 'ftp.FtpDownload("/Data/Model/485Model/3.txt", _SyncLoadDirPath & "3.txt") If ftp.FtpDownload(SyncToLoadFile, True) = 1 Then If VerifyFileMD5(SyncToLoadFile.Values(0), dic.Item("CONFIG_XML_MD5")) Then If VerifyFileMD5(SyncToLoadFile.Values(0), dic.Item("CONFIG_BIN_MD5")) Then Else _startFlag = 1 num = 600 RaiseEvent updateIcon(1) Return End If Else _startFlag = 1 num = 600 RaiseEvent updateIcon(1) Return End If Else _startFlag = 1 num = 600 RaiseEvent updateIcon(1) Return End If '检验文件是否下载到本地,和确保下载的文件是数据库的文件 _startFlag = 2 num = 3600 RaiseEvent updateIcon(2) Catch ex As Exception _startFlag = 1 num = 600 RaiseEvent updateIcon(1) Return 'AdminLog.ApplicationLog.WriteErrorLog(ex) End Try End Sub Public Sub FTPDownloadFile(SyncToLoadFile As Dictionary(Of String, String), FileMysqlRow As List(Of Dictionary(Of String, String))) Try UtsFtp.InitConnectParams(FtpPort, FtpUser, FtpPwd) Dim ftp As UtsFtp = UtsFtp.CreateObject() ftp.FtpHost = FtpHost 'For Each filepath In _SyncToLoadFile If SyncToLoadFile.Count = 0 Then _startFlag = 2 num = 3600 RaiseEvent updateIcon(2) Return End If 'ftp.FtpDownload("/Data/Model/485Model/3.txt", _SyncLoadDirPath & "3.txt") If ftp.FtpDownload(SyncToLoadFile) = 1 Then For i As Integer = 0 To SyncToLoadFile.Count - 1 '检验文件是否下载到本地,和确保下载的文件是数据库的文件 If VerifyFileMD5(SyncToLoadFile.Values(i), FileMysqlRow.Item(SyncNumber.Item(i)).Item("XLM_MD5")) Then Else _startFlag = 1 num = 600 RaiseEvent updateIcon(1) Return End If Next Else _startFlag = 1 num = 600 RaiseEvent updateIcon(1) Return End If _startFlag = 2 num = 3600 RaiseEvent updateIcon(2) Catch ex As Exception _startFlag = 1 num = 600 RaiseEvent updateIcon(1) Return 'AdminLog.ApplicationLog.WriteErrorLog(ex) End Try 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) = 1 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 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 updateIcon(ftpflag As Integer) End Class