我目前正面临一个关于QFtp的奇怪问题。我想从FTP服务器下载一堆文件,但是当我到了某个时候,在y上下载了x文件之后,完成了ftp->get()命令,文件被填充,但是SIGNAL commandFinished()没有发射,因此它没有下载其他文件。
这是我的代码:
void Ftp::commandFinished(int i, bool error)
{
if(ftp->currentCommand() == QFtp::Get)
{
if(error)
{
//blablabla-ERROR-blablabla
}
currentFile->close();
filesToDownload.pop_front();
processFileList();
}
/**Gestion de la commande Login (authentification de l'utilisateur)
*/
if(ftp->currentCommand() == QFtp::Login)
{//not utile here}
/**Gestion de la commande ConnectToHost (connexion au serveur)
*/
if (ftp->currentCommand() == QFtp::ConnectToHost)
{//not utile here}
/**Gestion de la commande List (téléchargement d'un fichier)
*/
if(ftp->currentCommand() == QFtp::List)
{
if(error)
{
//Nananana-FAIL-nanana
}
//!Tri des fichiers à télécharger en fonction de leur dernière date de modification
if (!filesToDownload.isEmpty())
{
currentPeripheral->setLastDownloadDate(newLastModifiedDate) ;
std::sort(filesToDownload.begin(),filesToDownload.end(),compareQUrlInfos);
processFileList();
}
}
}
void Ftp::processFileList()
{
QUrlInfo info;
if (filesToDownload.isEmpty())
{
//!Suicide de l'instance de Ftp
ftp->close();
disconnect(this,0,0,0);
this->deleteLater();
return ;
}
info = filesToDownload.first();
QDir dlDir(QString::number(currentPeripheral->getId()));
//!Si un fichier a été téléchargé, on déclenche son traitement
if (currentFile != nullptr)
{
emit(oneDownloadFinished(currentFile->fileName(),currentPeripheral));
delete currentFile;
currentFile = nullptr;
}
//!On crée un répertoire de téléchargement si nécessaire
if (!dlDir.exists())
{
dlDir.mkdir(".");
}
//!on crée le fichier qui contiendra le téléchargement
currentFile = new QFile(dlDir.filePath(info.name()));
if(!currentFile->open(QIODevice::WriteOnly))
{
delete currentFile;
currentFile = nullptr;
emit(writeToMonitoringConsole(QString("Erreur lors de la creation du fichier "+info.name()),"Error"));
return;
}
//Here I start (sometimes) a never ending fail
ftp->get(info.name(), currentFile);
}起初,我认为这是因为我提出了太多的请求,因此我被拒绝了,但是即使有了Sleep(2000),它也会阻塞。封锁出现得更快。我通常可以下载大约30个文件(当幸运的70,一旦我设法有200个!)使用Sleep(2000),我几乎无法下载2-3个文件。
是我搞错了吗?QFtp中有我没有发现的限制吗?还是别的什么?
编辑:发布后测试了一些东西,而当监视dataTransferProgress()信号时,令人吃惊的是问题文件已经完全下载(qDebug说"88928/88928"),但我从未输入commandFinished()。
我的插槽commandFinished()以这种方式链接到QFtp::commandFinished信号:
connect(ftp, SIGNAL(commandFinished(int,bool)), this, SLOT(commandFinished(int,bool)));发布于 2014-09-23 22:44:34
我也看到了同样的情况,我使用的FTPWidget类从未接收到GET请求的commandFinished。dataTransferProgress()信号可靠地报告了100%的下载量及其全部内容。
我使用了设置超时的方法,如下所示:
// Set up a single shot QTimer and connect it to a function to fix things
connect(m_lostFinishedTimer, SIGNAL(timeout()), this, SLOT(lostFinishedHack()));
// Start the timeout when the download data progress hits 100%
void FtpWidget::updateXferProgress(qint64 readBytes, qint64 totalBytes)
{
m_progressBar->setValue(progress_value(readBytes, totalBytes));
if (m_downloading && readBytes == totalBytes)
m_lostFinishedTimer->start();
}
// And don't forget to stop the timer if you do get a finish:
void FtpWidget::commandFinished(int id, bool error)
{
QFtp::Command cmd = m_ftp->currentCommand();
// ...
if (cmd == QFtp::Get)
{
m_lostFinishedTimer->stop();
//....
}
// ...
}当定时器连接到一个固定的例程时,基本上关闭下载文件,重新连接服务器,然后转到下一个文件。
void FtpWidget::lostFinishedHack()
{
if (!m_downloading)
return;
if (m_downloadFile)
{
DOUT("FtpWidget::lostFinshedHack() -- file: " << DS(m_downloadFile->fileName()) << "\n");
m_downloadFile->close();
delete m_downloadFile;
m_downloadFile = 0;
}
// just reconnect the thing
Connect(m_ftpServerName->text());
downloadNextFile();
}虽然我使用的超时时间要大得多,但这似乎已经足够了。
所以,在我把这个工作做好之后,我回去看看QFtp内部发生了什么。使用调试器在断点上添加一个print命令,我可以从。似乎正在发生的情况是,我使用的ftp服务器有时会以不同的顺序发送ftp回复。
当它起作用时,我的跟踪看上去就像:
FtpWidget::commandStarted(id: 265) -- Get
Function: QFtpPrivate::_q_piFtpReply(int, const QString &), code: 200, text "Operation successful"
Function: QFtpPrivate::_q_piFtpReply(int, const QString &), code: 213, text "135980"
Function: QFtpPrivate::_q_piFtpReply(int, const QString &), code: 200, text "Operation successful"
Function: QFtpPrivate::_q_piFtpReply(int, const QString &), code: 150, text "Opening BINARY connection for 000-23Sep2014_103527.jpg (135980 bytes)"
Function: QFtpPrivate::_q_piFtpReply(int, const QString &), code: 226, text "Operation successful"
FtpWidget::commandFinished(id: 265, error: 0) -- Get其中,FtpWidget::commandStarted和commandFinished是我的插槽,而_q_piFtpReply()是QFtp对象的私有部分,在这里我挂起了跟踪断点。
当它失败时,我得到:
FtpWidget::commandStarted(id: 256) -- Get
Function: QFtpPrivate::_q_piFtpReply(int, const QString &), code: 200, text "Operation successful"
Function: QFtpPrivate::_q_piFtpReply(int, const QString &), code: 213, text "135896"
Function: QFtpPrivate::_q_piFtpReply(int, const QString &), code: 200, text "Operation successful"
Function: QFtpPrivate::_q_piFtpReply(int, const QString &), code: 150, text "Opening BINARY connection for 000-23Sep2014_103525.jpg (135896 bytes)"
FtpWidget::lostFinshedHack()
FtpWidget::lostFinshedHack() -- file: C:/Users/sean/Temp/Snapshots5/000-23Sep2014_103525.jpg
FtpWidget::connect("ftp://root@169.254.1.180/")在200个回复代码之后获得150个回复代码似乎是个问题。因此,如果您查找FTP应答电码并查看qftp.cpp,它看起来在150个文件导致qftp实现状态机进入永久等待之后到达。据我所知,是我的ftp服务器把事情搞砸了,而不是qftp.cpp (也许我应该试试电线鲨鱼来确保)。
现在,我坚持解决这个问题的超时解决办法。
发布于 2016-07-06 10:50:42
我已经来不及回答了,但这可能会对其他人有所帮助。
在void Ftp::commandFinished(int i, bool error)中,检查QFtp::None选项总是更好的,因为如果您的最后一个命令是get,当调用commandFinished()时隙时,if (ftp->currentCommand() == QFtp::Get)将无法识别最后一个命令,因为您的当前命令可能不是get,因为它已经完成。
您还可以使用id作为if(ftp->currentCommand() == QFtp::Get || 1 == id)进行检查。
https://stackoverflow.com/questions/23698520
复制相似问题