付费下载
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、linux c 基于 libcurl 库的 ftp 上传和下载代码昨天考虑好了 IM 传送图片的方式,用的是 FTP 做缓存来传递图片,以减少聊天服务器的压力,用的是 libcurl 库,自己载了一个 Server-U 服务器, 下面是测试程序, 上传和下载都跑通了,剩下的就是在程序中写逻辑来实现具体的功能了。传代码:cpp view plain copy print?/ftp 上传实例/ 服务器地址:85 账号:spider 密码:spider/ 在服务器路径 a上建立一个 a.txt ,本地文件是 test.txt/ 命令行参数85 spider
2、 spider a a.txt D:/test.txt /#include<stdlib.h>/#include <stdio.h>/#include<curl/curl.h>/#include <string.h>/ /intdebugFun(CURL* curl, curl_infotype type, char* str, size_t len,void* stream)/ / 只打印 CURLINFO_TEXT类型的信息/ if(type = CURLINFO_TEXT)/f
3、write(str, 1, len, (FILE*)stream);/ /return 0;/ / /int main(int argc, char* argv)/ CURL* curl;/ CURLcode res; / FILEchar errorBufCURL_ERROR_SIZE;*sendFile, *debugFile;/ char ftpurl256 + 1;/char usrpasswd64 + 1;/ / curl_slist *slist=NULL;/ / if(argc != 7)/ /printf("Usage:nt./ftp ip username pass
4、word ftpPathdestFileName srcFile"); /return -1;/ /将相关的调试信息打印到 dubugFile.txt匚=f/if(NULL = (debugFile = fopen("debugFile.txt", "a+")/return -1;/ /打开 ftp 上传的源文件/if(NULL = (sendFile = fopen(argv6, "r")/fclose(debugFile); /return -1; /获取需要发送文件的大小/ fseek(sendFile, 0,SEEK
5、_END);/int sendSize = ftell(sendFile);/if(sendSize < 0)/fclose(debugFile);/fclose(sendFile);/return -1; /fseek(sendFile, 0L,SEEK_SET); /if(res =curl_global_init(CURL_GLOBAL_ALL) != 0)/ /fclose(debugFile); /fclose(sendFile); /return -1;/if(curl = curl_easy_init() = NULL)/fclose(debugFile); /fc
6、lose(sendFile);/curl_global_cleanup(); /return -1;/ / if(argv4strlen(argv4) - 1 != '/')/ else /sprintf(ftpurl, "ftp:/%s/%s/%s", argv1, argv4, argv5);sprintf(ftpurl, "ftp:/%s/%s%s",argv1, argv4, argv5);/ curl_easy_setopt(curl,CURLOPT_URL, ftpurl);/设置 ftp 上传 url, 组成如的 URL /
7、45/root/subdir/curl/testftp.txt/ /sprintf(usrpasswd, "%s:%s", argv2, argv3);/curl_easy_setopt(curl, CURLOPT_USERPWD, usrpasswd);/ / curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);/ curl_easy_setopt(curl, CURLOPT_DEBUGDATA,debugFile); / / curl_easy_setopt(curl,CURLOPT_READDATA,
8、 sendFile);/curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);/curl_easy_setopt(curl, CURLOPT_INFILESIZE, sendSize);/ curl_easy_setopt(curl,CURLOPT_FTP_CREATE_MISSING_DIRS, 1);/curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,debugFun); / res = curl_easy_perform(curl);/if(0 != res) / /fclose(sendFile);/fclose(d
9、ebugFile);/curl_easy_cleanup(curl);/curl_global_cleanup(); /return -1;/ / / curl_easy_cleanup(curl);/fclose(sendFile); / fclose(debugFile);/curl_global_cleanup(); / getchar(); /return 0;/载代码如下: cpp view plain copy print?/ftp载实例#include <stdio.h> #include <curl/curl.h>#inc
10、lude <curl/types.h> #include <curl/easy.h>struct FtpFile/定义一个结构为了传递给 my_fwrite 函数 .可用 curl_easy_setopt 的 CURLOPT_WRITEDATA 选项传递char *filename;FILE *stream; ;int my_fwrite(void *buffer, size_t size, size_t nmemb, void*stream) struct FtpFile *out=(struct FtpFile*)stream; /
11、stream 指针其实就是指向 struct FtpFile ftpfile 的if(out && !out->stream)out->stream=fopen(out->filename, "wb"); / 没有这个流的话就创建一个名字是out->filename.if(!out->stream)return -1;return fwrite(buffer, size,nmemb, out->stream);int main(int argc, char*a
12、rgv) CURL *curl;ftpfile="D:/Success.txt",NULL; / 初始化一个 FtpFile 结构curl_global_init(CURL_GLOBAL_DEFAULT);curl = curl_easy_init();if(curl)curl_easy_setopt(curl,CURLOPT_URL,"85/a/a.txt");curl_easy_setopt(curl, CURLOPT_USERPWD,"spider:spider");curl_easy_setop
13、t(curl, CURLOPT_WRITEFUNCTION,my_fwrite);curl_easy_setopt(curl,CURLOPT_WRITEDATA, &ftpfile); / 给相关函数的第四 个参数传递一个结构体的指针curl_easy_setopt(curl, CURLOPT_VERBOSE,TRUE);/CURLOPT_VERBOSE 这个选项很常用用来在屏幕显示对服务器相关操作返回的信息res = curl_easy_perform(curl);curl_easy_cleanup(curl);if(CURLE_OK != res)fprintf(stder
14、r, "curl told us %dn", res);if(ftpfile.stream)fclose(ftpfile.stream);curl_global_cleanup();return 0; 最后记录一个比较好用的:cpp view plain copy#include <stdio.h>print?#include <stdlib.h>CURLE_GOT_NOTHING;int c;#include <curl/curl.h> #include <sys/stat.
15、h> /* parse headers for Content-Length */ size_tgetcontentlengthfunc(void *ptr, size_t size, size_t nmemb, void*stream) int r;long len = 0;/*_snscanf() is Win32 specific */r = _snscanf(ptr, size *nmemb, "Content-Length: %ldn", &len);sscanf(const char*)ptr, "Content-Leng
16、th: %ldn", &len);if (r) /* Microsoft: we don't read the specs */*(long*) stream) = len;return size * nmemb; /* discarddownloaded data */ size_t discardfunc(void *ptr, size_t size,size_t nmemb, void *stream) return size *nmemb; /write data to upload size_t writefunc(void *ptr,size_t
17、size, size_t nmemb, void *stream) returnfwrite(ptr, size, nmemb, (FILE*)stream); /* read data toupload */ size_t readfunc(void *ptr, size_t size, size_t nmemb,void *stream) FILE *f = (FILE*)stream;size_tn;if (ferror(f)returnCURL_READFUNC_ABORT;n = fread(ptr, size,/* are we resuming? */if (c) /* yes
18、*/nmemb, f) * size;return n; int upload(CURL*curlhandle, const char * remotepath, const char * localpath,long timeout, long tries) FILE *f;longfopen(localpath, "rb");if (f = NULL)perror(NULL);return 0;curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);curl_easy_setopt(curlhandle, CURLOPT_URL
19、, remotepath);curl_easy_setopt(curlhandle, CURLOPT_USERPWD,"spider:spider");if (timeout)curl_easy_setopt(curlhandle,CURLOPT_FTP_RESPONSE_TIMEOUT, timeout);curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION,getcontentlengthfunc);curl_easy_setopt(curlhandle,CURLOPT_HEADERDATA, &uplo
20、aded_len);curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION,discardfunc);curl_easy_setopt(curlhandle,CURLOPT_READFUNCTION, readfunc);curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-"); /*disable passive mode */curl_easy_setopt(curlhandle,C
21、URLOPT_FTP_CREATE_MISSING_DIRS, 1L);curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);for (c = 0; (r != CURLE_OK) && (c < tries); c+)return 0; / 下载 int download(CURL/*will issue a SIZEto retrieve the result is* to parse the returnedContent-Length header. Thus,getcontentlengthf
22、unc(). We need discardfunc() above* because HEADER will dump the headers to stdout* without it.*/curl_easy_setopt(curlhandle, CURLOPT_NOBODY , 1L);curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);r = curl_easy_perform(curlhandle);if (r !=CURLE_OK)continue;curl_easy_setopt(curlhandle, CURLOPT_NOBODY
23、, 0L);curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0L);fseek(f, uploaded_len, SEEK_SET);curl_easy_setopt(curlhandle, CURLOPT_APPEND,1L);else /* no */curl_easy_setopt(curlhandle, CURLOPT_APPEND,0L);curl_easy_perform(curlhandle);fclose(f);if (r = CURLE_OK)return 1;else/* determine the length of the fi
24、le already written */ * With NOBODY and NOHEADER, libcurl* command, but the only wayfprintf(stderr, "%sn", curl_easy_strerror(r);*curlhandle, const char * remotepath, const char * localpath,long timeout, long tries) FILE *f;curl_off_tlocal_file_len = -1 ;long filesize =0 ;CURLcode r= CURLE
25、_GOT_NOTHING;struct stat file_info;int use_resume = 0;/获取本地文件大小信息if(stat(localpath, &file_info) = 0)local_file_len = file_info.st_size;use_resume = 1;/追加方式打开文件,实现断点续传f = fopen(localpath, "ab+");if (f = NULL)perror(NULL);return 0;curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath)
26、;curl_easy_setopt(curlhandle, CURLOPT_USERPWD, curl_easy_setopt(curlhandle,"spider:spider");/连接超时设置curl_easy_setopt(curlhandle,CURLOPT_CONNECTTIMEOUT, timeout);/设置头处理函数curl_easy_setopt(curlhandle,CURLOPT_HEADERFUNCTION, getcontentlengthfunc);curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025江西师范高等专科学校工作人员招聘考试试题
- 2025江苏省海安中等专业学校工作人员招聘考试试题
- 太阳能光伏安装专项施工方案
- 圆管涵首件施工指导书
- 卵石水系专项施工方案
- 2026年生物传感器疾病早期检测报告
- 文言文机器翻译系统在初中文言文音韵学教学中的应用课题报告教学研究课题报告
- 初中历史文物修复中电磁学原理的金属文物修复课题报告教学研究课题报告
- 董事会成员聘任合同书合同三篇
- 2026年工程承包合同二篇
- 2026年江苏南京市高三二模高考物理试卷试题(含答案详解)
- 第13课 每个人都有梦想 课件(内嵌视频)2025-2026学年道德与法治二年级下册统编版
- 2026四川省成都广定发展集团有限公司招聘3人备考题库(含答案详解)
- 2026四川成都市公共交通集团有限公司招聘投资管理专员岗位备考题库附答案详解(b卷)
- 金属材料的基础知识
- 煤矿在用主通风机系统安全检测检验规范
- GB/T 19001-2016质量管理体系要求
- MCGS 项目7:组态应用实例
- GA/T 1494-2018路面结冰监测系统通用技术条件
- FZ/T 52039-2014再生聚苯硫醚短纤维
- 徐霞客人物介绍分析课件
评论
0/150
提交评论