libcurl 下载ftp服务器全部文件

原:http://blog.csdn.net/mp295345033/article/details/49487347

最近做的一个任务,项目的需求,需要下载ftp上的全部文件,没有上传,只有下载,利用libcurl库,现在只做了linux版本。

头文件:

[cpp]  view plain  copy
  1. #ifndef FTP_DOWNLOAD_H  
  2. #define FTP_DOWNLOAD_H  
  3. #include <iostream>  
  4. #include <functional>  
  5. using namespace std;  
  6.   
  7. typedef std::function<void(string &localpath,string &filename)> FileDownloadedCallBack;  
  8.   
  9. class FtpDownloadder  
  10.   
  11. {  
  12. public:  
  13.        FtpDownloadder(const char *ip,const char *port,const char *username,const char *password);  
  14.        ~FtpDownloadder();  
  15.        bool Download(FileDownloadedCallBack callBack);  
  16. private:  
  17.        string m_ip;  
  18.        string m_port;  
  19.        string m_username;  
  20.        string m_password;  
  21.        FileDownloadedCallBack m_callBack;  
  22.          
  23.        bool Getlist(string &remotpath,string &myfloderlist,string &localpath);//获得每一个ftp目录的文件列表  
  24.        bool Downloadfile(string &remotpath,string &filename,string &localpath);//下载文件  
  25.        void Getfirstchar(string &filename,char &fistchar);//对文件名的操作  
  26. };  
  27.   
  28.   
  29. #endif   

cpp文件:

