完善ftp 发送压缩文件功能,发送完zip以后清除缓存

CigaretteSH
Flamingo 9 months ago
parent ccf44a00d3
commit cf45f5c8a1

@ -8,6 +8,7 @@
//#define __DEBUG //debug信息输出功能 //#define __DEBUG //debug信息输出功能
//#define __UDPSend //网络发送功能 //#define __UDPSend //网络发送功能
//#define __TCPSend // TCP发送 //#define __TCPSend // TCP发送
#define __ExportData // FTP发送
#define USB_BASLER_NEW_FW //使用basler定制固件 #define USB_BASLER_NEW_FW //使用basler定制固件
//#define IMM_PROCESS //拍照后立马处理,不等校验信号 //#define IMM_PROCESS //拍照后立马处理,不等校验信号
//#define IMM_FEED_BACK //处理完后立马反馈,不等校验信号 //#define IMM_FEED_BACK //处理完后立马反馈,不等校验信号

@ -5,6 +5,8 @@
#include <QFileInfoList> #include <QFileInfoList>
#include <QDir> #include <QDir>
#include <QFileInfo> #include <QFileInfo>
#include <QIODevice>
#include <QTextStream>
#include <QProcess> #include <QProcess>
#include <string> #include <string>
//#include <ftpManager.h> //#include <ftpManager.h>
@ -48,6 +50,8 @@ ExportDataThread::ExportDataThread(QObject* parent) : QThread(parent)
qDebug() << "First connect FTP failed because " << GetLastError(); qDebug() << "First connect FTP failed because " << GetLastError();
*/ */
readyToSendZip = false; readyToSendZip = false;
listToZip.clear();
tmpListToZip.clear();
} }
@ -103,8 +107,8 @@ void ExportDataThread::stop()
b_quit = true; b_quit = true;
_XMLExportDataInfo data; _XMLExportDataInfo data;
export_XMLData_Info_queue->put(data); export_XMLData_Info_queue->put(data);
InternetCloseHandle(hftp); //InternetCloseHandle(hftp);
InternetCloseHandle(hint); //InternetCloseHandle(hint);
} }
bool ExportDataThread::ConnectFtp(string *ip_, int *port_, string *username_, string *userpwd_) { bool ExportDataThread::ConnectFtp(string *ip_, int *port_, string *username_, string *userpwd_) {
@ -117,12 +121,20 @@ bool ExportDataThread::ConnectFtp(string *ip_, int *port_, string *username_, st
//string userName = "FTP2"; //string userName = "FTP2";
///* 密码 */ ///* 密码 */
//string pwd = "123"; //string pwd = "123";
cout << "222ftp ip =" << ip_ << "| prot =" << *port_ << endl; cout << "222ftp ip =" << *ip_ << "| prot =" << *port_ << endl;
cout << "222username =" << username_ << "| pwd =" << userpwd_ << endl; cout << "222username =" << *username_ << "| pwd =" << *userpwd_ << endl;
if (hftp != NULL) { if (hftp != NULL) {
InternetCloseHandle(hftp); InternetCloseHandle(hftp);
hftp = NULL; hftp = NULL;
} }
if (hint == NULL) {
hint = InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
if (hint == NULL) {
qDebug() << "Faile to open internet";
return false;
}
}
// 创建ftp连接 // 创建ftp连接
// hftp = InternetConnectA(hint, ftpServer.c_str(), port, userName.c_str(), pwd.c_str(), INTERNET_SERVICE_FTP, 0, 0); // hftp = InternetConnectA(hint, ftpServer.c_str(), port, userName.c_str(), pwd.c_str(), INTERNET_SERVICE_FTP, 0, 0);
hftp = InternetConnectA(hint, ip_->c_str(), *port_, username_->c_str(), userpwd_->c_str(), INTERNET_SERVICE_FTP, 0, 0); hftp = InternetConnectA(hint, ip_->c_str(), *port_, username_->c_str(), userpwd_->c_str(), INTERNET_SERVICE_FTP, 0, 0);
@ -285,9 +297,9 @@ int ExportDataThread::insertXMLNode(const char* xmlPath, _XMLExportDataInfo& dat
void ExportDataThread::run() void ExportDataThread::run()
{ {
while (!b_quit) { while (!b_quit || (b_quit && (!export_XMLData_Info_queue->isEmpty() || readyToSendZip || !tmpListToZip.isEmpty()))) {
_XMLExportDataInfo element; _XMLExportDataInfo element;
if (!export_XMLData_Info_queue->isEmpty()) {
export_XMLData_Info_queue->take(element); export_XMLData_Info_queue->take(element);
if (element.cameraId != -1 && flag) { if (element.cameraId != -1 && flag) {
@ -300,21 +312,38 @@ void ExportDataThread::run()
QString remotePath = "/tmp/" + xmlPath; QString remotePath = "/tmp/" + xmlPath;
ConnectServer(filePath, remotePath); ConnectServer(filePath, remotePath);
} }
}
if (b_quit && (!tmpListToZip.isEmpty())) {
listToZip += tmpListToZip;
tmpListToZip.clear();
readyToSendZip = true;
}
if (readyToSendZip) { if (readyToSendZip) {
QString remotePath; QString remotePath;
QStringList zipFilePaths; QStringList zipFilePaths;
QString zipFilePath; QStringList fileList;
QString okStr = "ok"; QString okStr = "OK";
QProcess process(0); QProcess process(0);
QDateTime now_ts = QDateTime::currentDateTime(); QDateTime now_ts = QDateTime::currentDateTime();
zipFilePaths.append(g_conf_path.save_pics_path + now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") + "ng.zip");
zipFilePaths.append(g_conf_path.save_pics_path + now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") + "ok.zip");
remotePath = listToZip[0].mid(listToZip[0].indexOf("image") - 1);
QStringList argsNg; QStringList argsNg;
QStringList argsOk; QStringList argsOk;
zipFilePaths.append(g_conf_path.save_pics_path + "/" + now_ts.toString("yyyy-MM-dd_HH-mm-ss_") + "ng.zip");
zipFilePaths.append(g_conf_path.save_pics_path + "/" + now_ts.toString("yyyy-MM-dd_HH-mm-ss_") + "ok.zip");
fileList.append(g_conf_path.save_pics_path + "/" + now_ts.toString("yyyy-MM-dd_HH-mm-ss_") + "ng.txt");
fileList.append(g_conf_path.save_pics_path + "/" + now_ts.toString("yyyy-MM-dd_HH-mm-ss_") + "ok.txt");
QFile fileNg(fileList[0]);
QFile fileOk(fileList[1]);
fileNg.open(QIODevice::WriteOnly);
fileOk.open(QIODevice::WriteOnly);
QTextStream ngStream(&fileNg);
QTextStream okStream(&fileOk);
argsNg.append("a"); argsNg.append("a");
argsNg.append(zipFilePaths[0]); argsNg.append(zipFilePaths[0]);
@ -326,11 +355,11 @@ void ExportDataThread::run()
strList = strList[strList.size() - 1].split("."); strList = strList[strList.size() - 1].split(".");
int index = listToZip[i].lastIndexOf("/\\", 0); int index = listToZip[i].lastIndexOf("/\\", 0);
if (strList[0] == okStr) { if (strList[0] == okStr) {
argsOk.append(listToZip[i]); okStream << listToZip[i] << endl;
} }
else { else {
argsNg.append(listToZip[i]); ngStream << listToZip[i] << endl;
} }
/* /*
QDir dir; QDir dir;
@ -340,16 +369,29 @@ void ExportDataThread::run()
} }
*/ */
} }
argsNg.append("-i@" + fileList[0]);
argsNg.append("-mx=3"); argsNg.append("-mx=3");
argsOk.append("-i@" + fileList[1]);
argsOk.append("-mx=3"); argsOk.append("-mx=3");
fileNg.close();
fileOk.close();
for (int i = 0; i < zipFilePaths.size(); i++) { for (int i = 0; i < zipFilePaths.size(); i++) {
process.start(QApplication::applicationDirPath() + "/7z.exe", i == 0 ? argsNg : argsOk); process.start(QApplication::applicationDirPath() + "/7z.exe", i == 0 ? argsNg : argsOk);
process.waitForStarted(); process.waitForStarted();
process.waitForFinished(); process.waitForFinished();
//QString out = QString::fromLocal8Bit(process.readAllStandardOutput());
//qDebug() << out;
remotePath = zipFilePaths[i].mid(zipFilePaths[i].indexOf("image") - 1);
qDebug() << remotePath;
ConnectServer(zipFilePaths[i], remotePath); ConnectServer(zipFilePaths[i], remotePath);
QFile file(zipFilePaths[i]);
file.remove();
} }
fileNg.remove();
fileOk.remove();
readyToSendZip = false; readyToSendZip = false;
} }
} }
@ -358,6 +400,11 @@ void ExportDataThread::run()
for (int index = 0; index < NumberOfSupportedCameras; index++) { for (int index = 0; index < NumberOfSupportedCameras; index++) {
pDocument[index]->~XMLDocument(); pDocument[index]->~XMLDocument();
} }
InternetCloseHandle(hftp);
InternetCloseHandle(hint);
hftp = NULL;
hint = NULL;
} }
//*****************************************已经调试好,没有理清逻辑前不要动********************** //*****************************************已经调试好,没有理清逻辑前不要动**********************
@ -434,18 +481,72 @@ void ExportDataThread::ConnectServer(QString srcPath, QString destPath) {
} }
} }
/*
bool compression_zip_file(const QString& selectFile2DirPath, const QString& savePath)
{
if (selectFile2DirPath.isEmpty() || savePath.isEmpty())
{
return false;
}
if (!QFile::exists(selectFile2DirPath) || !QFileInfo(savePath).isDir())
{
return false;
}
if (QFileInfo(selectFile2DirPath).isFile())///压缩的是一个文件
{
QString fileName = QFileInfo(selectFile2DirPath).baseName();
QString writerFilePath = savePath + "/" + fileName + ".zip";
QFile selectFile(selectFile2DirPath);
qint64 size = selectFile.size() / 1024 / 1024;
if (!selectFile.open(QIODevice::ReadOnly) || size > FILE_MAX_SIZE)
{
///打开文件失败或者大于1GB导致无法压缩的文件
return false;
}
QString addFileName = QFileInfo(selectFile2DirPath).fileName();
QZipWriter writer(writerFilePath);
writer.addFile(addFileName, selectFile.readAll());
selectFile.close();
return true;
}
else///压缩的是一个文件夹
{
QString zipRootFolder = selectFile2DirPath.mid(selectFile2DirPath.lastIndexOf("/") + 1);
QString selectDirUpDir = selectFile2DirPath.left(selectFile2DirPath.lastIndexOf("/"));
QString saveFilePath = savePath + "/" + zipRootFolder + ".zip";
QZipWriter writer(saveFilePath);
writer.addDirectory(zipRootFolder);
QFileInfoList fileList = ergodic_compression_file(&writer, selectDirUpDir, selectFile2DirPath);
writer.close();
if (0 == fileList.size())
return true;
return false;
}
}
*/
void ExportDataThread::GetDataFromSaveThread(QString filePath) { void ExportDataThread::GetDataFromSaveThread(QString filePath) {
QString remotePath; QString remotePath;
if (timer->isActive()) { if (timer->isActive()) {
if ((filePath.indexOf("ng_result") != -1) || (filePath.indexOf("ng") == -1)) {
tmpListToZip.append(filePath); tmpListToZip.append(filePath);
} }
}
else { else {
listToZip = tmpListToZip; listToZip = tmpListToZip;
tmpListToZip.clear(); tmpListToZip.clear();
if ((filePath.indexOf("ng_result") != -1) || (filePath.indexOf("ng") == -1)) {
tmpListToZip.append(filePath); tmpListToZip.append(filePath);
}
if (!listToZip.isEmpty()) {
readyToSendZip = true; readyToSendZip = true;
}
//remotePath = filePath.mid(filePath.indexOf("image") - 1); //remotePath = filePath.mid(filePath.indexOf("image") - 1);
//ConnectServer(filePath, remotePath); //ConnectServer(filePath, remotePath);
timer->start(1000 * 60 * 5); // 5min timer->start(1000 * 60 * 5); // 5min

Loading…
Cancel
Save