过程如下:
Private Sub StartDownLoadBmp()
' 获取客户端BMP存贮位置,创建文件夹
Dim BmpPath, CreateBmpPath
BmpPath = funReadlist(App.Path & "\info.xml", "bmppath")
If BmpPath(0) <> "错误" Then
CreateBmpPath = CreatePath(App.Path & "\FileInfo\FileTree.xml", (BmpPath(0)))
If CreateBmpPath = True Then
Lblmsg.Caption = "创建图档文件夹成功"
Else
Lblmsg.Caption = "创建图档文件夹失败"
Exit Sub
End If
End If
'创建文件夹结束
'比对FileTree中的图档列表与本地图档txt文件
Dim GetFileTree, MyFileTxt
Dim GetBmpTime, GetDBTime
Dim ConnectFTP
Dim a, b As Integer
a = 0
b = 0
GetFileTree = funReadlist(App.Path & "\FileInfo\FileTree.xml", "FileNameList")
If GetFileTree(0) <> "错误" Then
ConnectFTP = 连接服务器(InetDF.RemoteHost, InetDF.RemotePort, InetDF.UserName, InetDF.Password)
Do While a < UBound(GetFileTree)
MyFileTxt = GetFileToArr(App.Path & "\FileInfo\" & GetFileTree(a) & ".txt")
Dim LineLeft, LineRight, FileName As String
Do While b < UBound(MyFileTxt)
GetBmpTime = ""
LineLeft = Split(MyFileTxt(b), "=")(0)
LineRight = Split(MyFileTxt(b), "=")(1)
FileName = Split(LineRight, "\")(UBound(Split(LineRight, "\")))
GetBmpTime = GetLastTime(BmpPath(0) & "\" & GetFileTree(a) & LineLeft & "\" & FileName)
If GetBmpTime = "" Then
Debug.Print Trim("ftp://" & InetDF.RemoteHost & LineRight & "," & BmpPath(0) & "\" & GetFileTree(a) & LineLeft & "\" & FileName)
ConnectFTP = 下载文件(Trim("ftp://" & InetDF.RemoteHost & LineRight), Trim(BmpPath(0) & "\" & GetFileTree(a) & LineLeft & "\" & FileName))
DoEvents
End If
b = b + 1
Loop
a = a + 1
Loop
End If
'比对结束End Sub
模块如下:
Option Explicit
Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Private Const INTERNET_OPEN_TYPE_DIRECT = 1
Private Const INTERNET_OPEN_TYPE_PROXY = 3
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hFtpSession As Long, ByVal lpszLocalFile As String, ByVal lpszRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
Private Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" (ByVal hFtpSession As Long, ByVal lpszExsiting As String, ByVal lpszNew As String) As Boolean
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
Private Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession&, ByVal lpszDirectory$) As Boolean
Dim 连接状态 As Boolean
Dim 连接句柄 As Long
Public Function 连接服务器(IP As String, 端口 As Integer, 帐号 As String, 密码 As String) As Boolean
Dim 初始化 As Long
Dim INTERNET_OPEN_TYPE_PRECONFIG As Long
初始化 = InternetOpen(vbNullString, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0&)
连接句柄 = InternetConnect(初始化, IP, 端口, 帐号, 密码, 1, 0, 0)
If 连接句柄 > 0 Then
连接状态 = True
Else
连接状态 = False
End If
连接服务器 = 连接状态
End Function
Public Function 上传文件(本地路径 As String, 远程路径 As String) As Boolean
If 连接状态 = True Then
上传文件 = FtpPutFile(连接句柄, 本地路径, 远程路径, 1, 0)
Else
MsgBox "请先确认连接" & ":上传文件"
End If
End Function
Public Function 下载文件(远程路径 As String, 本地路径 As String) As Boolean
If 连接状态 = True Then
下载文件 = FtpGetFile(连接句柄, 远程路径, 本地路径, False, &H80000000, &H0, 0)
Else
MsgBox "请先确认连接" & ":下载文件"
End If
End Function
Public Function 删除文件(ByVal 文件路径) As Boolean
If 连接状态 = True Then
删除文件 = FtpDeleteFile(连接句柄, 文件路径)
Else
MsgBox "请先确认连接" & ":删除文件"
End If
End Function
Public Function 创建目录(ByVal 路径 As String) As String
If 连接状态 = True Then
创建目录 = FtpCreateDirectory(连接句柄, 路径)
Else
MsgBox "请先确认连接" & ":创建目录"
End If
End Function
Public Function 文件改名(ByVal 原文件名 As String, ByVal 新文件名 As String)
If 连接状态 = True Then
ReturnVal& = FtpRenameFile(连接句柄, 原文件名, 新文件名)
Else
MsgBox "请先确认连接" & ":文件改名"
End If
End Function各位VB大神,求解,我的所有FTP上的BMP文件的路径都存在一个数组里,BMP文件的大小大约为4MB,怎样把所有的BMP文件都下载到本地,目前用的是WinInet下载,但是下载下来文件大小全部为0,并且后面一个文件会覆盖前面的文件
Private Sub StartDownLoadBmp()
' 获取客户端BMP存贮位置,创建文件夹
Dim BmpPath, CreateBmpPath
BmpPath = funReadlist(App.Path & "\info.xml", "bmppath")
If BmpPath(0) <> "错误" Then
CreateBmpPath = CreatePath(App.Path & "\FileInfo\FileTree.xml", (BmpPath(0)))
If CreateBmpPath = True Then
Lblmsg.Caption = "创建图档文件夹成功"
Else
Lblmsg.Caption = "创建图档文件夹失败"
Exit Sub
End If
End If
'创建文件夹结束
'比对FileTree中的图档列表与本地图档txt文件
Dim GetFileTree, MyFileTxt
Dim GetBmpTime, GetDBTime
Dim ConnectFTP
Dim a, b As Integer
a = 0
b = 0
GetFileTree = funReadlist(App.Path & "\FileInfo\FileTree.xml", "FileNameList")
If GetFileTree(0) <> "错误" Then
ConnectFTP = 连接服务器(InetDF.RemoteHost, InetDF.RemotePort, InetDF.UserName, InetDF.Password)
Do While a < UBound(GetFileTree)
MyFileTxt = GetFileToArr(App.Path & "\FileInfo\" & GetFileTree(a) & ".txt")
Dim LineLeft, LineRight, FileName As String
Do While b < UBound(MyFileTxt)
GetBmpTime = ""
LineLeft = Split(MyFileTxt(b), "=")(0)
LineRight = Split(MyFileTxt(b), "=")(1)
FileName = Split(LineRight, "\")(UBound(Split(LineRight, "\")))
GetBmpTime = GetLastTime(BmpPath(0) & "\" & GetFileTree(a) & LineLeft & "\" & FileName)
If GetBmpTime = "" Then
Debug.Print Trim("ftp://" & InetDF.RemoteHost & LineRight & "," & BmpPath(0) & "\" & GetFileTree(a) & LineLeft & "\" & FileName)
ConnectFTP = 下载文件(Trim("ftp://" & InetDF.RemoteHost & LineRight), Trim(BmpPath(0) & "\" & GetFileTree(a) & LineLeft & "\" & FileName))
DoEvents
End If
b = b + 1
Loop
a = a + 1
Loop
End If
'比对结束End Sub
模块如下:
Option Explicit
Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Private Const INTERNET_OPEN_TYPE_DIRECT = 1
Private Const INTERNET_OPEN_TYPE_PROXY = 3
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hFtpSession As Long, ByVal lpszLocalFile As String, ByVal lpszRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
Private Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" (ByVal hFtpSession As Long, ByVal lpszExsiting As String, ByVal lpszNew As String) As Boolean
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
Private Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession&, ByVal lpszDirectory$) As Boolean
Dim 连接状态 As Boolean
Dim 连接句柄 As Long
Public Function 连接服务器(IP As String, 端口 As Integer, 帐号 As String, 密码 As String) As Boolean
Dim 初始化 As Long
Dim INTERNET_OPEN_TYPE_PRECONFIG As Long
初始化 = InternetOpen(vbNullString, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0&)
连接句柄 = InternetConnect(初始化, IP, 端口, 帐号, 密码, 1, 0, 0)
If 连接句柄 > 0 Then
连接状态 = True
Else
连接状态 = False
End If
连接服务器 = 连接状态
End Function
Public Function 上传文件(本地路径 As String, 远程路径 As String) As Boolean
If 连接状态 = True Then
上传文件 = FtpPutFile(连接句柄, 本地路径, 远程路径, 1, 0)
Else
MsgBox "请先确认连接" & ":上传文件"
End If
End Function
Public Function 下载文件(远程路径 As String, 本地路径 As String) As Boolean
If 连接状态 = True Then
下载文件 = FtpGetFile(连接句柄, 远程路径, 本地路径, False, &H80000000, &H0, 0)
Else
MsgBox "请先确认连接" & ":下载文件"
End If
End Function
Public Function 删除文件(ByVal 文件路径) As Boolean
If 连接状态 = True Then
删除文件 = FtpDeleteFile(连接句柄, 文件路径)
Else
MsgBox "请先确认连接" & ":删除文件"
End If
End Function
Public Function 创建目录(ByVal 路径 As String) As String
If 连接状态 = True Then
创建目录 = FtpCreateDirectory(连接句柄, 路径)
Else
MsgBox "请先确认连接" & ":创建目录"
End If
End Function
Public Function 文件改名(ByVal 原文件名 As String, ByVal 新文件名 As String)
If 连接状态 = True Then
ReturnVal& = FtpRenameFile(连接句柄, 原文件名, 新文件名)
Else
MsgBox "请先确认连接" & ":文件改名"
End If
End Function各位VB大神,求解,我的所有FTP上的BMP文件的路径都存在一个数组里,BMP文件的大小大约为4MB,怎样把所有的BMP文件都下载到本地,目前用的是WinInet下载,但是下载下来文件大小全部为0,并且后面一个文件会覆盖前面的文件
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货