libCurl上传下载代码一个好用的

[cpp]   view plain copy print ?
  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5.   
  6. size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb, void *stream)   
  7. {  
  8.     int r;  
  9.     long len = 0;  
  10.       
  11.     //r = _snscanf(ptr, size * nmemb, "Content-Length: %ld\n", &len);  
  12.     r = sscanf((const char*)ptr, "Content-Length: %ld\n", &len);  
  13.     if (r)   
  14.         *((long *) stream) = len;  
  15.     return size * nmemb;  
  16. }  
  17.   
  18. size_t discardfunc(void *ptr, size_t size, size_t nmemb, void *stream)   
  19. {  
  20.     return size * nmemb;  
  21. }  
  22. //write data to upload  
  23. size_t writefunc(void *ptr, size_t size, size_t nmemb, void *stream)  
  24. {  
  25.     return fwrite(ptr, size, nmemb, (FILE*)stream);  
  26. }  
  27.   
  28. size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream)  
  29. {  
  30.     FILE *f = (FILE*)stream;  
  31.     size_t n;  
  32.     if (ferror(f))  
  33.         return CURL_READFUNC_ABORT;  
  34.     n = fread(ptr, size, nmemb, f) * size;  
  35.     return n;  
  36. }  
  37. int upload(CURL *curlhandle, const char * remotepath, const char * localpath, long timeout, long tries)  
  38. {  
  39.     FILE *f;  
  40.     long uploaded_len = 0;  
  41.     CURLcode r = CURLE_GOT_NOTHING;  
  42.     int c;  
  43.     f = fopen(localpath, "rb");  
  44.     if (f == NULL) {  
  45.         perror(NULL);  
  46.         return 0;  
  47.     }  
  48.     curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);  
  49.     curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);  
  50.     curl_easy_setopt(curlhandle, CURLOPT_USERPWD, "spider:spider");     
  51.     if (timeout)  
  52.         curl_easy_setopt(curlhandle, CURLOPT_FTP_RESPONSE_TIMEOUT, timeout);  
  53.     curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);  
  54.     curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &uploaded_len);  
  55.     curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, discardfunc);  
  56.     curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);  
  57.     curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);  
  58.     curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-");   
  59.     curl_easy_setopt(curlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);  
  60.     curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);  
  61.     for (c = 0; (r != CURLE_OK) && (c < tries); c++) {  
  62.           
  63.         if (c) {   
  64.               
  65.               
  66.             curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1L);  
  67.             curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);  
  68.             r = curl_easy_perform(curlhandle);  
  69.             if (r != CURLE_OK)  
  70.                 continue;  
  71.             curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 0L);  
  72.             curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0L);  
  73.             fseek(f, uploaded_len, SEEK_SET);  
  74.             curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);  
  75.         }  
  76.         else {   
  77.             curl_easy_setopt(curlhandle, CURLOPT_APPEND, 0L);  
  78.         }  
  79.         r = curl_easy_perform(curlhandle);  
  80.     }  
  81.     fclose(f);  
  82.     if (r == CURLE_OK)  
  83.         return 1;  
  84.     else {  
  85.         fprintf(stderr, "%s\n", curl_easy_strerror(r));  
  86.         return 0;  
  87.     }  
  88. }  
  89. // 下载  
  90. int download(CURL *curlhandle, const char * remotepath, const char * localpath, long timeout, long tries)  
  91. {  
  92.     FILE *f;  
  93.     curl_off_t local_file_len = -1 ;  
  94.     long filesize =0 ;  
  95.     CURLcode r = CURLE_GOT_NOTHING;  
  96.     struct stat file_info;  
  97.     int use_resume = 0;  
  98.     //获取本地文件大小信息  
  99.     if(stat(localpath, &file_info) == 0)  
  100.     {  
  101.         local_file_len = file_info.st_size;   
  102.         use_resume = 1;  
  103.     }  
  104.     //追加方式打开文件,实现断点续传  
  105.     f = fopen(localpath, "ab+");  
  106.     if (f == NULL) {  
  107.         perror(NULL);  
  108.         return 0;  
  109.     }  
  110.     curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);  
  111.     curl_easy_setopt(curlhandle, CURLOPT_USERPWD, "spider:spider");     
  112.     //连接超时设置  
  113.     curl_easy_setopt(curlhandle, CURLOPT_CONNECTTIMEOUT, timeout);  
  114.     //设置头处理函数  
  115.     curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);  
  116.     curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &filesize);  
  117.     // 设置断点续传  
  118.     curl_easy_setopt(curlhandle, CURLOPT_RESUME_FROM_LARGE, use_resume?local_file_len:0);  
  119.     curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, writefunc);  
  120.     curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, f);  
  121.     curl_easy_setopt(curlhandle, CURLOPT_NOPROGRESS, 1L);  
  122.     curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);  
  123.     r = curl_easy_perform(curlhandle);  
  124.     fclose(f);  
  125.     if (r == CURLE_OK)  
  126.         return 1;  
  127.     else {  
  128.         fprintf(stderr, "%s\n", curl_easy_strerror(r));  
  129.         return 0;  
  130.     }  
  131. }  
  132. int main(int c, char **argv)   
  133. {  
  134.     CURL *curlhandle = NULL;  
  135.     CURL *curldwn = NULL;  
  136.     curl_global_init(CURL_GLOBAL_ALL);  
  137.     curlhandle = curl_easy_init();  
  138.     curldwn = curl_easy_init();  
  139.     upload(curlhandle, "ftp://192.168.0.185/a/success""D:/abc.jpg", 1, 3);  
  140.     download(curldwn, "ftp://192.168.0.185/a/success""D:/abc1.jpg", 1, 3);  
  141.     curl_easy_cleanup(curlhandle);  
  142.     curl_easy_cleanup(curldwn);  
  143.     curl_global_cleanup();  
  144.     return 0;  
  145. }  
libCurl上传下载代码一个好用的
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值