windows下使用vcpkg编译libcurl库并使用C++实现ftp上传下载功能

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值