CurlFtp.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef CURLFTP_H
  2. #define CURLFTP_H
  3. #include "curl/curl.h"
  4. #include <string>
  5. #include <vector>
  6. #include <mutex>
  7. #include "CurlFtpInfo.h"
  8. /**
  9. * 使用CURL进行FTP操作,这个类有两种方式,一种是直接使用静态方法,另一种是创建实例
  10. * 使用静态函数可以进行一些简单的操作,但是如果需要进行多次操作,最好创建实例
  11. *
  12. * 1.默认超时时间是10s
  13. * 2.默认端口是21
  14. *
  15. */
  16. class CurlFtp
  17. {
  18. public:
  19. CurlFtp();
  20. ~CurlFtp();
  21. /* 列出FTP文件夹 */
  22. static std::string listDir(const std::string &ftpUrl, const std::string &username, const std::string &password);
  23. /* 列出文件夹中的所有文件 */
  24. static bool listFiles(const std::string &ftpUrl, const std::string &username, const std::string &password, std::vector<std::string>& fileList);
  25. /* 创建文件夹 */
  26. static bool createDir(const std::string &ftpUrl, const std::string &username, const std::string &password, const std::string &dirName);
  27. /*********************** 成员函数 *********************/
  28. /* 设置IP和端口 */
  29. bool setFtpIPAndPort(const std::string& IP, const int port);
  30. /* 设置用户名和密码 */
  31. bool setFtpUsernameAndPassword(const std::string& username, const std::string& password);
  32. /* 列出文件列表 */
  33. bool getFileList(std::string dir, std::vector<std::string>& fileList);
  34. /* 获取文件夹列表 */
  35. bool getDirList(std::string dir, std::vector<std::string>& dirList);
  36. /* 判断文件夹是否存在 */
  37. bool isDirExist(const std::string& dir);
  38. /* 创建FTP文件夹 */
  39. bool createDirectory(const std::string& ftpDir);
  40. /* 创建FTP文件夹,递归创建 */
  41. bool createDirectories(const std::string& ftpDir);
  42. /* 下载文件 */
  43. bool downloadFile(const std::string& remoteFile, const std::string& localFile);
  44. /* 上传文件 */
  45. bool uploadFile(const std::string& localFile, const std::string& remoteFile);
  46. private:
  47. /* 列出所有内容 */
  48. bool listAll(CURL* curl, std::string dir, std::vector<CF_FileInfo>& fileInfoList);
  49. /* 检查FTP文件夹路径是否合规,不合规就修改 */
  50. std::string checkDirPath(const std::string& dir);
  51. /* 检查FTP文件路径是否合规 */
  52. std::string checkFilePath(const std::string& file);
  53. /* 检查本地文件夹是否存在,不存在则创建 */
  54. bool checkLocalDir(const std::string& localDir);
  55. private:
  56. std::mutex m_mutexCurl; /* curl互斥锁 */
  57. std::string m_currentDir; /* 当前目录 */
  58. std::string m_IP; /* IP */
  59. int m_port = 21; /* 端口 */
  60. std::string m_ftpUrl; /* ftpUrl */
  61. std::string m_username;
  62. std::string m_password;
  63. };
  64. #endif /* CURLFTP_H */