zoukankan      html  css  js  c++  java
  • C++ Operate FTP

    //
     //*********************************************************
     //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();


  • 相关阅读:
    “连城决”——预示2008年手机营销体式格式新打破
    都会演出连城诀—诺基亚N78决战入手入手了!
    Lyx:阔别单调的 LaTeX 节制命令
    [转载]Oracle 11g R1下的自动内存经管(2)
    假造化手艺是决胜企业IT化的关头
    请各位博友对HyperV的运用终了指摘
    有199元的Office,还要用盗版吗?
    十一回南通,当晚和同学去小石桥附近的网吧
    Windows 消息
    WinAPI: 钩子回调函数之 MsgFilterProc
  • 原文地址:https://www.cnblogs.com/yefengmeander/p/2887557.html
Copyright © 2011-2022 走看看