各位亲 有时间可以去看看我的 “金骏家居淘宝店” 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