我通过ftpGetFile函数来从ftp服务器上下载文件。
当连接到一个局域网内部的ftp服务器上可以正常的取得文件。
但当连接到一个外网中的ftp服务器时,调用ftpGetFile就会失败。
错误号12003 The system cannot find the file specified.
不知道是什么原因。希望兄弟们指教!谢谢!Private Function GetFileFromFtpServer(ByRef mFile As CProgramFile, _
                                      ByRef SourcePath As String, _
                                      ByRef DestinationPath As String, _
                                      ByRef Rename As Boolean, _
                                      Optional ByRef NewFileName As String = Empty) As enumAutoUpdateErrors
  Dim mblnRet As Boolean
  Dim lngInet As Long
  Dim lngInetconn As Long
  Dim blnRC As Long
  Dim mstrRemoteFileName As String    '源文件全称
  Dim mstrUniqueFileName As String    '唯一文件名
  Dim mstrLocalFileName As String     '目的文件名称    On Error GoTo GetFileFromFtpServer_Err
    
    '初始化网络连接
    lngInet = InternetOpen(vbNullString, INTERNET_OPEN_TYPE_PROXY, vbNullString, vbNullString, 0&)
    If lngInet = 0 Then
        Err.Raise 65535, "GetFileFromFtpServer.InternetOpen", "初始化网络连接时出错"
    End If
    
    '连接到FTP
    lngInetconn = InternetConnect(lngInet, mstrFtpServerName, INTERNET_INVALID_PORT_NUMBER, mstrUserName, mstrUserPassword, INTERNET_SERVICE_FTP, 0, 0)
    If lngInetconn = 0 Then
        Err.Raise 65534, "GetFileFromFtpServer.InernetConnect", "不能连接到FTP服务器"
    End If
    
    '源文件全称=升级路径+文件名称
    If Right$(SourcePath, 1) = "/" Then
        mstrRemoteFileName = SourcePath & mFile.FileName
    Else
        mstrRemoteFileName = SourcePath & "/" & mFile.FileName
    End If
    
    '新文件是否重新命名
    If Rename Then
        '得到一个唯一名称
        mstrUniqueFileName = GetUniqueFileName(DestinationPath, "NEW")
        If mstrUniqueFileName = Empty Then
            Err.Raise 65533, "GetFileFromFtpServer.GetUniqueFileName", "不能得到一个唯一的文件名"
        End If
        NewFileName = mstrUniqueFileName    '将名称传给参数,用于返回
        mstrLocalFileName = mstrUniqueFileName
    Else
        If Right(DestinationPath, 1) = "\" Then
            mstrLocalFileName = DestinationPath & mFile.FileName
        Else
            mstrLocalFileName = DestinationPath & "\" & mFile.FileName
        End If
    End If
            
    If Not mblnRet Then
        'blnRC = FtpGetFile(lngInetconn, mstrRemoteFileName, mstrLocalFileName, False _
                        , FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY + INTERNET_FLAG_RESYNCHRONIZE, 0)
        blnRC = FtpGetFile(lngInetconn, mstrRemoteFileName, mstrLocalFileName, 0, 0, 1, 0)
        If blnRC <> 1 Then
            Err.Raise 65532, "GetFileFormFtpServer.FtpGetFile", "从Ftp服务器取得文件时发生错误"
        End If
    End If

