使用C++和libcurl下载指定的文件

使用C++语言,利用libcurl库,下载指定的https中的文件:比如https://www.xxxx.com/xxx/yyy/zzz.pdf

#include 
#include 
#include 

// 写入数据的回调函数
size_t WriteData(void* ptr, size_t size, size_t nmemb, std::ofstream* stream)
{
    stream->write(static_cast(ptr), size * nmemb);
    return size * nmemb;
}

// 分段下载函数
bool DownloadSegment(const std::string& url, std::ofstream& output, long start, long end)
{
    CURL* curl;
    CURLcode res;
    curl = curl_easy_init();
    if (curl) {
        // 设置URL
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

        // 设置Range头
        std::string range = std::to_string(start) + "-" + std::to_string(end);
        curl_easy_setopt(curl, CURLOPT_RANGE, range.c_str());

        // 设置写入数据的回调函数
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteData);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &output);

        // 设置SSL选项
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); // 验证服务器的SSL证书
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); // 验证证书上的主机名

        // 执行请求
        res = curl_easy_perform(curl);

        // 清理
        curl_easy_cleanup(curl);

        if (res != CURLE_OK) {
            std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
            return false;
        }
        return true;
    }
    return false;
}

// 获取文件大小
long GetFileSize(const std::string& url)
{
    CURL* curl;
    CURLcode res;
    double file_size = 0.0;

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_NOBODY, 1L); // 不下载内容
        curl_easy_setopt(curl, CURLOPT_HEADER, 1L); // 只获取头部信息

        // 设置SSL选项
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); // 验证服务器的SSL证书
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); // 验证证书上的主机名

        res = curl_easy_perform(curl);

        if (res == CURLE_OK) {
            res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &file_size);
            if ((res == CURLE_OK) && (file_size > 0.0)) {
                curl_easy_cleanup(curl);
                return static_cast(file_size);
            }
        } else {
            std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
        }

        curl_easy_cleanup(curl);
    }

    return -1;
}

int main() {
    std::string url = "https://img.linux.net.cn/lfs/LFS-BOOK-7.7-systemd.pdf"; // 替换为实际URL
    std::string output_filename = "/home/licks/https_download/LFS-BOOK-7.7-systemd.pdf";
    long segment_size = 1024 * 1024; // 1MB

    // 获取文件大小
    long file_size = GetFileSize(url);
    if (file_size == -1) {
        std::cerr << "Failed to get file size." << std::endl;
        return 1;
    }

    std::cout << "File size: " << file_size << " bytes" << std::endl;

    // 打开输出文件
    std::ofstream output(output_filename, std::ios::binary);
    if (!output.is_open()) {
        std::cerr << "Failed to open output file." << std::endl;
        return 1;
    }

    // 分段下载
    for (long start = 0; start < file_size; start += segment_size) {
        long end = start + segment_size - 1;
        if (end >= file_size) {
            end = file_size - 1;
        }
        std::cout << "Downloading segment: " << start << "-" << end << std::endl;
        if (!DownloadSegment(url, output, start, end)) {
            std::cerr << "Failed to download segment." << std::endl;
            output.close();
            return 1;
        }
    }

    // 关闭输出文件
    output.close();
    std::cout << "Download complete." << std::endl;

    return 0;
}

使用的是CMake作为代码工程管理手段:

cmake_minimum_required(VERSION 3.0)
project(download_https)

set(CMAKE_CXX_STANDARD 11)

add_executable(download_https main.cpp)
target_link_libraries(download_https curl)

你可能感兴趣的:(c++,数据库,开发语言)