include
include
pragma comment(lib,”libcurl_a_debug.lib”)
size_t getcontentlengthfunc(void* ptr,size_t size,size_t nmemb,void *stream)
{
int r=0;
long len=0;
r=sscanf((char*)ptr,"Content_Length:%ld\n",&len);
if(r)
*((long*)stream)=len;
return size*nmemb;
}
size_t writefunc(void* ptr,size_t size,size_t nmemb,void* stream)
{
FILE fp=(FILE)stream;
size_t nWrite=fwrite(ptr,size,nmemb,fp);
return nWrite;
}
int ProgressCallback(double dltotal,double dlnow,double ultotal,double ulnow)
{
if(dltotal>0.1&&dltotal<0.1)
return 0;
int nPos=(int)((dlnow/dltotal)*100);
std::cout<<"dltotal: "<<(long)dltotal<<" ---dlnow: "<<(long)dlnow<<std::endl;
return 0;
}
int download(CURL curlhandle,const char remotepath,const char* localpath,long timeout=0)
{
FILE *f=NULL;
curl_off_t local_file_len=-1;
long filesize=0;
CURLcode r=CURLE_GOT_NOTHING;
struct stat file_info;
int use_resume=0;
char szRequest[MAX_PATH]={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;
}
r=curl_easy_setopt(curlhandle,CURLOPT_URL,remotepath);
//连接超时设置
r=curl_easy_setopt(curlhandle,CURLOPT_CONNECTTIMEOUT,timeout);
//设置头处理函数
r=curl_easy_setopt(curlhandle,CURLOPT_HEADERFUNCTION,getcontentlengthfunc);
r=curl_easy_setopt(curlhandle,CURLOPT_HEADERDATA,&filesize);
//设置断点续传
r=curl_easy_setopt(curlhandle,CURLOPT_RESUME_FROM,use_resume?local_file_len:0);
r=curl_easy_setopt(curlhandle,CURLOPT_WRITEFUNCTION,writefunc);
r=curl_easy_setopt(curlhandle,CURLOPT_WRITEDATA,f);
r=curl_easy_setopt(curlhandle,CURLOPT_NOPROGRESS,0L);
curl_easy_setopt(curlhandle,CURLOPT_PROGRESSFUNCTION,ProgressCallback);
r=curl_easy_setopt(curlhandle,CURLOPT_VERBOSE,1L);
r=curl_easy_perform(curlhandle);
fclose(f);
if(r==CURLE_OK)
return 1;
else if(r==CURLE_FTP_COULDNT_USE_REST)
{
//如果服务器不支持断点续传则做相应的处理
bool bIsOk = DeleteFileA(localpath);
if(bIsOk==false)
{
DeleteFileA(localpath);
}
f=fopen(localpath,"ab+");
if(f==NULL)
{
perror(NULL);
return 0;
}
curl_easy_cleanup(curlhandle);
curlhandle=curl_easy_init();
curl_easy_setopt(curlhandle,CURLOPT_URL,remotepath);
//连接超时设置
curl_easy_setopt(curlhandle,CURLOPT_CONNECTTIMEOUT,timeout);
//设置头处理函数
curl_easy_setopt(curlhandle,CURLOPT_HEADERFUNCTION,getcontentlengthfunc);
curl_easy_setopt(curlhandle,CURLOPT_HEADERDATA,&filesize);
//设置断点续传
curl_easy_setopt(curlhandle,CURLOPT_RESUME_FROM,use_resume?local_file_len:0);
curl_easy_setopt(curlhandle,CURLOPT_WRITEFUNCTION,writefunc);
curl_easy_setopt(curlhandle,CURLOPT_WRITEDATA,f);
curl_easy_setopt(curlhandle,CURLOPT_NOPROGRESS,0L);
curl_easy_setopt(curlhandle,CURLOPT_PROGRESSFUNCTION,ProgressCallback);
curl_easy_setopt(curlhandle,CURLOPT_VERBOSE,1L);
r=curl_easy_perform(curlhandle);
fclose(f);
}
else
{
fprintf(stderr,"%s\n",curl_easy_strerror(r));
}
return 0;
}
int main()
{
CURL *curldwn=NULL;
curl_global_init(CURL_GLOBAL_ALL);
curldwn=curl_easy_init();
//1-超时时间
//download(curldwn,"http://192.168.0.17:8080/%E6%B7%B1%E5%85%A5%E5%BA%94%E7%94%A8C++11%EF%BC%9A%E4%BB%A3%E7%A0%81%E4%BC%98%E5%8C%96%E4%B8%8E%E5%B7%A5%E7%A8%8B%E7%BA%A7%E5%BA%94%E7%94%A8%20,%E7%A5%81%E5%AE%87%E7%BC%96%E8%91%97,%20P414.pdf","C:\\22.pdf",1);
download(curldwn,"ftp://Archer:520503@127.0.0.1/1.pdf","C:\\21.pdf",1);
curl_easy_cleanup(curldwn);
curl_global_cleanup();
getchar();
return 0;
}