From 1ed252b5ffddb11bc5c464846990e7ca8f3f9881 Mon Sep 17 00:00:00 2001 From: Flamingo Date: Mon, 8 Apr 2024 21:57:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3FTP=E4=BC=A0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=9C=89=E6=97=B6=E5=80=99=E8=B7=AF=E5=BE=84=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E5=88=9B=E5=BB=BA=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cigarette/exportData.cpp | 104 ++++++++++++++------------------------- Cigarette/exportData.h | 1 + 2 files changed, 39 insertions(+), 66 deletions(-) diff --git a/Cigarette/exportData.cpp b/Cigarette/exportData.cpp index 40565d9..b8edf3f 100644 --- a/Cigarette/exportData.cpp +++ b/Cigarette/exportData.cpp @@ -52,6 +52,8 @@ ExportDataThread::ExportDataThread(QObject* parent) : QThread(parent) readyToSendZip = false; listToZip.clear(); tmpListToZip.clear(); + hftp = NULL; + hint = NULL; } @@ -62,8 +64,8 @@ void ExportDataThread::init(string ip_, int port_, string username_, string user port = port_; username = username_; userpwd = userpwd_; - cout << "333333ftp ip =" << ip << "| prot =" << port << endl; - cout << "333333username =" << username << "| pwd =" << userpwd << endl; + cout << "ftp ip =" << ip << "| prot =" << port << endl; + cout << "username =" << username << "| pwd =" << userpwd << endl; b_quit = false; flag = false; @@ -86,8 +88,8 @@ void ExportDataThread::init(string ip_, int port_, string username_, string user pDocument[index]->InsertFirstChild(declaration); } } + //init(ip, port, username, userpwd); - cout << "11111111111111"; hint = InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0); if (hint == NULL) { return; @@ -111,6 +113,23 @@ void ExportDataThread::stop() //InternetCloseHandle(hint); } +bool ExportDataThread::FtpConnectionTest () { + int retryCount = 3; + if ((hftp == NULL) || (!FtpSetCurrentDirectoryA(hftp, "/"))) { + while (!ConnectFtp(&ip, &port, &username, &userpwd)) { + if (retryCount-- == 0) { + break; + } + QTime delayTime = QTime::currentTime().addMSecs(100); //100ms + while (QTime::currentTime() < delayTime) { + QCoreApplication::processEvents(QEventLoop::AllEvents, 100); + } + } + } + + return (retryCount > 0); +} + bool ExportDataThread::ConnectFtp(string *ip_, int *port_, string *username_, string *userpwd_) { //// FTP地址 @@ -121,20 +140,12 @@ bool ExportDataThread::ConnectFtp(string *ip_, int *port_, string *username_, st //string userName = "FTP2"; ///* 密码 */ //string pwd = "123"; - cout << "222ftp ip =" << *ip_ << "| prot =" << *port_ << endl; - cout << "222username =" << *username_ << "| pwd =" << *userpwd_ << endl; + if (hftp != NULL) { InternetCloseHandle(hftp); 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连接 // 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); @@ -385,7 +396,7 @@ void ExportDataThread::run() //QString out = QString::fromLocal8Bit(process.readAllStandardOutput()); //qDebug() << out; remotePath = zipFilePaths[i].mid(zipFilePaths[i].indexOf("image") - 1); - qDebug() << remotePath; + //qDebug() << remotePath; ConnectServer(zipFilePaths[i], remotePath); QFile file(zipFilePaths[i]); file.remove(); @@ -401,10 +412,14 @@ void ExportDataThread::run() pDocument[index]->~XMLDocument(); } - InternetCloseHandle(hftp); - InternetCloseHandle(hint); - hftp = NULL; - hint = NULL; + if (hftp != NULL) { + InternetCloseHandle(hftp); + hftp = NULL; + } + if (hftp != NULL) { + InternetCloseHandle(hint); + hint = NULL; + } } //*****************************************已经调试好,没有理清逻辑前不要动********************** @@ -448,17 +463,22 @@ void ExportDataThread::ConnectServer(QString srcPath, QString destPath) { size = files.size(); } + if (!FtpConnectionTest()) { + qDebug() << "Failed to connect FTP server"; + return; + } + // 上传文件源为一个文件 if (size == 0) { int pos = remotePath.find_last_of('/'); string destFileName = remotePath.substr(pos + 1); string tempPath = remotePath.substr(0, pos + 1); - check_save_dir(tempPath); if (!InternetGetConnectedState(NULL, 0)) { qDebug() << "计算机未连接到互联网"; } else { + check_save_dir(tempPath); while (!FtpPutFileA(hftp, filePath.c_str(), remotePath.c_str(), FTP_TRANSFER_TYPE_BINARY, 0)) { qDebug() << "ftp put file " << filePath.c_str() << "failed because" << GetLastError(); if (!ConnectFtp(&ip, &port, &username, &userpwd)) @@ -481,54 +501,6 @@ 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) { QString remotePath; diff --git a/Cigarette/exportData.h b/Cigarette/exportData.h index 8143ff4..cf146e4 100644 --- a/Cigarette/exportData.h +++ b/Cigarette/exportData.h @@ -127,6 +127,7 @@ public: //int insertXMLNode(const char* xmlPath, _ExportDataInfo& data); int insertXMLNode(const char* xmlPath, _XMLExportDataInfo& data); bool ConnectFtp(string *ip_, int *port_, string *username_, string *userpwd_); + bool FtpConnectionTest(); void ConnectServer(QString srcPath, QString destPath); void GetFiles(string path, std::vector& files);