解决方案 »

  1.   

    //错误号12003 The system cannot find the file specified意思是没找到相应的文件,检查一下你的文件路径是否正确
      

  2.   

    留下email我给你发一个完整的程序
      

  3.   

    哦呵呵,我也要~~[email protected]
      

  4.   

    路径应该没有问题的!
    [email protected] 谢谢!
      

  5.   

    呵呵
    還有我
    [email protected]
    謝啦!!!
      

  6.   

    谢谢各位!我的问题解决了!
    就是在get文件之前要用ftpsetcurrentdirectory设置一下当前的工作目录。
    呵呵!
    还是希望暴风雨老大把代码给我们开开眼!!呵呵!!
      

  7.   

    re://就是在get文件之前要用ftpsetcurrentdirectory设置一下当前的工作目录。
    如果你使用的是绝对路径,那么不会有此问题。就象在本地使用磁盘文件一样。
    至于例子嘛,我也有一个:
    Private Type WIN32_FIND_DATA
        dwFileAttributes As Long
        ftCreationTime As FILETIME
        ftLastAccessTime As FILETIME
        ftLastWriteTime As FILETIME
        nFileSizeHigh As Long
        nFileSizeLow As Long
        dwReserved0 As Long
        dwReserved1 As Long
        cFileName As String * MAX_PATH
        cAlternate As String * 14
    End Type
    Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
    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 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 FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
    Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Long
    Private Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
    Private Declare Function FtpRemoveDirectory Lib "wininet.dll" Alias "FtpRemoveDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) 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 lpszExisting As String, ByVal lpszNew As String) As Boolean
    Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hConnect As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Long, ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, ByRef dwContext As Long) As Boolean
    Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hConnect As Long, ByVal lpszLocalFile As String, ByVal lpszNewRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
    Private Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" (lpdwError As Long, ByVal lpszBuffer As String, lpdwBufferLength As Long) As Boolean
    Private Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long
    Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long
    Const PassiveConnection As Boolean = True
    Private Sub Form_Load()
        'KPD-Team 2000
        'URL: http://www.allapi.net
        'E-Mail: [email protected]
        Dim hConnection As Long, hOpen As Long, sOrgPath  As String
        'open an internet connection
        hOpen = InternetOpen("API-Guide sample program", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
        'connect to the FTP server
        hConnection = InternetConnect(hOpen, "your ftp server", INTERNET_DEFAULT_FTP_PORT, "your login", "your password", INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
        'create a buffer to store the original directory
        sOrgPath = String(MAX_PATH, 0)
        'get the directory
        FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
        'create a new directory 'testing'
        FtpCreateDirectory hConnection, "testing"
        'set the current directory to 'root/testing'
        FtpSetCurrentDirectory hConnection, "testing"
        'upload the file 'test.htm'
        FtpPutFile hConnection, "C:\test.htm", "test.htm", FTP_TRANSFER_TYPE_UNKNOWN, 0
        'rename 'test.htm' to 'apiguide.htm'
        FtpRenameFile hConnection, "test.htm", "apiguide.htm"
        'enumerate the file list from the current directory ('root/testing')
        EnumFiles hConnection
        'retrieve the file from the FTP server
        FtpGetFile hConnection, "apiguide.htm", "c:\apiguide.htm", False, 0, FTP_TRANSFER_TYPE_UNKNOWN, 0
        'delete the file from the FTP server
        FtpDeleteFile hConnection, "apiguide.htm"
        'set the current directory back to the root
        FtpSetCurrentDirectory hConnection, sOrgPath
        'remove the direcrtory 'testing'
        FtpRemoveDirectory hConnection, "testing"
        'close the FTP connection
        InternetCloseHandle hConnection
        'close the internet connection
        InternetCloseHandle hOpen
    End Sub
    Public Sub EnumFiles(hConnection As Long)
        Dim pData As WIN32_FIND_DATA, hFind As Long, lRet As Long
        'set the graphics mode to persistent
        Me.AutoRedraw = True
        'create a buffer
        pData.cFileName = String(MAX_PATH, 0)
        'find the first file
        hFind = FtpFindFirstFile(hConnection, "*.*", pData, 0, 0)
        'if there's no file, then exit sub
        If hFind = 0 Then Exit Sub
        'show the filename
        Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
        Do
            'create a buffer
            pData.cFileName = String(MAX_PATH, 0)
            'find the next file
            lRet = InternetFindNextFile(hFind, pData)
            'if there's no next file, exit do
            If lRet = 0 Then Exit Do
            'show the filename
            Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
        Loop
        'close the search handle
        InternetCloseHandle hFind
    End Sub
    Sub ShowError()
        Dim lErr As Long, sErr As String, lenBuf As Long
        'get the required buffer size
        InternetGetLastResponseInfo lErr, sErr, lenBuf
        'create a buffer
        sErr = String(lenBuf, 0)
        'retrieve the last respons info
        InternetGetLastResponseInfo lErr, sErr, lenBuf
        'show the last response info
        MsgBox "Error " + CStr(lErr) + ": " + sErr, vbOKOnly + vbCritical
    End Sub
      

  8.   

    re://就是在get文件之前要用ftpsetcurrentdirectory设置一下当前的工作目录。
    如果你使用的是绝对路径,那么不会有此问题。就象在本地使用磁盘文件一样。
    ——————————————————————————————————————恩。可是如过文件在根目录。比如“/UpdateList.ini”有时就能成功。有时就失败!
    不知道原因何在!谢谢!
      

  9.   

    暴风雨老大!你的程序我已收到!
    但get文件也会失败!也提示错误12003!