QNetworkManager上传文件到FTP崩溃 [英] QNetworkManager uploading file to FTP crash
问题描述
我试图上传一个简单的测试文本文件到FTP服务器。为了实现这一点,我使用 QNetworkAccessManager ,因为 QFtp 已在Qt 5.1中弃用。
我在程序目录中创建了一个test.txt文件,并使用 QFile 我打开它作为 QIODevice :: ReadWrite | QIODevice中::文本
。
问题是当我设置连接并告诉 QNetworkAccessManager 上传程序崩溃的文件(FTPConnectionTest不响应)。当我尝试使用外部FTP服务器或使用 FileZilla
创建的本地FTP服务器时,会发生这种情况。
$ b
我连接了回复发送的所有信号(函数: uploadFinish
, uploadProgress
uploadError
)但是没有任何反馈被捕获。 问题:这个问题是否存在FTP服务器的一面或者我在我的代码中做错了什么?
代码如下:
Main.cpp
#include< QCoreApplication>
#include< ftp.h>
main main(int argc,char * argv [])
{
QCoreApplication a(argc,argv);
ftp ftp;
返回a.exec();
}
ftp.cpp
#includeftp.h
#include< QtNetwork / QNetworkAccessManager>
#include< QtNetwork / QNetworkReply>
#include< QtNetwork / QNetworkRequest>
#include< QFile>
#include< QUrl>
#include< QDebug>
Ftp :: Ftp()
{
QFile文件(test.txt);
if(file.open(QIODevice :: ReadWrite | QIODevice :: Text)){
url = QUrl(ftp://127.0.0.1/test.txt);
url.setUserName(user);
url.setPassword(password);
qDebug()<< URL集<<网址;
QNetworkAccessManager * nam = new QNetworkAccessManager();
qDebug()<< nam set;
QNetworkReply * rep = nam-> put(QNetworkRequest(url),& file);
qDebug()<< 代表之后;
connect(rep,SIGNAL(finished()),this,SLOT(uploadFinish()));
connect(rep,SIGNAL(error(QNetworkReply :: NetworkError)),this,SLOT(uploadError(QNetworkReply :: NetworkError)));
connect(rep,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(uploadProgress(qint64,qint64)));
}
else qDebug()<< 未能打开;
}
void Ftp :: uploadFinish()
{
qDebug()<< 完成上传文件;
}
void Ftp :: uploadProgress(qint64 a,qint64 b)
{
qDebug()<< a<< /<< b;
}
void Ftp :: uploadError(QNetworkReply :: NetworkError状态)
{
qDebug()<< 状态<<州;
c> QNetworkAccessManager :: put 文档:
data 必须打开以供阅读当这个函数被调用并且必须保持有效,直到完成()信号发出这个回复。
您的 file
对象在构造函数完成执行时超出了范围,所以 QNetworkAccessManager
可能会尝试从已被删除的对象读取。您需要使用 file
类成员变量或使用 QFile * file = new QFile()
来创建它。
I am trying to upload a simple test text file to a FTP server. In order to achieve this I am using QNetworkAccessManager, since QFtp has been deprecated in Qt 5.1.
I created a test.txt file in the programs directory and using QFile I am opening it as QIODevice::ReadWrite | QIODevice::Text
.
The problem is when I set the connection and tell the QNetworkAccessManager to upload a file the program crashes ("FTPConnectionTest does not respond"). It happens both when I am trying to use an external FTP server or a local one created with FileZilla
.
I connected all signals emitted by the reply (functions: uploadFinish
, uploadProgress
, uploadError
) however no feedback is beeing captured.
Question: Is this problem lying on the side of FTP server or am I doing something wrong in my code? Code snipped below:
Main.cpp
#include <QCoreApplication>
#include <ftp.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Ftp ftp;
return a.exec();
}
ftp.cpp
#include "ftp.h"
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include <QFile>
#include <QUrl>
#include <QDebug>
Ftp::Ftp()
{
QFile file("test.txt");
if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
url = QUrl("ftp://127.0.0.1/test.txt");
url.setUserName("user");
url.setPassword("password");
qDebug() << "URL set" << url;
QNetworkAccessManager* nam = new QNetworkAccessManager();
qDebug() << "nam set";
QNetworkReply *rep = nam->put(QNetworkRequest(url), &file);
qDebug() << "after rep";
connect(rep, SIGNAL(finished()), this, SLOT(uploadFinish()));
connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(uploadError(QNetworkReply::NetworkError)));
connect(rep, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(uploadProgress(qint64,qint64)));
}
else qDebug() << "failed to open";
}
void Ftp::uploadFinish()
{
qDebug() << "finished uploading file";
}
void Ftp::uploadProgress(qint64 a, qint64 b)
{
qDebug() << a << "/" << b;
}
void Ftp::uploadError(QNetworkReply::NetworkError state)
{
qDebug() << "State" << state;
}
See the QNetworkAccessManager::put
documentation:
data must be opened for reading when this function is called and must remain valid until the finished() signal is emitted for this reply.
Your file
object falls out of scope when the constructor finishes execution, so QNetworkAccessManager
probably tries to read from object that is already deleted. You need to make file
a class member variable or create it using QFile* file = new QFile()
.
这篇关于QNetworkManager上传文件到FTP崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!