CurlFtp.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. * 3.设置好IP和端口号后,后续输入的文件路径都是想对你路径,无需添加IP和端口号
  15. *
  16. */
  17. class CurlFtp
  18. {
  19. public:
  20. CurlFtp();
  21. ~CurlFtp();
  22. // /* 列出FTP文件夹 */
  23. // static std::string listDir(const std::string &ftpUrl, const std::string &username, const std::string &password);
  24. // /* 列出文件夹中的所有文件 */
  25. // static bool listFiles(const std::string &ftpUrl, const std::string &username, const std::string &password, std::vector<std::string>& fileList);
  26. // /* 创建文件夹 */
  27. // static bool createDir(const std::string &ftpUrl, const std::string &username, const std::string &password, const std::string &dirName);
  28. /*********************** 成员函数 *********************/
  29. /* 设置IP和端口 */
  30. bool setFtpIPAndPort(const std::string& IP, const int port);
  31. /* 设置SFTP的IP和端口 */
  32. bool setSftpIPAndPort(const std::string& IP, const int port);
  33. /* 设置是否忽略SSL证书,使用SFTP不建议忽略 */
  34. void setIgnoreSSLCert(bool isIgnore);
  35. /* 设置CA证书文件 */
  36. void setCaCertFile(const std::string& caCertFile);
  37. /* 设置用户名和密码 */
  38. bool setUsernameAndPassword(const std::string& username, const std::string& password);
  39. /* 列出文件列表 */
  40. bool getFileList(std::string dir, std::vector<std::string>& fileList);
  41. /* 获取文件夹列表 */
  42. bool getDirList(std::string dir, std::vector<std::string>& dirList);
  43. /* 判断文件夹是否存在 */
  44. bool isDirExist(const std::string& dir);
  45. /* 创建FTP文件夹 */
  46. bool createDirectory(const std::string& ftpDir);
  47. /* 创建FTP文件夹,递归创建 */
  48. bool createDirectories(const std::string& ftpDir);
  49. /* 下载文件 */
  50. bool downloadFile(const std::string& remoteFile, const std::string& localFile);
  51. /* 上传文件 */
  52. bool uploadFile(const std::string& localFile, const std::string& remoteFile);
  53. private:
  54. /* 列出所有内容 */
  55. bool listAll(CURL* curl, std::string dir, std::vector<CF_FileInfo>& fileInfoList);
  56. /* 检查FTP文件夹路径是否合规,不合规就修改 */
  57. std::string checkDirPath(const std::string& dir);
  58. /* 检查FTP文件路径是否合规 */
  59. std::string checkFilePath(const std::string& file);
  60. /* 检查本地文件夹是否存在,不存在则创建 */
  61. bool checkLocalDir(const std::string& localDir);
  62. private:
  63. std::mutex m_mutexCurl; /* curl互斥锁 */
  64. std::string m_currentDir; /* 当前目录 */
  65. std::string m_IP; /* IP */
  66. int m_port = 21; /* 端口 */
  67. std::string m_ftpUrl; /* ftpUrl */
  68. std::string m_username;
  69. std::string m_password;
  70. bool m_isSftp = false; /* 是否是SFTP */
  71. bool m_isIgnoreSSLCert = false; /* 是否忽略SSL证书 */
  72. std::string m_caCertFile; /* CA证书文件 */
  73. };
  74. #endif /* CURLFTP_H */