CurlFtp.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "CurlFtp.h"
  2. #include <iostream>
  3. #include <regex>
  4. #include <iosfwd>
  5. CurlFtp::CurlFtp()
  6. {
  7. }
  8. CurlFtp::~CurlFtp()
  9. {
  10. }
  11. /* 检查文件夹是否存在,不确定好不好用 */
  12. // bool CurlFtp::isDirExists(const std::string &ftpUrl, const std::string &username, const std::string &password)
  13. // {
  14. // CURL *curl;
  15. // CURLcode res;
  16. // bool result = false;
  17. // std::string retList;
  18. // curl_global_init(CURL_GLOBAL_DEFAULT);
  19. // curl = curl_easy_init();
  20. // if(curl)
  21. // {
  22. // curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str());
  23. // curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str());
  24. // curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str());
  25. // // curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1L); // We only want the directory listing
  26. // curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
  27. // curl_easy_setopt(curl, CURLOPT_WRITEDATA, &retList);
  28. // res = curl_easy_perform(curl);
  29. // // printf("res = %d\n", res);
  30. // if(res != CURLE_OK)
  31. // {
  32. // fprintf(stderr, "getDirList() failed, error code %d, :%s\n",res, curl_easy_strerror(res));
  33. // }
  34. // else
  35. // {
  36. // // std::cout << "Directory list: \n" << retList << std::endl;
  37. // }
  38. // curl_easy_cleanup(curl);
  39. // }
  40. // curl_global_cleanup();
  41. // return result;
  42. // }
  43. /* 使用正则表达式提取出文件夹 */
  44. // bool CurlFtp::extractDirectories(const std::string& responseString)
  45. // {
  46. // std::regex directoryRegex(R"(^d.*\s(\S+)$)");
  47. // std::smatch match;
  48. // std::istringstream retStream(responseString);
  49. // std::string line;
  50. // while (std::getline(retStream, line))
  51. // {
  52. // if (std::regex_match(line, match, directoryRegex))
  53. // {
  54. // std::cout << "Directory: " << match[1] << std::endl;
  55. // }
  56. // }
  57. // return true;
  58. // }
  59. /**
  60. * @brief 列出FTP文件夹
  61. *
  62. * @param ftpUrl 需要列出文件夹的FTP地址
  63. * @param username
  64. * @param password
  65. * @return std::string
  66. */
  67. std::string CurlFtp::listDir(const std::string &ftpUrl, const std::string &username, const std::string &password)
  68. {
  69. CURL *curl;
  70. CURLcode res;
  71. bool result = false;
  72. std::string retList;
  73. curl_global_init(CURL_GLOBAL_DEFAULT);
  74. curl = curl_easy_init();
  75. if(curl)
  76. {
  77. curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str());
  78. curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str());
  79. curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str());
  80. // curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1L); // We only want the directory listing
  81. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
  82. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &retList);
  83. res = curl_easy_perform(curl);
  84. // printf("res = %d\n", res);
  85. if(res != CURLE_OK)
  86. {
  87. fprintf(stderr, "getDirList() failed, error code %d, :%s\n",res, curl_easy_strerror(res));
  88. }
  89. else
  90. {
  91. // std::cout << "Directory list: \n" << retList << std::endl;
  92. }
  93. curl_easy_cleanup(curl);
  94. }
  95. curl_global_cleanup();
  96. return retList;
  97. }
  98. /* 写入回调函数,listDir需要调用 */
  99. size_t CurlFtp::writeCallback(void *contents, size_t size, size_t nmemb, std::string *s)
  100. {
  101. size_t newLength = size*nmemb;
  102. size_t oldLength = s->size();
  103. try
  104. {
  105. s->resize(oldLength + newLength);
  106. }
  107. catch(std::bad_alloc &e)
  108. {
  109. //handle memory problem
  110. return 0;
  111. }
  112. std::copy((char*)contents,(char*)contents+newLength,s->begin()+oldLength);
  113. return size*nmemb;
  114. }
  115. // Callback function to collect the list of files
  116. static int filelistCallback(void* buffer, size_t size, size_t nmemb, void* userp)
  117. {
  118. std::vector<std::string>* fileList = static_cast<std::vector<std::string>*>(userp);
  119. std::string line(static_cast<char*>(buffer), size * nmemb);
  120. fileList->push_back(line);
  121. return size * nmemb;
  122. }
  123. /* 列出文件夹中的所有文件 */
  124. bool CurlFtp::listFiles(const std::string &ftpUrl, const std::string &username, const std::string &password, std::vector<std::string>& fileList)
  125. {
  126. CURL *curl;
  127. CURLcode res;
  128. bool result = false;
  129. curl_global_init(CURL_GLOBAL_DEFAULT);
  130. curl = curl_easy_init();
  131. if(curl)
  132. {
  133. curl_easy_setopt(curl, CURLOPT_URL, (ftpUrl).c_str());
  134. curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str());
  135. curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str());
  136. curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1L);
  137. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, filelistCallback);
  138. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fileList);
  139. res = curl_easy_perform(curl);
  140. if(res != CURLE_OK)
  141. {
  142. fprintf(stderr, "Failed to get file list, error code :%d ,%s\n", res, curl_easy_strerror(res));
  143. }
  144. else
  145. {
  146. // for(const std::string& filename : fileList)
  147. // {
  148. // printf("%s\n", filename.c_str());
  149. // }
  150. result = true;
  151. }
  152. curl_easy_cleanup(curl);
  153. }
  154. curl_global_cleanup();
  155. return result;
  156. }
  157. /* 创建文件夹 */
  158. bool CurlFtp::createDir(const std::string &ftpUrl, const std::string &username, const std::string &password, const std::string &dirName)
  159. {
  160. CURL *curl;
  161. CURLcode res;
  162. bool result = false;
  163. curl_global_init(CURL_GLOBAL_DEFAULT);
  164. curl = curl_easy_init();
  165. if(curl)
  166. {
  167. curl_easy_setopt(curl, CURLOPT_URL, ftpUrl.c_str());
  168. curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str());
  169. curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str());
  170. // Create a list of FTP commands to be executed on the server
  171. struct curl_slist *headerlist = NULL;
  172. std::string mkdir = "MKD " + dirName;
  173. headerlist = curl_slist_append(headerlist, mkdir.c_str());
  174. // Set the list of FTP commands to be executed on the server before the transfer
  175. curl_easy_setopt(curl, CURLOPT_QUOTE, headerlist);
  176. // Tell libcurl to not include the body in the output
  177. curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
  178. res = curl_easy_perform(curl);
  179. if(res != CURLE_OK)
  180. {
  181. fprintf(stderr, "createDir() failed, error code :%d ,%s\n", res, curl_easy_strerror(res));
  182. result = false;
  183. }else
  184. {
  185. result = true;
  186. }
  187. curl_easy_cleanup(curl);
  188. curl_slist_free_all(headerlist);
  189. }
  190. curl_global_cleanup();
  191. return result;
  192. }