[cpp]  view plain  copy
  1. #include "download.h"  
  2. #include <stdio.h>  
  3. #include <iostream>  
  4. #include <curl/curl.h>  
  5. #include <fstream>  
  6. #include <string.h>  
  7. #include <vector>  
  8. #include <sys/stat.h>    
  9. #include <sys/types.h>   
  10. using namespace std;  
  11.   
  12.  struct FtpFile  
  13.   {  
  14.       string local;  
  15.       FILE *stream;  
  16.   };  
  17. FtpDownloadder::FtpDownloadder(const char *ip,const char *port,const char *username,const char *password)  
  18. {  
  19.        m_ip = ip;  
  20.        m_port = port;  
  21.        m_username = username;  
  22.        m_password = password;  
  23. };  
  24. FtpDownloadder::~FtpDownloadder(){};  
  25.   
  26.    
  27. bool FtpDownloadder::Getlist(string &remotpath,string &myfloderlist,string &localpath)  
  28. {  
  29.   CURL *curl;  
  30.   CURLcode res;  
  31.   char fistchar;  
  32.   FILE *ftpfile;  
  33.   string filename;  
  34.   int i=0;  
  35.   char listbuf[200];  
  36.     /* local file name to store the file as */   
  37.   ftpfile = fopen((myfloderlist+".txt").c_str(), "wb"); /* b is binary, needed on win32 */   
  38.   curl = curl_easy_init();  
  39.     
  40.   if(curl) {  
  41.     /* Get a file listing from sunet */   
  42.     curl_easy_setopt(curl, CURLOPT_URL, remotpath.c_str());  
  43.     curl_easy_setopt(curl, CURLOPT_USERNAME, m_username.c_str());  
  44.     curl_easy_setopt(curl, CURLOPT_PASSWORD, m_password.c_str());  
  45.     curl_easy_setopt(curl, CURLOPT_WRITEDATA, ftpfile);  
  46.     res = curl_easy_perform(curl);  
  47.     /* Check for errors */   
  48.       
  49.     if(res != CURLE_OK)  
  50.       fprintf(stderr, "curl_easy_perform() failed: %s\n",  
  51.               curl_easy_strerror(res));  
  52.    
  53.     /* always cleanup */   
  54.     curl_easy_cleanup(curl);  
  55.   }  
  56.   fclose(ftpfile);   
  57.   string strFtpListFile = myfloderlist+".txt";  
  58.   ifstream infile(strFtpListFile.c_str());  
  59.   if(!infile.is_open())  
  60.   {  
  61.         cout << "can not open ftpList.txt" << endl;  
  62.         return false;  
  63.         //return -1;  
  64.   }  
  65.   while(getline(infile,filename))  
  66.   {  
  67.         Getfirstchar(filename,fistchar);  
  68.         string nextdir = remotpath + filename + "/";  
  69.         string localpath1 = localpath + "/" + filename;  
  70.         strcpy(listbuf,strFtpListFile.c_str());  
  71.         sprintf(listbuf,"%d",i);  
  72.         string myfloderlist(listbuf);  
  73.         string folderlist=myfloderlist+".txt";  
  74.         i++;  
  75.         if('d'==fistchar)  
  76.         {  
  77.               
  78.             int err=mkdir(localpath1.c_str(),S_IRWXU);  
  79.                 if(-1==err)  
  80.                 {  
  81.                     cout<<"error mkdir "<<endl;  
  82.                 }     
  83.              
  84.               
  85.             Getlist(nextdir,myfloderlist,localpath1);  
  86.                            
  87.         }  
  88.         else  
  89.         {  
  90.             string remotpath2=remotpath;  
  91.            if(Downloadfile(remotpath2,filename,localpath))  
  92.            {  
  93.                if (m_callBack!=NULL)  
  94.                m_callBack(localpath,filename);  
  95.            }  
  96.            else  
  97.            {  
  98.                cout << "download error";  
  99.                return false;  
  100.            }  
  101.             //vector<string> vec;  
  102.             //vec.pushback(localpath);  
  103.         }  
  104.   }  
  105.     /* close the local file */  
  106.     return true;  
  107.   
  108. }  
  109.   
  110. int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)  
  111. {  
  112.      struct FtpFile *out=(struct FtpFile *)stream;  
  113.       if(out && !out->stream)  
  114.           {     
  115.             /* open file for writing */   
  116.             out->stream=fopen((out->local).c_str(), "ab+");  
  117.             if(!out->stream)  
  118.             return -1; /* failure, can't open file to write */   
  119.           }  
  120.      return fwrite(buffer, size, nmemb, out->stream);  
  121. }  
  122.   
  123. bool FtpDownloadder::Downloadfile(string &remotpath,string &filename,string &localpath)  
  124. {  
  125.   CURL *curl;  
  126.   CURLcode res;   
  127.    
  128.     
  129.       
  130.   curl_global_init(CURL_GLOBAL_DEFAULT);  
  131.     
  132.     
  133.       // long getlocal ;  
  134.       //FILE *destFilePath;  
  135.          
  136.       int use_resume = 0;  
  137.       curl_off_t local_file_len = -1 ;   
  138.       struct stat file_info;    
  139.     
  140.       if(stat(localpath.c_str(), &file_info) == 0)    
  141.        {    
  142.           local_file_len = file_info.st_size;     
  143.           use_resume = 1;    
  144.        }    
  145.          
  146.        struct FtpFile ftpfile=  
  147.         {  
  148.          localpath+"/"+filename, /* name to store the file as if successful */   
  149.          NULL  
  150.         };  
  151.        curl = curl_easy_init();  
  152.        if(curl)   
  153.   
  154.        {  
  155.         curl_easy_setopt(curl, CURLOPT_URL, (remotpath+filename).c_str());          
  156.         curl_easy_setopt(curl, CURLOPT_USERNAME, m_username.c_str());  
  157.         curl_easy_setopt(curl, CURLOPT_PASSWORD, m_password.c_str());   
  158.         //curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1);  
  159.         curl_easy_setopt(curl, CURLOPT_RESUME_FROM_LARGE, use_resume?local_file_len:0);  
  160.         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);  
  161.         curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);  
  162.         curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);  
  163.         res = curl_easy_perform(curl);    
  164.           curl_easy_cleanup(curl);  
  165.        }  
  166.           
  167.    curl_global_cleanup();  
  168.    return true;  
  169. }  
  170.   
  171. void FtpDownloadder::Getfirstchar(string &filename,char &fistchar)  
  172. {  
  173.     vector<string>v1(sizeof(filename), filename);  
  174.     vector<string>::iterator it = v1.begin();  
  175.     int err = it->find_last_of(' ');  
  176.     string filename1;  
  177.     filename1.assign(it->begin()+err+1, it->end());  
  178.     const char *p = filename.c_str();  
  179.     filename=filename1;  
  180.     fistchar = *p;  
  181. }  
  182.   
  183.   
  184.   
  185. bool FtpDownloadder::Download(FileDownloadedCallBack callBack)  
  186. {  
  187.     m_callBack=callBack;  
  188.     string remotpath="ftp://"+m_ip+":"+m_port+"/";  
  189.     string list="mylist";  
  190.     string localpath="/home/FtpServer/";  
  191.     int err=mkdir("/home/FtpServer",S_IRWXU);  
  192.                 if(-1==err)  
  193.                 {  
  194.                     return false;  
  195.                 }     
  196.     if(Getlist(remotpath,list,localpath))  
  197.     {  
  198.         return false;  
  199.     }  
  200.     return true;  
  201. }  
测试:

[cpp]  view plain  copy
  1. #include "download.h"  
  2. #include <iostream>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     FileDownloadedCallBack callBack;  
  7.     FtpDownloadder *dl=new FtpDownloadder("192.168.1.1","21","zheng","yun");  
  8.     if(dl->Download(callBack))  
  9.     {  
  10.         cout<<"download over";  
  11.     }  
  12.     delete dl;  
  13.     return 0;  
  14. }  

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值