(四)操作Libcurl下载文件,处置无信号中缀,下载中掉电复原后断点

各位亲 有时间可以去看看我的  “金骏家居淘宝店” http://jinjun1688.taobao.com/shop/view_shop.htm?tracelog=twddp 买时说明在我的博客看到有优惠哦 还有意外礼品赠送  真正的程序员淘宝店


//全局变量

bool resumeDownload = false;             //能否需求下载的标记位

long downloadFileLenth = 0;                 //需求下载的总大小,长途文件的大小

/* 失去当地文件大小的函数,若不是续传则前往0, 不然前往指定门路地址的文件大小 */

long getLocalFileLenth(const char* localPath){

      if (!resumeDownload){

             return 0;

}

return fs_open(localPath).fs_size();

}

/* 失去长途文件的大小,要下载文件大小 */

long getDownloadFileLenth(const char *url){

long downloadFileLenth = 0;

CURL *handle = curl_easy_init();

curl_easy_setopt(handle, CURLOPT_URL, url);

curl_easy_setopt(handle, CURLOPT_HEADER, 1);    //只要求header头

curl_easy_setopt(handle, CURLOPT_NOBODY, 1);    //不需求body

if (curl_easy_perform(handle) == CURLE_OK) {

curl_easy_getinfo(handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &downloadFileLenth);

} else {

downloadFileLenth = -1;

}

return downloadFileLenth;

}

/* scomoDownload回调的计算进度条的函数 */

void getProgressValue(const char* localSize, double dt, double dn, double ult, double uln){

     double showTotal, showNow;

showTotal = downloadFileLenth;

int localNow = atoi (localSize.c_str());

showNow = localNow + dn;

showProgressBar(showTotal, showNow);

}

/* 间接停止下载的函数 */

public CurlCode scomoDownload(long timeout) {

      long localFileLenth = getLocalFileLenth();

      const char *localFileLenthStr;

      sprint(localFileLenthStr, %ld, localFileLenth);

curl_easy_setopt(handle, CURLOPT_URL, mUrl);

curl_easy_setopt(handle, CURLOPT_HEADER, 0);

curl_easy_setopt(handle, CURLOPT_TIMEOUT, timeout);

curl_easy_setopt(handle, CURLOPT_CONNECTIONTIMEOUT, 0);

curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, &writeDataCallback);

curl_easy_setopt(handle, CURLOPT_WRITEDATA, this);

curl_easy_setopt(handle, CURLOPT_RESUME_FROM_LARGE, localFileLenth);

curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0);

curl_easy_setopt(handle, CURLOPT_ PROGRESSFUNCTION, getProgressValue);

curl_easy_setopt(handle, CURLOPT_PROGRESSDATA, localFileLenthStr);

if (curl_easy_perform) {

       resumeDownload = true;

       return DS_FAILED;

} else {

       resumeDownload = false;

       return DS_FINISHED;

}

}

/* downloadControl函数用来管制整个下载进程的节拍,管制下载的次数, 每次期待的工夫等 */

public void downloadControler(){

      downloadFileLenth = getDownloadFileLenth();                 //下载前失去要下载的文件大小赋值给全局变量

      int times = 605;                                      //600次*50ms=5分钟,以此确保5分钟内的重试次数,而5次是反常下载的中缀次数,意思即是5次内能反常完成下载.

      int count = 0;

      int timeout = 30;

      DSTATUS dstatus = DS_FAILED;

      while (count++ < times){

             status = scomoDownload(timeout);

             if (dstatus == DS_FINISHED){

                    break;

             }


http://www.nnwnn.com/a/shoujibiancheng/2012/0920/17756.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值