zoukankan      html  css  js  c++  java
  • 使用curl下载文件

    curl是一个非常好的网络传输库,使用也很简单。常用的使用方式是用它来下载资源文件,以下提供一个下载方法

     1 #include <stdio.h>
     2 #include <iostream.h>
     3 #include <curl/curl.h>
     4 
     5 using namespace std;
     6 
     7 int download(string url, string local_file, int down_speed)
     8 {
     9   CURL *image;
    10   CURLcode imgresult;
    11   FILE *fp;
    12   //url_download.c_str();
    13 
    14     image = curl_easy_init();
    15     if( image )
    16      {
    17         //Open File
    18         fp = fopen(local_file.c_str(), "w");
    19         if( fp == NULL ) cout << "File cannot be opened";
    20         
    21         curl_easy_setopt(image, CURLOPT_WRITEFUNCTION, NULL);
    22         curl_easy_setopt(image, CURLOPT_WRITEDATA, fp);
    23         curl_easy_setopt(image, CURLOPT_URL, url.c_str());
    24         curl_easy_setopt(image, CURLOPT_FOLLOWLOCATION, 1);
    25 //这里限速 100KB/s
    26         curl_easy_setopt(image, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t)100 * 1024);
    27         curl_easy_setopt(image, CURLOPT_NOPROGRESS, 0);
    28         //CURLOPT_RESUME_FROM
    29         
    30         // Grab image
    31         imgresult = curl_easy_perform(image);
    32         if( imgresult )
    33             {
    34                 cout << "Cannot grab the File!
    ";
    35             }
    36     }
    37     //Clean up the resources
    38     curl_easy_cleanup(image);
    39     //Close the file
    40     fclose(fp);
    41     return 0;
    42 }
  • 相关阅读:
    SA 的参数
    superobject中 JavaToDelphiDateTime的使用
    关于Linux下进程间使用共享内存和信号量通信的时的编译问题
    Linux匿名管道与命名管道
    C++复数四则运算的实现
    C++类编程(一)const的使用
    读书会思考与学期总结
    xenomai安装
    Xenomai 安装准备工作
    Xenomai
  • 原文地址:https://www.cnblogs.com/howeho/p/3402035.html
Copyright © 2011-2022 走看看