CInternetSessio和CFtpConnection连接FTP

要联接到FTP服务器,需要两个步骤,首先必须创建一个CInternetSession对象,用类CInterSession创建并初始化一个或几个同时存在的Internet会话(session),并描述与代理服务器的连接(如果有必要的话),如果在程序运行期间需要保持与Internet的连接,可以创建一个CInternetSession对象作为类CWinApp的成员。  

MFC中的类CFtpConnection管理我们与Internet服务器的连接,并直接操作服务器上的目录和文件,FTP是MFC的WinInet支持的三个Internet功能之一,我们需要先创建一个CInternetSession实例和一个CFtpConnection对象就可以实现和一个FTP服务器的通信,我们不需要直接创建CFtpConnection对象,而是通过调用CInternetSession::GetFtpConnection来完成这项工作。它创建CFtpConnection对象并返回一个指向该对象的指针。  

Ftp连接类的信息  

下面我们简要介绍连接类的信息  

CInternetSession对象  
CInternetSession(LPCTSTR pstrAgent,DWORD dwConText ,DWORD dwAccessType,LPCTSTR pstrProxyName,LPCTSTR pstrProxyBypass,DWORD dwFlags);  

在创建CInternetSession对象时调用这个成员函数,CInternetSession是应用程序第一个要调用的Internet函数,它将创始化内部数据结构,以备将来在应用程序中调用。如果dwFlags包含INTERNET_FLAG_ASYNC,那末从这个句柄派生的所有的句柄,在状态回调例程注冊之前,都会出现异步状态。如果沒有打开Internet连接,CInternetSession就会抛出一个例外,AfxThorowInternetException。  


GetFtpConnection()函数  
CFtpConnection* CIternetSession::GetFtpConnection(LPCTSTR pstrServer,LPCTSTR pstrUserName,LPCTSTR pstrPassword,INTERNET_PORT nPort,BOOL bPassive);  

调用这个函数建立一个FTP连接,并获得一个指向CFtpConnection对象的指针,GetFtpConnection连接到一个FTP服务器,创建并返回指向CFtpConnection对象的指针,它不在服务器上进行任何操作。如果打算读写文件,必须进行分步操作。关于查找,打开和读/写文件的信息需参考CFtpConnection和CFtpFileFind类。  

对这个函数的调用返回一个指向CFtpConnection对象的指针。如果调用失败,检查抛出的CInternetException对象,就可以确定失败的原因。  


GetFile()函数  
BOOL GetFile(LPCTSTR pstrRemoteFile,LPCTSTR pstrLocalFile,BOOL bFailExists ,DWORD dwAttributes,DWORD dwFlags,DWORD dwContext);  

调用这个成员函数,可以从FTP服务器取得文件,并且把文件保存在本地机器上。GetFile()函数是一个比较高级的例程,它可以处理所有有关从FTP服务器读文件,以及把文件存放在本地机器上的工作。如果dwFlags为FILE_TRANSFER_TYPE_ASCII,文件数据的传输也会把控制和格式符转化为Windows中的等阶符号。默认的传输模式是二进制模式,文件会以和服务器上相同的格式被下载。  

pstrRemoteFile和 pstrLocalFile可以是相对于当前目录的部分文件名,也可以是全文件名,在这两个名字中间,都既可以用反斜杠(\)或者正斜杠(/)来作为文件名的目录分隔符,GetFile()在使用前会把目录分隔符转化为适当的字符。  

可以用自己选择的值来取代dwContext默认的值,设置为上下文标识符与CFtpConnection对象的定位操作有关,这个操作由CFtpConnection中的CInternetSession对象创建。返回给CInternetSession::OnStatusCallBack的值指出了所标识操作的状态。  

如果调用成功,函数的返回为非0,否则返回0,如果调用失败,可以调用Win32函数GetLastError(),确认出错的原因。  


PutFile()函数  
BOOL PutFile(LPCTSTR pstrLocalFile, LPCTSTR pstrRemoveFile ,DWORD dwFlags, DWORD dwContext);  

调用这个成员函数可以把文件保存到FTP服务器。PutFile()函数是一个比较高级的例程,它可以处理有关把文件存放到服务器上的工作。只发送数据,或要严格控制文件传输的应用程序,应该调用OpenFile和 CInternet::Write。利用自己选择的值来取代dwContext默认的值,设置为上下文标识符,上下文标识符是CInternetSession对象创建的CFtpConnection对象的特定操作有关,这个值返回给CInternetSession::OnStateCallBack,从而把操作的状态通报给它所标识的上下文。  

如果调用成功,函数的返回为非0,否则返回0,如果调用失败,可以调用Win32函数GetLastError(),确认出错的原因。 
 
下面这个函数是我写的一个例子。供参考
/************************************************************************/
/*
参数说明: 
 FtpPath   要下载的ftp 目录或文件
 localPath    保存到本地的目录
 ftpAdd       ftp地址
 ftpSuser   登陆ftp的用户名
 ftpPass       登陆ftp的密码
 sErr          发生错误时,返回的错误信息*/
                                                        
