以下代码是在前面示例代码基础上添加连接和登录失败处理的代码。在 FTPClient 类中,connect() 和 login() 两个操作会返回 bool 类型,表示是否连接或者登录成功。使用 FTPClient 类时,可以根据这个返回值来判断操作是否成功。
#include <curl/curl.h>
#include <iostream>
class FTPClient {
public:
FTPClient(const char* server, const char* username, const char* password)
: curl_handle_(curl_easy_init()), server_(server), username_(username), password_(password)
{
curl_global_init(CURL_GLOBAL_DEFAULT);
}
~FTPClient() {
curl_easy_cleanup(curl_handle_);
curl_global_cleanup();
}
// 连接 FTP 服务器
bool connect() {
if(curl_handle_ == NULL) {
std::cerr << "Failed to initialize curl!" << std::endl;
return false;
}
std::string ftpurl = "ftp://" + std::string(server_);
curl_easy_setopt(curl_handle_, CURLOPT_URL, ftpurl.c_str());
curl_easy_setopt(curl_handle_, CURLOPT_PORT, 21L);
return true;
}
// 登录
bool login() {
if(curl_handle_ == NULL) {
std::cerr << "FTPClient not initialized!" << std::endl;
return false;
}
std::string userpwd = username_ + ":" + password_;
curl_easy_setopt(curl_handle_, CURLOPT_USERPWD, userpwd.c_str());
curl_easy_setopt(curl_handle_, CURLOPT_TRANSFERTEXT, 0L);
CURLcode result = curl_easy_perform(curl_handle_);
if(result != CURLE_OK) {
std::cerr << "Login failed: " << curl_easy_strerror(result) << std::endl;
return false;
}
return true;
}
// 上传文件
bool upload(const char* localpath, const char* remotepath) {
bool result = false;
FILE* fp = fopen(localpath, "rb");
if(fp == NULL) {
std::cerr << "Failed to open file: " << localpath << std::endl;
return false;
}
curl_easy_setopt(curl_handle_, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl_handle_, CURLOPT_READDATA, fp);
std::string remoteurl = "ftp://" + std::string(server_) + remotepath;
curl_easy_setopt(curl_handle_, CURLOPT_URL, remoteurl.c_str());
CURLcode res = curl_easy_perform(curl_handle_);
if(res == CURLE_OK) {
result = true;
} else {
std::cerr << "Upload failed: " << curl_easy_strerror(res) << std::endl;
}
curl_easy_reset(curl_handle_);
fclose(fp);
return result;
}
// 下载文件
bool download(const char* remotepath, const char* localpath) {
bool result = false;
FILE* fp = fopen(localpath, "wb");
if(fp == NULL) {
std::cerr << "Failed to open file: " << localpath << std::endl;
return false;
}
std::string remoteurl = "ftp://" + std::string(server_) + remotepath;
curl_easy_setopt(curl_handle_, CURLOPT_URL, remoteurl.c_str());
curl_easy_setopt(curl_handle_, CURLOPT_TRANSFERTEXT, 0L);
curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION, fwrite);
curl_easy_setopt(curl_handle_, CURLOPT_WRITEDATA, fp);
CURLcode res = curl_easy_perform(curl_handle_);
if(res == CURLE_OK) {
result = true;
} else {
std::cerr << "Download failed: " << curl_easy_strerror(res) << std::endl;
}
curl_easy_reset(curl_handle_);
fclose(fp);
return result;
}
private:
CURL* curl_handle_;
const char* server_;
const char* username_;
const char* password_;
};
int main() {
FTPClient ftpclient("example.com", "username", "password");
if(!ftpclient.connect()) {
std::cerr << "Failed to connect FTP server!" << std::endl;
return 1;
}
if(!ftpclient.login()) {
std::cerr << "Failed to login FTP server!" << std::endl;
return 1;
}
if(!ftpclient.upload("localfile.txt", "/remotefolder/remote.txt")) {
std::cerr << "Failed to upload file!" << std::endl;
}
if(!ftpclient.download("/remotefolder/test.txt", "localfile.txt")) {
std::cerr << "Failed to download file!" << std::endl;
}
return 0;
}
在 connect() 和 login() 操作中,使用了 std::cerr 来输出错误信息,可以修改为抛出异常等方式来处理错误。除此之外,FTPClient 类还可以根据实际需求添加其他操作,例如删除文件、创建目录等。