C++ 关于断点续传的方法,使用CURL技术

来源:互联网 发布:cad线切割编程软件 编辑:程序博客网 时间:2024/05/25 16:38

STEP1

CURL *curlhandle = NULL;// curl_global_init(CURL_GLOBAL_ALL);curlhandle = curl_easy_init();bool flag = download(curlhandle,myvar->url.c_str(),myvar->path.c_str(),myvar->proxy_address.c_str(),myvar->proxy_port.c_str(),myvar->username.c_str(),myvar->password.c_str(),1,3);curl_easy_cleanup(curlhandle);curl_global_cleanup();


STEP2

int ABCD::download(CURL *curlhandle,  const char * remotepath, const char * localpath, const char* proxy_address,   const char* proxy_port,   const char* username,   const char* password,           long timeout, long tries){       curl_off_t local_file_len = -1 ;       long filesize =0 ;              CURLcode res = CURLE_GOT_NOTHING;       int c;  struct stat file_info;  int use_resume = 0;  /* 得到本地文件大小 */  //if(access(localpath,F_OK) ==0)      if(stat(localpath, &file_info) == 0)      {        local_file_len =  file_info.st_size;        use_resume  = 1;      }  //采用追加方式打开文件,便于实现文件断点续传工作       fp = fopen(localpath, "ab+");        if (fp == NULL)    {              perror(NULL);              return 0;       }        curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);   //setting proxy network ++++++std::string str_proxy="";str_proxy = proxy_address;str_proxy +=":";str_proxy += proxy_port;        curl_easy_setopt(curlhandle, CURLOPT_PROXY, str_proxy.c_str());  //test OKstd::string str_user_pwd="";str_user_pwd = username;str_user_pwd +=":";str_user_pwd += password;curl_easy_setopt(curlhandle, CURLOPT_PROXYUSERPWD, str_user_pwd.c_str());        //----------------------------------------------------------------------       curl_easy_setopt(curlhandle, CURLOPT_CONNECTTIMEOUT, timeout);  // 设置连接超时,单位秒       //设置http 头部处理函数       curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);       curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &filesize);       // 设置文件续传的位置给libcurl       curl_easy_setopt(curlhandle, CURLOPT_RESUME_FROM_LARGE, use_resume?local_file_len:0);        curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, fp);       curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curlhandle, CURLOPT_NOPROGRESS, 1L);curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);curl_easy_setopt(curlhandle, CURLOPT_NOSIGNAL, 1);curl_easy_setopt(curlhandle, CURLOPT_FOLLOWLOCATION, 1L);curl_easy_setopt(curlhandle, CURLOPT_SSL_VERIFYPEER, false);curl_easy_setopt(curlhandle, CURLOPT_SSL_VERIFYHOST, false);curl_easy_setopt(curlhandle, CURLOPT_NOPROGRESS, 0);curl_easy_setopt(curlhandle, CURLOPT_PROGRESSFUNCTION, my_progress_func);curl_easy_setopt(curlhandle, CURLOPT_PROGRESSDATA, iPer);       res = curl_easy_perform(curlhandle);       fclose(fp);        if (res == CURLE_OK)   {long http_code = 0;res = curl_easy_getinfo(curlhandle, CURLINFO_RESPONSE_CODE, &http_code);if(CURLE_OK == res && 200 == http_code){res = CURLE_OK;return 1;}else{res = CURL_LAST;//delete file , because the file is not existed!remove(localpath);return 0; // is failed! not find the download file.}   }       else    {           fprintf(stderr, "%s\n", curl_easy_strerror(res));   g_i_connect_network = 6;           return 0;       }}

STEP3

关于多线程如何传递多个值,请参考上一篇博客。

STEP4

需要定义一个结构体为传多value

   static FILE *fp;static int iPer; //Define sturct typedef struct DOWNLOAD_PARAMETER{int index;std::string url; }Download_Parametar;


TEST OK!

0 0
原创粉丝点击