app_curlFunc.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "app_curlFunc.h"
  2. #include <QFile>
  3. #include <QFileInfo> // 添加此行
  4. #include <QThread>
  5. #include <QDebug>
  6. #include <m_loger.h>
  7. CurlFtpUploader::CurlFtpUploader(QObject *parent) : QObject(parent), m_port(21) {}
  8. CurlFtpUploader::~CurlFtpUploader() {
  9. curl_global_cleanup();
  10. }
  11. void CurlFtpUploader::setFtpParams(const QString &username,
  12. const QString &password,
  13. const QString &host,
  14. int port,
  15. const QString &remotePath) {
  16. m_username = username;
  17. m_password = password;
  18. m_host = host;
  19. m_port = port;
  20. m_remotePath = remotePath;
  21. }
  22. void CurlFtpUploader::uploadFileAsync(const QString &localFilePath) {
  23. LOG("uploadFileAsync...");
  24. // 在子线程中执行上传(避免阻塞主线程)
  25. QThread *thread = QThread::create([this, localFilePath]() {
  26. performUpload(localFilePath);
  27. });
  28. connect(thread, &QThread::finished, thread, &QThread::deleteLater);
  29. thread->start();
  30. }
  31. void CurlFtpUploader::performUpload(const QString &localFilePath) {
  32. curl_global_init(CURL_GLOBAL_ALL);
  33. CURL *curl = curl_easy_init();
  34. if (!curl) {
  35. emit finished(false, "Failed to initialize CURL");
  36. return;
  37. }
  38. // 打开本地文件
  39. QFile file(localFilePath);
  40. if (!file.open(QIODevice::ReadOnly)) {
  41. emit finished(false, "Cannot open local file: " + localFilePath);
  42. curl_easy_cleanup(curl);
  43. return;
  44. }
  45. emit downloadStart(localFilePath);
  46. // 构建 FTP URL(示例:ftp://user:pass@host:port/remote/path/filename)
  47. QString url = QString("ftp://%1/%2/%3")
  48. .arg(m_host)
  49. .arg(m_remotePath)
  50. .arg(QFileInfo(localFilePath).fileName());
  51. // 转义 URL 中的特殊字符
  52. //char *escapedUrl = curl_easy_escape(curl, url.toUtf8().constData(), 0);
  53. QString username = m_username + ":" + m_password;
  54. // 设置 CURL 选项
  55. curl_easy_setopt(curl, CURLOPT_URL, url.toUtf8().constData());
  56. curl_easy_setopt(curl, CURLOPT_USERPWD, username.toUtf8().constData());
  57. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  58. curl_easy_setopt(curl, CURLOPT_READDATA, &file);
  59. curl_easy_setopt(curl, CURLOPT_READFUNCTION, &CurlFtpUploader::readCallback);
  60. curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file.size());
  61. curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L); // 自动创建目录
  62. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180L); // 传输超时 180 秒
  63. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 20L); // 连接超时 10 秒
  64. // 进度回调
  65. curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, &CurlFtpUploader::progressCallback);
  66. curl_easy_setopt(curl, CURLOPT_XFERINFODATA, this);
  67. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
  68. // 执行上传
  69. CURLcode res = curl_easy_perform(curl);
  70. file.close();
  71. // 处理结果
  72. if (res != CURLE_OK) {
  73. ERROR("上传失败:{}",curl_easy_strerror(res));
  74. emit finished(false, localFilePath);
  75. } else {
  76. emit finished(true, localFilePath);
  77. }
  78. curl_easy_cleanup(curl);
  79. }
  80. // 静态回调:读取文件内容
  81. size_t CurlFtpUploader::readCallback(char *buffer, size_t size, size_t nitems, void *userdata){
  82. QFile *file = static_cast<QFile*>(userdata);
  83. qint64 bytesRead = file->read(buffer, size * nitems);
  84. return static_cast<size_t>(bytesRead);
  85. }
  86. // 静态回调:上传进度
  87. int CurlFtpUploader::progressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
  88. Q_UNUSED(dltotal);
  89. Q_UNUSED(dlnow);
  90. CurlFtpUploader *uploader = static_cast<CurlFtpUploader*>(clientp);
  91. if (ultotal > 0) {
  92. double percent = (static_cast<double>(ulnow) / ultotal) * 100.0;
  93. emit uploader->progressChanged(percent);
  94. }
  95. // 计算剩余时间
  96. double remainingTime = 0.0;
  97. return 0;
  98. }