在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
resume.c
1 //采用CURLOPT_RESUME_FROM_LARGE 实现文件断点续传功能 2 #include <stdlib.h> 3 #include <stdio.h> 4 #include <sys/stat.h> 5 #include <curl/curl.h> 6 //这个函数为CURLOPT_HEADERFUNCTION 参数构造 7 /*从http 头部获取文件size*/ 8 size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb, void *stream) { 9 int r; 10 long len = 0; 11 /* _snscanf() is Win32 specific */ 12 // r = _snscanf(ptr, size * nmemb, "Content-Length: %ld\n", &len); 13 r = sscanf(ptr, "Content-Length: %ld\n", &len); 14 if (r) /* Microsoft: we don't read the specs */ 15 *((long *) stream) = len; 16 return size * nmemb; 17 } 18 /*保存下载文件*/ 19 size_t wirtefunc(void *ptr, size_t size, size_t nmemb, void *stream) 20 { 21 return fwrite(ptr, size, nmemb, stream); 22 } 23 /*读取上传文件*/ 24 size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream) 25 { 26 FILE *f = stream; 27 size_t n; 28 if (ferror(f)) 29 return CURL_READFUNC_ABORT; 30 n = fread(ptr, size, nmemb, f) * size; 31 return n; 32 } 33 //下载或者上传文件函数 34 int download(CURL *curlhandle, const char * remotepath, const char * localpath, 35 long timeout, long tries) 36 { 37 FILE *f; 38 curl_off_t local_file_len = -1 ; 39 long filesize =0 ; 40 CURLcode r = CURLE_GOT_NOTHING; 41 int c; 42 struct stat file_info; 43 int use_resume = 0; 44 /*得到本地文件大小*/ 45 //if(access(localpath,F_OK) ==0) 46 if(stat(localpath, &file_info) == 0) 47 { 48 local_file_len = file_info.st_size; 49 use_resume = 1; 50 } 51 //采用追加方式打开文件,便于实现文件断点续传工作 52 f = fopen(localpath, "ab+"); 53 if (f == NULL) { 54 perror(NULL); 55 return 0; 56 } 57 //curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L); 58 curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath); 59 curl_easy_setopt(curlhandle, CURLOPT_CONNECTTIMEOUT, 60 timeout); //设置连接超时,单位秒 61 //设置http 头部处理函数 62 curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, 63 getcontentlengthfunc); 64 curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &filesize); 65 //设置文件续传的位置给libcurl 66 curl_easy_setopt(curlhandle, CURLOPT_RESUME_FROM_LARGE, 67 use_resume?local_file_len:0); 68 curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, f); 69 curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, wirtefunc); 70 //curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc); 71 //curl_easy_setopt(curlhandle, CURLOPT_READDATA, f); 72 curl_easy_setopt(curlhandle, CURLOPT_NOPROGRESS, 1L); 73 curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L); 74 r = curl_easy_perform(curlhandle); 75 fclose(f); 76 if (r == CURLE_OK) 77 return 1; 78 else { 79 fprintf(stderr, "%s\n", curl_easy_strerror(r)); 80 return 0; 81 } 82 } 83 int main(int c, char **argv) { 84 CURL *curlhandle = NULL; //定义一个指针 85 curl_global_init(CURL_GLOBAL_ALL); //初始化libcurl库 86 curlhandle = curl_easy_init(); //获得指针 87 //download(curlhandle, "ftp://user:pass@host/path/file", "C:\\file", 0, 3); 88 download(curlhandle , "http://software.skyunion.cn/index.asp","/work/index.asp",1,3); 89 curl_easy_cleanup(curlhandle); 90 curl_global_cleanup(); 91 return 0; 92 }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论