FTP服務初始化時,考慮到重連、以及commandFinished信號信號執行完成置m_bCmdFinished 為true;
void ICore::connectFtpServer()
{if(g_pFile == nullptr){g_pFile = new QFile;}if(g_pFtp){g_pFtp->state();g_pFtp->abort();g_pFtp->deleteLater();g_pFtp = nullptr;m_bCmdFinished = false;}if (g_pFtp == nullptr){g_pFtp = new QFtp;
// connect(ICore::instance().getFtpPtr(), &QFtp::dataTransferProgress,[](qint64 readBytes, qint64 totalBytes){
// if(readBytes == totalBytes)
// {
// qDebug() << QString::fromLocal8Bit("數據傳輸結束,%1字節/%2字節").arg(readBytes).arg(totalBytes);
// }
// });connect(ICore::instance().getFtpPtr(), &QFtp::stateChanged,[](int state){cDebug(QString(u8"FTP狀態改變:%1!").arg(state));if(QFtp::LoggedIn != state){/*g_pFtp->state();g_pFtp->abort();g_pFtp->deleteLater();g_pFtp = nullptr;connectFtpServer();*/cDebug(u8"FTP服務器斷開,正在進行重連!");
// m_bCmdFinished = false;}
// else if(QFtp::Connected == state){
// cDebug(u8"FTP服務器連接成功!");
// m_bCmdFinished = true;
// }
// else if(QFtp::LoggedIn == state){
// cDebug(u8"FTP服務器登錄成功!");
// m_bCmdFinished = true;
// }});connect(ICore::instance().getFtpPtr(), &QFtp::listInfo,[](QUrlInfo urlInfo){if( !urlInfo.name().isEmpty()){m_ftpUrlList.insert(QString::fromLocal8Bit(urlInfo.name().toLatin1()));}});connect(g_pFtp, &QFtp::commandFinished,[](int ret, bool error){if (error){cDebug(QString::fromLocal8Bit(u8"正在操作FTP服務器,操作失敗:%1").arg(g_pFtp->errorString()));}else{if(g_pFtp->currentCommand() == QFtp::ConnectToHost){cDebug(tr(u8"正在連接FTP服務器,成功連接到服務器"));m_bCmdFinished = true;}else if (g_pFtp->currentCommand() == QFtp::Login) {cDebug(tr(u8"正在連接FTP服務器,登錄成功"));m_ftpUrlList.clear();g_pFtp->list();m_bFtpVaild = true;m_reConnectFtpTimer.start(10000);}else if (g_pFtp->currentCommand() == QFtp::Close) {cDebug(tr(u8"正在斷開FTP服務器連接,斷開成功"));m_ftpUrlList.clear();g_pFile->close();m_bFtpVaild = false;m_bCmdFinished = true;}else if (ICore::instance().getFtpPtr()->currentCommand() == QFtp::Cd){cDebug(tr(u8"正在操作FTP服務器,切換路徑成功!"));m_ftpUrlList.clear();g_pFtp->list();g_pFile->close();}else if (ICore::instance().getFtpPtr()->currentCommand() == QFtp::Put){cDebug(tr(u8"正在操作FTP服務器,文件上傳完成"));m_ftpUrlList.clear();g_pFtp->list();g_pFile->close();}else if (ICore::instance().getFtpPtr()->currentCommand() == QFtp::Mkdir){cDebug(tr(u8"正在操作FTP服務器,新建目錄完成"));m_ftpUrlList.clear();g_pFtp->list();}if (ICore::instance().getFtpPtr()->currentCommand() == QFtp::Get) {cDebug(tr(u8"正在操作FTP服務器,文件下載完成"));m_ftpUrlList.clear();g_pFtp->list();g_pFile->close();}else if(ICore::instance().getFtpPtr()->currentCommand() == QFtp::Rmdir || ICore::instance().getFtpPtr()->currentCommand() == QFtp::Remove){cDebug(tr(u8"正在操作FTP服務器,文件刪除完成!"));m_ftpUrlList.clear();g_pFtp->list();}else if(ICore::instance().getFtpPtr()->currentCommand() == QFtp::List){m_bCmdFinished = true;}}});QSettings settings("./data/ftpCfg.ini", QSettings::IniFormat);settings.setIniCodec(QTextCodec::codecForName("UTF-8"));QString hostName = settings.value("server/hostName").toString();uint port = settings.value("server/port").toString().toUInt();g_pFtp->connectToHost(hostName, port);//連接成功,執行下一條指令while(!m_bCmdFinished){QCoreApplication::processEvents();QThread::msleep(10);}resetCmdFinished();g_pFtp->login("anonymous","");//登錄成功,執行下一條指令while(!m_bCmdFinished){QCoreApplication::processEvents();QThread::msleep(10);}resetCmdFinished();g_pFtp->list();}
}
以UI界面上傳文件為例,
{//第一步:根據資源類型,判斷目錄是否存在,不存在則創建目錄ICore::instance().getFtpPtr()->cd(ICore::instance().getFTPRootDir());QString sDir = comboBox_type->currentText();QString sdir = QString::fromLatin1(comboBox_type->currentText().toLocal8Bit());if(!ICore::instance().isExist(sDir)){while(!ICore::instance().currentCmdIsFinished()){QCoreApplication::processEvents();QThread::msleep(10);}ICore::instance().resetCmdFinished();ICore::instance().getFtpPtr()->mkdir(sdir);}//第二步:切換進目錄while(!ICore::instance().currentCmdIsFinished()){QCoreApplication::processEvents();QThread::msleep(10);}ICore::instance().resetCmdFinished();ICore::instance().getFtpPtr()->cd(sdir);//第三步:上傳本地文件至ftp服務器while(!ICore::instance().currentCmdIsFinished()){QCoreApplication::processEvents();QThread::msleep(10);}ICore::instance().resetCmdFinished();QString fileName = m_pUpLoadRecord.filepath.split("/").takeLast();QString filename = QString::fromLatin1( m_pUpLoadRecord.filepath.toLocal8Bit());if(!ICore::instance().isExist(fileName)){ICore::instance().getLocalFile()->setFileName( lineEdit_filePath->property("fullFilePath").toString());if (ICore::instance().getLocalFile()->isOpen()) {ICore::instance().getLocalFile()->close();}ICore::instance().getLocalFile()->open(QIODevice::ReadOnly);ICore::instance().getFtpPtr()->put(ICore::instance().getLocalFile(), filename);}else{qDebug() << QString::fromLocal8Bit("ftp服務器目錄[%1]下,文件[%2]已存在").arg(sDir).arg(fileName);//第三點一步:刪除已有文件while(!ICore::instance().currentCmdIsFinished()){QCoreApplication::processEvents();QThread::msleep(10);}ICore::instance().resetCmdFinished();ICore::instance().getFtpPtr()->remove(filename);//第三點二步:上傳新文件while(!ICore::instance().currentCmdIsFinished()){QCoreApplication::processEvents();QThread::msleep(10);}ICore::instance().getLocalFile()->setFileName( lineEdit_filePath->property("fullFilePath").toString());if (ICore::instance().getLocalFile()->isOpen()) {ICore::instance().getLocalFile()->close();}ICore::instance().getLocalFile()->open(QIODevice::ReadOnly);ICore::instance().getFtpPtr()->put(ICore::instance().getLocalFile(), filename);}//第四步:返回根目錄while(!ICore::instance().currentCmdIsFinished()){QCoreApplication::processEvents();QThread::msleep(10);}ICore::instance().resetCmdFinished();ICore::instance().getFtpPtr()->cd(ICore::instance().getFTPRootDir());//第五步:等待返回根目錄后,上傳記錄到數據庫表while(!ICore::instance().currentCmdIsFinished()){QCoreApplication::processEvents();QThread::msleep(10);}ICore::instance().resetCmdFinished();.........
}
參見【QFtp在QT5版本下使用時遇到部分問題解決方案_qftp被斷開重連-CSDN博客】
參見【Qt 應用開發】輕松掌握Qt FTP 機制:實現高效文件傳輸-阿里云開發者社區