/************************************************************************/
BOOL GetFileFromFTP( CString FtpPath, CString LocalPath ,CString ftpAdd, CString ftpUser, CString ftpPass, CString &sErr )
{
 CString sTemp ;
 CString sLocalTemp ;
 CString sTempFtpPath ;
 CInternetSession  pInternetCon(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS); 
 CFtpConnection *pFtpCon ;
 try 
 {
  pFtpCon =pInternetCon.GetFtpConnection( ftpAdd, ftpUser, ftpPass, 21 );
  if ( pFtpCon != NULL)
  {
   CFtpFileFind FtpFinder( pFtpCon ) ;
   BOOL bWork = FtpFinder.FindFile( FtpPath );
   while ( bWork )
   {
    bWork = FtpFinder.FindNextFile() ;
    if ( FtpFinder.IsDots() ) 
     continue;
    if(FtpFinder.IsDirectory() )
    {  //是目录
     sTemp.Empty() ;
     sTemp = FtpFinder.GetFileName() ;
     sTempFtpPath.Empty() ;
     sTempFtpPath = FtpPath.Left( FtpPath.GetLength() - 3 )  ;
     sTempFtpPath += sTemp + "//*.*" ;
     sLocalTemp.Empty() ;
     sLocalTemp = LocalPath + "\\" + sTemp ;
     CFileStatus   rStatus;
     if( !( CFile::GetStatus( sLocalTemp, rStatus ) ) )
     {//文件夹不存在, 创建之
      if( !(CreateDirectory((LPCTSTR)sLocalTemp, NULL)) )
      {//创建失败
       DWORD dErr = GetLastError() ;
       sErr.Format( "创建新目录失败: ERROR  %d" ,dErr ) ;
       return FALSE ; 
      }
     }
     GetFileFromFTP( sTempFtpPath, sLocalTemp, ftpAdd, ftpUser, ftpPass, sErr ) ;
    }
    else
    {
     sTemp.Empty() ;
     sTemp = FtpFinder.GetFileName();
     sLocalTemp.Empty() ;
     sLocalTemp = LocalPath + "\\" + sTemp ;
     sTemp.Empty() ;
     sTemp = FtpFinder.GetFilePath();//
     CFileStatus   rStatus;
     if( !( CFile::GetStatus( sLocalTemp, rStatus ) ) )
     {//文件不存在,下载之
      if( !( pFtpCon->GetFile( sTemp, sLocalTemp, true, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 1) ) )
      {//下载失败
       DWORD dErr = GetLastError() ;
       sErr.Format( "下载失败: ERROR  %d" ,dErr ) ;
       return FALSE ;
      } 
     }
    }
   }
   FtpFinder.Close() ;
      }      
 }
 catch (CInternetException * pEx)
 {
  TCHAR szError[1024];
  pEx->GetErrorMessage(szError,1024) ;
  sErr.Format( "连接FTP失败:%s", pEx ) ;
  pEx->Delete();
  pFtpCon=NULL;
  return FALSE ;
 }
 return TRUE ;
}

例子二,代码如下:
//
//*********************************************************
//Ftp basic operation
//*********************************************************
//
//
//1. connect to ftp
//
BOOL flag;
CString   cstrFtpServer   = TEXT("10.142.252.155"); //ftp server address
CString   cstrFtpUserName = TEXT("pdmug");          //user name
CString   cstrFtpPassword = TEXT("pdmuguser");      //password
CInternetSession* m_pInternetSession = NULL;
CFtpConnection* m_pFtpConnection = NULL;


try
{
    m_pInternetSession = new CInternetSession();
    m_pFtpConnection = m_pInternetSession->GetFtpConnection(cstrFtpServer, 
                           cstrFtpUserName, cstrFtpPassword, 21);  //21 --- ftp port
}
catch (CInternetException* pEx)    //error:can not connect to specific ftp
{
    if (m_pInternetSession != NULL)
    {
        delete m_pInternetSession;
    }
    if (m_pFtpConnection != NULL)
    {
        delete m_pFtpConnection;
    }
   
    return;
}


//
//2. get current directory
//
CString cstrCurrDir;
flag = m_pFtpConnection->GetCurrentDirectory(cstrCurrDir);
if (!flag)  //get current directory error
{
}


//
//3. set current directory
//
CString cstrNewCurrDir = TEXT("//pdmpv/GOX/BACK_COVER/");
flag = m_pFtpConnection->SetCurrentDirectory(cstrNewCurrDir);
if (!flag)  //set current directory error
{
}


//
//4. download file from ftp
//
flag = m_pFtpConnection->GetFile(TEXT("CA110900_2ND_MD.ol"), 
                                 TEXT("D:\\123.ol"),
                                 TRUE);
if (!flag)  //download file fail
{
}


//
//5. upload file to ftp
//
flag = m_pFtpConnection->PutFile(TEXT("D:\\123.txt"), TEXT("456.txt"));
if (!flag)  //upload file fail
{
}


