#include "app_curlFunc.h" #include #include // 添加此行 #include #include #include CurlFtpUploader::CurlFtpUploader(QObject *parent) : QObject(parent), m_port(21) {} CurlFtpUploader::~CurlFtpUploader() { curl_global_cleanup(); } void CurlFtpUploader::setFtpParams(const QString &username, const QString &password, const QString &host, int port, const QString &remotePath) { m_username = username; m_password = password; m_host = host; m_port = port; m_remotePath = remotePath; } void CurlFtpUploader::uploadFileAsync(const QString &localFilePath) { LOG("uploadFileAsync..."); // 在子线程中执行上传(避免阻塞主线程) QThread *thread = QThread::create([this, localFilePath]() { performUpload(localFilePath); }); connect(thread, &QThread::finished, thread, &QThread::deleteLater); thread->start(); } void CurlFtpUploader::performUpload(const QString &localFilePath) { curl_global_init(CURL_GLOBAL_ALL); CURL *curl = curl_easy_init(); if (!curl) { emit finished(false, "Failed to initialize CURL"); return; } // 打开本地文件 QFile file(localFilePath); if (!file.open(QIODevice::ReadOnly)) { emit finished(false, "Cannot open local file: " + localFilePath); curl_easy_cleanup(curl); return; } emit downloadStart(localFilePath); // 构建 FTP URL(示例:ftp://user:pass@host:port/remote/path/filename) QString url = QString("ftp://%1/%2/%3") .arg(m_host) .arg(m_remotePath) .arg(QFileInfo(localFilePath).fileName()); // 转义 URL 中的特殊字符 //char *escapedUrl = curl_easy_escape(curl, url.toUtf8().constData(), 0); QString username = m_username + ":" + m_password; // 设置 CURL 选项 curl_easy_setopt(curl, CURLOPT_URL, url.toUtf8().constData()); curl_easy_setopt(curl, CURLOPT_USERPWD, username.toUtf8().constData()); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); curl_easy_setopt(curl, CURLOPT_READDATA, &file); curl_easy_setopt(curl, CURLOPT_READFUNCTION, &CurlFtpUploader::readCallback); curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file.size()); curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L); // 自动创建目录 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180L); // 传输超时 180 秒 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 20L); // 连接超时 10 秒 // 进度回调 curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, &CurlFtpUploader::progressCallback); curl_easy_setopt(curl, CURLOPT_XFERINFODATA, this); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); // 执行上传 CURLcode res = curl_easy_perform(curl); file.close(); // 处理结果 if (res != CURLE_OK) { ERROR("上传失败:{}",curl_easy_strerror(res)); emit finished(false, localFilePath); } else { emit finished(true, localFilePath); } curl_easy_cleanup(curl); } // 静态回调:读取文件内容 size_t CurlFtpUploader::readCallback(char *buffer, size_t size, size_t nitems, void *userdata){ QFile *file = static_cast(userdata); qint64 bytesRead = file->read(buffer, size * nitems); return static_cast(bytesRead); } // 静态回调:上传进度 int CurlFtpUploader::progressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) { Q_UNUSED(dltotal); Q_UNUSED(dlnow); CurlFtpUploader *uploader = static_cast(clientp); if (ultotal > 0) { double percent = (static_cast(ulnow) / ultotal) * 100.0; emit uploader->progressChanged(percent); } // 计算剩余时间 double remainingTime = 0.0; return 0; }