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 机制:实现高效文件传输-阿里云开发者社区