//
//6. rename file on ftp
//
flag = m_pFtpConnection->Rename(TEXT("456.txt"), TEXT("456_wy.txt"));
if (!flag)  //rename file fail
{
}


//
//7. remove file on ftp
//
flag = m_pFtpConnection->Remove(TEXT("456.txt"));
if (!flag)  //remove file fail
{
}


//
//8. create directory on ftp
//
flag = m_pFtpConnection->CreateDirectory(TEXT("WangYao"));
if (!flag)  //create directory on ftp fail
{
}


//
//9. remove directory on ftp
//Note: directory must be empty or will cause error
//
flag = m_pFtpConnection->RemoveDirectory(TEXT("WangYao"));
if (!flag)  //remove directory on ftp fail
{
}


//
//10. Do not forget to free resource
//
delete m_pInternetSession;
delete m_pFtpConnection;




//
//*********************************************************
//Ftp file finder
//*********************************************************
//
//
//1. 如上:connect to ftp
//


//
//2. 如上:set current directory
//


//
//3. find file(参考CFileFind)
//
CFtpFileFind fFinder(m_pFtpConnection);
BOOL bFind = fFinder.FindFile(TEXT("*.*"));
while (bFind)
{
    bFind = fFinder.FindNextFile();


    //当前文件夹及上层文件夹(名称分别为.和..)-----------------
    if (fFinder.IsDots()) 
    {
        continue;
    }


    //子文件夹---------------------------------------------
    if(fFinder.IsDirectory()) 
    {
        CString cstrDirName = fFinder.GetFileName();  //directory name
        CString cstrDirPath = fFinder.GetFilePath();  //directory path
        continue;
    }


    //文件-------------------------------------------------
    CString cstrFileName = fFinder.GetFileName();   //file name
    CString cstrFilePath = fFinder.GetFilePath();   //file path
}


fFinder.Close();

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MFC中,可以使用CFtpConnection类和CFtpFileFind类来上传、下载、删除文件夹。 1. 上传文件夹: 首先需要连接FTP服务器,然后使用CFtpFileFind类遍历文件夹中的所有文件,使用CFtpConnection类上传每个文件。 示例代码: ``` // 连接FTP服务器 CFtpConnection ftp; ftp.Connect(_T("ftp.example.com")); ftp.Login(_T("username"), _T("password")); ftp.SetCurrentDirectory(_T("/remote/folder")); // 遍历本地文件夹 CFileFind finder; CString strWildcard = _T("C:\\local\\folder\\*.*"); BOOL bWorking = finder.FindFile(strWildcard); while (bWorking) { bWorking = finder.FindNextFile(); // 如果是文件夹,则忽略 if (finder.IsDots() || finder.IsDirectory()) continue; // 上传文件 CString strRemoteFile = finder.GetFileName(); CString strLocalFile = finder.GetFilePath(); ftp.PutFile(strLocalFile, strRemoteFile); } // 断开连接 ftp.Close(); ``` 2. 下载文件夹: 同样需要连接FTP服务器,然后使用CFtpFileFind类遍历远程文件夹中的所有文件,使用CFtpConnection类下载每个文件。 示例代码: ``` // 连接FTP服务器 CFtpConnection ftp; ftp.Connect(_T("ftp.example.com")); ftp.Login(_T("username"), _T("password")); ftp.SetCurrentDirectory(_T("/remote/folder")); // 遍历远程文件夹 CFtpFileFind finder(&ftp); BOOL bWorking = finder.FindFile(_T("*.*")); while (bWorking) { bWorking = finder.FindNextFile(); // 如果是文件夹,则忽略 if (finder.IsDots() || finder.IsDirectory()) continue; // 下载文件 CString strRemoteFile = finder.GetFileName(); CString strLocalFile = _T("C:\\local\\folder\\") + strRemoteFile; ftp.GetFile(strRemoteFile, strLocalFile); } // 断开连接 ftp.Close(); ``` 3. 删除文件夹: 需要连接FTP服务器,然后使用CFtpFileFind类遍历远程文件夹中的所有文件,使用CFtpConnection类删除每个文件,最后使用CFtpConnection类删除空文件夹。 示例代码: ``` // 连接FTP服务器 CFtpConnection ftp; ftp.Connect(_T("ftp.example.com")); ftp.Login(_T("username"), _T("password")); ftp.SetCurrentDirectory(_T("/remote/folder")); // 遍历远程文件夹 CFtpFileFind finder(&ftp); BOOL bWorking = finder.FindFile(_T("*.*")); while (bWorking) { bWorking = finder.FindNextFile(); // 如果是文件夹,则忽略 if (finder.IsDots() || finder.IsDirectory()) continue; // 删除文件 CString strRemoteFile = finder.GetFileName(); ftp.Remove(strRemoteFile); } // 删除空文件夹 ftp.RemoveDirectory(_T("/remote/folder")); // 断开连接 ftp.Close(); ``` 需要注意的是,CFtpConnection类和CFtpFileFind类的使用需要包含头文件<afxinet.h>。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值