利用curl实现下载功能

直接上代码(可直接用,注意参数):

#include <curl/curl.h>
#include <sys/stat.h>
#include "http_upgrade.h"


int download_Progress = 0;
int http_progress = 0;
static int ProgressCallback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)  
{  
    if ( dltotal > -0.1 && dltotal < 0.1 )  
       return 0;  
    int nPos = (int) ( (dlnow/dltotal)*100 );

    //通知进度条更新下载进度  
    
	download_Progress = ((1<<16) | (nPos >> 1));

	http_progress = nPos;
	printf("Progress=========%d \n",nPos);


    return 0;  
}
int pf_http_download(char* remote, char* file, char** received_data)
{
    CURL *curl;
    CURLcode res;
    FILE* fp = NULL;
    long code = 0;
    struct stat buf;
    int m_iTimeout = 10;
//	curl_global_init(CURL_GLOBAL_DEFAULT);  
	double downloadFileLenth = -1;
	double receivedFileLenth = -2;
	
    curl = curl_easy_init();
    if(curl)
    {
        fp = fopen(file, "w");
        if(fp)
        {
            curl_easy_setopt(curl, CURLOPT_URL, remote);
		    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
    
            //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
            curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
			
				//设置进度回调函数	
		    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, ProgressCallback);  
					 //set timeout
			curl_easy_setopt(curl, CURLOPT_TIMEOUT, 600L);//10000L);                                                   
    		curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 60L);  
			curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);	
			
			res = curl_easy_perform(curl);
			
			if (res != CURLE_OK)  
			{
                fprintf(stderr, "curl_easy_perform() failed: %s\n",
                        curl_easy_strerror(res));
				downloadFileLenth = -1;
            }
			else 
			{  
				curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &downloadFileLenth);  
				printf("~~~~~~~~~~~~http_download curl_easy_getinfo CURLINFO_CONTENT_LENGTH_DOWNLOAD downloadFileLenth:%lf\n",downloadFileLenth);
			}
            fclose(fp);
            curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
        }
        curl_easy_cleanup(curl);
		curl = NULL;
//		curl_global_cleanup();

        if(code == 200)
        {
        	if(stat(file, &buf) == 0)
			{
				receivedFileLenth = buf.st_size;
			}

			if(receivedFileLenth != downloadFileLenth || downloadFileLenth <= 0 || receivedFileLenth <= 0)
			{
				printf("~~~~~~~~~~~~http_download downloadFileLenth:%lf receivedFileLenth:%lf, not equal~~~~~~~~~~~~~~\n", downloadFileLenth, receivedFileLenth);
				return -1;
			}
            return 0;
        }
        else
        {
            if(stat(file, &buf) == 0)
            {
                fp = fopen(file, "r");
                if(fp)
                {
                    *received_data = malloc(buf.st_size);
                    if(*received_data)
                    {
                        if(fread(*received_data, 1, buf.st_size, fp) == buf.st_size)
                        {
                            fclose(fp);
                            remove(file);
                            return -1;
                        }
                        free(*received_data);
                        *received_data = NULL;
                    }
                    fclose(fp);
                }
                remove(file);
            }
            return -1;
        }
    }

    return -1;

}

int pf_http_url_down(char* url, char *file_path)
{
    int ret = -1;
    http_progress = 0;
    if(!url || !file_path)
    {
        printf("http down url or savepath error\n");
        return ret;
    }
    
    char cmdbuf[1024];
    char* msg = NULL;
    char download_path[64]="/tmp/";
    sprintf(cmdbuf, url,download_path);

    printf("cmdbuf = %s\n",cmdbuf);
    ret = pf_http_download(cmdbuf, file_path, &msg);
    return ret;
}

int pf_http_down_progress()
{
    return http_progress;
}


Logo

更多推荐