- #include"curl/curl.h"
- #pragma comment(lib,"libcurl_imp.lib")
- bool HelloWorld::init()
- {
- CCLayer::init();
- CURL *pCurl;
- CURLcode nResCode;
- pCurl = curl_easy_init();//初始化CURL取得初始化成功后的CURL指针
- if (pCurl != NULL)
- {
- FILE *pFile;
- pFile = fopen("XXX.jpg", "wb");
- curl_easy_setopt(pCurl,CURLOPT_URL,"http://ww1.sinaimg.cn/large/7f32a2c8jw1e8lyw03zpbj20c8d1ynpd.jpg");
- if(pFile != NULL)
- curl_easy_setopt(pCurl,CURLOPT_FILE,pFile); //指定写入的文件指针
- curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, pWriteCallback);//设置写数据的回调函数
- curl_easy_setopt(pCurl, CURLOPT_VERBOSE, TRUE); //让CURL报告每一件意外的事情
- curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 60); //设置超时时间
- curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS,0L);
- curl_easy_setopt(pCurl, CURLOPT_PROGRESSFUNCTION, HelloWorld::DownProgresss);//指定显示进度的回调函数
- nResCode = curl_easy_perform(pCurl);//执行上面设定的动作并返回状态码
- curl_easy_cleanup(pCurl); //释放相关资源
- fclose(pFile);
- nResCode == CURLE_OK ? CCLOG("DownLoad Success") : CCLOG("CODE: %d",nResCode);
- }
- return true;
- }
- size_t HelloWorld::pWriteCallback(void *pData, size_t n, size_t nDataSize, FILE *stream)
- {
- size_t nWritten = fwrite(pData,n,nDataSize,(FILE *)stream);
- return nWritten;
- }
- int HelloWorld::DownProgresss(void* clientp,double fDownLoadTotal,double fDownLoaded,double fUpTotal,double fUpLoaded)
- {
- if (fDownLoaded >= 0 && fDownLoadTotal != 0)
- CCLOG("%6.2f%%\n", 100*fDownLoaded/fDownLoadTotal);//下载百分比
- return 0;
- }
OK最后附上本工程的下载地址需要的可以去下载研究研究http://t.cn/8Fp5zUD
本人郑重声明如下 一、本文来自CSDN博客,传送门:http://BlOG.CSDN.NET/yirancpp 二、All Rights Reserved. 任何个人或网站转载本文时不得移除本声明. 三、不得对文章进行修改,除非明确说明.同时欢迎大家评论转载和分享.