Add feature to upload files to remote ftp server

main
Flamingo 1 year ago
parent 5aa4f78df5
commit 6dffbbba19

@ -393,6 +393,7 @@
</CustomBuild>
</ItemGroup>
<ItemGroup>
<ClInclude Include="FtpManager.h" />
<ClInclude Include="tinyxml2.h" />
<CustomBuild Include="threadReceive.h">
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(QTDIR)\bin\moc.exe;%(FullPath)</AdditionalInputs>

@ -0,0 +1,75 @@
#pragma once
#include "exportData.h"
#include "QtCore\qfile.h"
#include "QtCore\qtextstream.h"
#include <QUrl>
#include <QFile>
#include <QObject>
#include <QByteArray>
#include <QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
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();
});
}
}
};

@ -7,6 +7,7 @@
#include <QtCore\qcryptographichash.h>
#include <QtCore\qprocess.h>
#include "exportData.h"
#include <FtpManager.h>
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; //是否管理员模式

@ -1,18 +1,15 @@
#include "exportData.h"
#include "QtCore\qfile.h"
#include "QtCore\qtextstream.h"
#include <QUrl>
#include <QFile>
#include <QObject>
#include <QByteArray>
#include <QNetworkAccessManager>
#include <string>
#include <ftpManager.h>
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);
}
}

@ -7,11 +7,13 @@
#include <PLCDevice.h>
#include "exportData.h"
#include <QMap>
#include <ftpManager.h>
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);
}
}
}

Loading…
Cancel
Save