From 6dffbbba19ab6a8b50e531b32d0583e09a266b83 Mon Sep 17 00:00:00 2001 From: Flamingo Date: Tue, 2 Jan 2024 00:09:56 +0800 Subject: [PATCH] Add feature to upload files to remote ftp server --- Cigarette/Cigarette.vcxproj | 1 + Cigarette/FtpManager.h | 75 +++++++++++++++++++++++++++++++++++++ Cigarette/cigarette.cpp | 2 + Cigarette/exportData.cpp | 45 ++-------------------- Cigarette/workthread.cpp | 14 ++++++- 5 files changed, 94 insertions(+), 43 deletions(-) create mode 100644 Cigarette/FtpManager.h diff --git a/Cigarette/Cigarette.vcxproj b/Cigarette/Cigarette.vcxproj index 5d0c458..b4f501d 100644 --- a/Cigarette/Cigarette.vcxproj +++ b/Cigarette/Cigarette.vcxproj @@ -393,6 +393,7 @@ + $(QTDIR)\bin\moc.exe;%(FullPath) diff --git a/Cigarette/FtpManager.h b/Cigarette/FtpManager.h new file mode 100644 index 0000000..fa1ccd0 --- /dev/null +++ b/Cigarette/FtpManager.h @@ -0,0 +1,75 @@ +#pragma once + +#include "exportData.h" +#include "QtCore\qfile.h" +#include "QtCore\qtextstream.h" +#include +#include +#include +#include +#include +#include + +class FtpManager +{ +private: + QUrl url; + QNetworkAccessManager manager; + +public: + FtpManager() { + /* 设置通讯协议 */ + url.setScheme("ftp"); + /* 设置用户名 */ + url.setUserName("FTP2"); + /* 设置密码 */ + url.setPassword("123"); + /* 设置主机,也可以是域名 */ + url.setHost("192.168.1.170"); + /* 设置端口号,一般为21 */ + url.setPort(666); + } + + void uploadSingleFile(QString filePath, QString remotePath) { + + // 设置路径 + url.setPath(remotePath); + qDebug() << "uploadSingleFile path " << url.path(); + + // 装载本地文件 + QFile file(filePath); + bool isopen = false; + isopen = file.open(QIODevice::ReadOnly); + qDebug() << "Open file " << isopen; + if (isopen) { + // 读取本地文件数据 + QByteArray data = file.readAll(); + file.close(); + // 上传数据,上传成功后会在远端创建文件 + manager.setNetworkAccessible(QNetworkAccessManager::Accessible); + QNetworkReply* reply = manager.put(QNetworkRequest(url), data); + + QEventLoop eventLoop; + QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit())); + // 进入等待,但事件循环依然进行 */ + eventLoop.exec(); + + QObject::connect(reply, &QNetworkReply::finished, [&]() { + if (reply->error() == QNetworkReply::NoError) { + // 读取响应数据 + QByteArray responseData = reply->readAll(); + // 处理响应数据 + qDebug() << "Received response:" << responseData; + } + else { + // 处理错误 + qDebug() << "Error occurred:" << reply->errorString(); + } + + // 清理资源 + reply->deleteLater(); + }); + } + } + +}; \ No newline at end of file diff --git a/Cigarette/cigarette.cpp b/Cigarette/cigarette.cpp index a7d4e94..6112ade 100644 --- a/Cigarette/cigarette.cpp +++ b/Cigarette/cigarette.cpp @@ -7,6 +7,7 @@ #include #include #include "exportData.h" +#include int g_op_time; //操作员权限时长(默认300秒) int g_admin_time; //管理员操作权限时长(默认300秒) @@ -59,6 +60,7 @@ SyncQueue<_UDPSendInfo>* UDP_Info_queue; //SyncQueue<_ExportDataInfo> *export_Data_Info_queue; SyncQueue<_XMLExportDataInfo>* export_XMLData_Info_queue; ExportDataThread exportDataThread; +FtpManager ftpManager; bool g_admin_mode; //是否管理员模式 diff --git a/Cigarette/exportData.cpp b/Cigarette/exportData.cpp index 5a7f35a..a443907 100644 --- a/Cigarette/exportData.cpp +++ b/Cigarette/exportData.cpp @@ -1,18 +1,15 @@ #include "exportData.h" #include "QtCore\qfile.h" #include "QtCore\qtextstream.h" -#include -#include -#include -#include -#include #include +#include Cigarette* cg; //extern SyncQueue<_ExportDataInfo>* export_Data_Info_queue; extern SyncQueue<_XMLExportDataInfo>* export_XMLData_Info_queue; extern ConfPath g_conf_path; extern bool flag; +extern FtpManager ftpManager; ExportDataThread::ExportDataThread(QObject* parent) : QThread(parent) { @@ -272,43 +269,7 @@ void ExportDataThread::run() sprintf(buf, filePath.toLocal8Bit().constData(), element.cameraId); insertXMLNode(buf, element); - // - // Ftp transfer demo - // - QNetworkAccessManager manager; - QUrl url; - /* 设置通讯协议 */ - url.setScheme("ftp"); - /* 设置用户名 */ - url.setUserName("FTP2"); - /* 设置密码 */ - url.setPassword("123"); - /* 设置主机,也可以是域名 */ - url.setHost("192.168.1.170"); - /* 设置端口号,一般为21 */ - url.setPort(666); - /* 设置路径 */ - url.setPath("/tmp/" + xmlPath); - - /* 装载本地文件 */ - QFile file(filePath); - file.open(QIODevice::ReadOnly); - /* 读取本地文件数据 */ - QByteArray data = file.readAll(); - file.close(); - - QNetworkRequest request(url); - /* 上传数据,上传成功后会在远端创建/tmp/hello.png文件 */ - QNetworkReply* reply = manager.put(request, data); - - //QEventLoop eventLoop; - //QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit())); - /* 进入等待,但事件循环依然进行 */ - //eventLoop.exec(); - - //if (reply->error() != QNetworkReply::NoError) { - // qDebug() << "Error: " << reply->errorString(); - //} + ftpManager.uploadSingleFile(filePath, filePath); } } diff --git a/Cigarette/workthread.cpp b/Cigarette/workthread.cpp index 579456c..bd068da 100644 --- a/Cigarette/workthread.cpp +++ b/Cigarette/workthread.cpp @@ -7,11 +7,13 @@ #include #include "exportData.h" #include +#include extern AlgJd alg_jd[NumberOfSupportedCameras]; //检测胶点的AI算法 extern ConfPath g_conf_path; extern SysConf g_sys_conf; //系统配置参数 extern DisplayLabelConf g_display_label_conf[NumberOfSupportedCameras]; +extern FtpManager ftpManager; extern int rotationAngle[NumberOfSupportedCameras]; //图片旋转角度 extern bool isNeedRotate[NumberOfSupportedCameras]; @@ -240,7 +242,12 @@ void WorkThread::run() now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") + QString::number(local_camera_number + 1) + "#" + "_" + QString::number(index + 1) + "_" + ng_reason_maps[ngReason] + ".jpg"; + QString remotePath = "/image/ng/" + + now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") + QString::number(local_camera_number + 1) + + "#" + "_" + QString::number(index + 1) + "_" + ng_reason_maps[ngReason] + + ".jpg"; g_save_queue->put(std::make_pair(file_name.toLocal8Bit().constData(), m)); + ftpManager.uploadSingleFile(file_name, remotePath); m = vec_out[index].clone(); file_name = g_conf_path.save_pics_path + "/ng_result/" + @@ -248,9 +255,14 @@ void WorkThread::run() + QString::number(local_camera_number + 1) + "/" + QString::number(index + 1) + "/" + now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") + QString::number(local_camera_number + 1) + "#" + "_" + QString::number(index + 1) + "_" + ng_reason_maps[ngReason] + - ".bmp";; + ".bmp"; + remotePath = "/image/ng_result/" + + now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") + QString::number(local_camera_number + 1) + + "#" + "_" + QString::number(index + 1) + "_" + ng_reason_maps[ngReason] + + ".jpg"; //g_save_queue->put(std::make_pair(file_name.toStdString(), m)); g_save_queue->put(std::make_pair(file_name.toLocal8Bit().constData(), m)); + ftpManager.uploadSingleFile(file_name, remotePath); } } }