公告:

毕业五年,一切如走马观灯、过眼烟云,如梦境般似真亦幻,当年的豪言壮语已随风飘逝,本我、自我、超我不断碰撞,最后只剩下一具缺乏灵魂的僧侣,一生被定格在那个片段,固定的作息、固定的生活、固定的朋友,不知千年的轮回,是否只为这一生孤寂的宿命。

VB API实现FTP上传、下载、创建目录、删除文件、改名类模块

调用示例:

Private Sub Form_Load()
    Dim F As FTP
    Set F = New FTP

    Call F.连接服务器("127.0.0.1", 21, "admin", "admin888")

    Call F.下载文件("/wwwroot/index.php", "c:\test.php")
End Sub

FTP类模块
cls:

VBScript code复制代码
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
    初始化 = 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



文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
评论: 0 | 引用: 0 | 查看次数: 29025
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