#include "CurlFtp.h" #include #include #include CurlFtp::CurlFtp() { } CurlFtp::~CurlFtp() { } /* 检查文件夹是否存在,不确定好不好用 */ // bool CurlFtp::isDirExists(const std::string &ftpUrl, const std::string &username, const std::string &password) // { // CURL *curl; // CURLcode res; // bool result = false; // std::string retList; // curl_global_init(CURL_GLOBAL_DEFAULT); // curl = curl_easy_init(); // if(curl) // { // curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str()); // curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str()); // curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str()); // // curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1L); // We only want the directory listing // curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback); // curl_easy_setopt(curl, CURLOPT_WRITEDATA, &retList); // res = curl_easy_perform(curl); // // printf("res = %d\n", res); // if(res != CURLE_OK) // { // fprintf(stderr, "getDirList() failed, error code %d, :%s\n",res, curl_easy_strerror(res)); // } // else // { // // std::cout << "Directory list: \n" << retList << std::endl; // } // curl_easy_cleanup(curl); // } // curl_global_cleanup(); // return result; // } /* 使用正则表达式提取出文件夹 */ // bool CurlFtp::extractDirectories(const std::string& responseString) // { // std::regex directoryRegex(R"(^d.*\s(\S+)$)"); // std::smatch match; // std::istringstream retStream(responseString); // std::string line; // while (std::getline(retStream, line)) // { // if (std::regex_match(line, match, directoryRegex)) // { // std::cout << "Directory: " << match[1] << std::endl; // } // } // return true; // } /** * @brief 列出FTP文件夹 * * @param ftpUrl 需要列出文件夹的FTP地址 * @param username * @param password * @return std::string */ std::string CurlFtp::listDir(const std::string &ftpUrl, const std::string &username, const std::string &password) { CURL *curl; CURLcode res; bool result = false; std::string retList; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str()); curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str()); curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str()); // curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1L); // We only want the directory listing curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &retList); res = curl_easy_perform(curl); // printf("res = %d\n", res); if(res != CURLE_OK) { fprintf(stderr, "getDirList() failed, error code %d, :%s\n",res, curl_easy_strerror(res)); } else { // std::cout << "Directory list: \n" << retList << std::endl; } curl_easy_cleanup(curl); } curl_global_cleanup(); return retList; } /* 写入回调函数,listDir需要调用 */ size_t CurlFtp::writeCallback(void *contents, size_t size, size_t nmemb, std::string *s) { size_t newLength = size*nmemb; size_t oldLength = s->size(); try { s->resize(oldLength + newLength); } catch(std::bad_alloc &e) { //handle memory problem return 0; } std::copy((char*)contents,(char*)contents+newLength,s->begin()+oldLength); return size*nmemb; } // Callback function to collect the list of files static int filelistCallback(void* buffer, size_t size, size_t nmemb, void* userp) { std::vector* fileList = static_cast*>(userp); std::string line(static_cast(buffer), size * nmemb); fileList->push_back(line); return size * nmemb; } /* 列出文件夹中的所有文件 */ bool CurlFtp::listFiles(const std::string &ftpUrl, const std::string &username, const std::string &password, std::vector& fileList) { CURL *curl; CURLcode res; bool result = false; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, (ftpUrl).c_str()); curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str()); curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str()); curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, filelistCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fileList); res = curl_easy_perform(curl); if(res != CURLE_OK) { fprintf(stderr, "Failed to get file list, error code :%d ,%s\n", res, curl_easy_strerror(res)); } else { // for(const std::string& filename : fileList) // { // printf("%s\n", filename.c_str()); // } result = true; } curl_easy_cleanup(curl); } curl_global_cleanup(); return result; } /* 创建文件夹 */ bool CurlFtp::createDir(const std::string &ftpUrl, const std::string &username, const std::string &password, const std::string &dirName) { CURL *curl; CURLcode res; bool result = false; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str()); curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str()); curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str()); // Create a list of FTP commands to be executed on the server struct curl_slist *headerlist = NULL; std::string mkdir = "MKD " + dirName; headerlist = curl_slist_append(headerlist, mkdir.c_str()); // Set the list of FTP commands to be executed on the server before the transfer curl_easy_setopt(curl, CURLOPT_QUOTE, headerlist); // Tell libcurl to not include the body in the output curl_easy_setopt(curl, CURLOPT_NOBODY, 1L); res = curl_easy_perform(curl); if(res != CURLE_OK) { fprintf(stderr, "createDir() failed, error code :%d ,%s\n", res, curl_easy_strerror(res)); result = false; }else { result = true; } curl_easy_cleanup(curl); curl_slist_free_all(headerlist); } curl_global_cleanup(); return result; }