初始化项目
This commit is contained in:
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