1、下载安装vcpkg
git clone https://github.com/microsoft/vcpkg
2、编译vcpkg
使用cmd命令
D:\Code\ThirdParty>cd vcpkg
D:\Code\ThirdParty\vcpkg>bootstrap-vcpkg.bat
3、使用vcpkg编译所需的库
进入vckpkg目录,使用vckpkg install 命令进行安装。在安装前可使用vcpkg search命令进行库的检索。
4、下载编译x64位的libcurl库
vcpkg install curl:x64-windows
之后在这个路径下可以获取到需要的动态库和静态库以及头文件,debug和release都有。
6、示例证明libcurl库被正确编译和使用
正确链接编译产生的库,该示例是显示网址的源码
#include <iostream>
#include "include/curl/curl.h"
int main()
{
CURL* curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
}
system("pause");
return 0;
}
5、使用libcurl库进行ftp的上传和下载
#include <iostream>
#include "include/curl/curl.h"
// 回调函数,用于显示进度
size_t progress_callback(void* clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
{
std::cout << "上传进度: " << ulnow << " / " << ultotal << std::endl;
return 0;
}
int uploadFile(const std::string& ftpUrl, const std::string& localFilePath, const std::string& ftpUsername, const std::string& ftpPassword)
{
CURL* curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl)
{
// 设置 FTP 上传地址
curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str());
// 设置用户名和密码
curl_easy_setopt(curl, CURLOPT_USERNAME, ftpUsername.c_str());
curl_easy_setopt(curl, CURLOPT_PASSWORD, ftpPassword.c_str());
// 启用上传
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
// 本地文件路径
FILE* fp = fopen(localFilePath.c_str(), "rb");
if (fp)
{
curl_easy_setopt(curl, CURLOPT_READDATA, fp);
// 设置进度回调函数
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
res = curl_easy_perform(curl);
fclose(fp);
if (res != CURLE_OK)
std::cerr << "上传失败: " << curl_easy_strerror(res) << std::endl;
else
std::cout << "上传成功" << std::endl;
}
else {
std::cerr << "无法打开本地文件: " << localFilePath << std::endl;
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
// 回调函数,用于写入下载的数据
size_t write_data(void* ptr, size_t size, size_t nmemb, FILE* stream)
{
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
int downloadFile(const std::string& ftpUrl, const std::string& savePath, const std::string& ftpUsername, const std::string& ftpPassword)
{
CURL* curl;
CURLcode res;
FILE* fp;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl)
{
fp = fopen(savePath.c_str(), "wb");
curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str());
curl_easy_setopt(curl, CURLOPT_USERNAME, ftpUsername.c_str());
curl_easy_setopt(curl, CURLOPT_PASSWORD, ftpPassword.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
fclose(fp);
if (res != CURLE_OK)
std::cerr << "下载失败: " << curl_easy_strerror(res) << std::endl;
else
std::cout << "下载成功" << std::endl;
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
int main()
{
std::string ftpUrl = "ftp://192.168.1.5:21/2.png"; // ftp服务器的文件路径,不存在的文件会自动创建
std::string localFilePath = "D:/2.png"; // 本地文件路径
std::string ftpUsername = "2906141298@qq.com"; // 登录ftp服务器的用户名
std::string ftpPassword = "Zhn258369."; // 登录ftp服务器的密码
// 上传
uploadFile(ftpUrl, localFilePath, ftpUsername, ftpPassword);
// 下载
downloadFile(ftpUrl, localFilePath, ftpUsername, ftpPassword);
system("pause");
return 0;
}