简体   繁体   English

如何使用Qt5和QNetworkAccessManager下载文件

[英]How to Download file using Qt5 with QNetworkAccessManager

I need a very simple console application to FTP into the server and get some file , I try with QNetworkAccessManager , and it seems like not working and getting nothing. 我需要一个非常简单的控制台应用程序以FTP进入服务器并获取一些文件,我尝试使用QNetworkAccessManager,但似乎无法正常工作且什么也没得到。

Here is my code , 这是我的代码,

ftp.h ftp.h

    #ifndef FTP_H
#define FTP_H
#include <string>
#include <vector>

#include <qstring.h>
#include <qfile.h>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include "qdebug.h"

#include <QCoreApplication>
#include <QDebug>
#include <string>
#include <iostream>
#include <sstream>
#include <QTextStream>
#include <QObject>

class ftp
{
//    Q_OBJECT
public:
    ftp();

private:
    QNetworkAccessManager *manager;
    QNetworkReply *reply;

private slots:
    void slotError(QNetworkReply::NetworkError code);
    void replyFinished(QNetworkReply *reply);
};

#endif // FTP_H

ftp.cpp ftp.cpp

#include "ftp.h"
#include "QDebug"

#include <qstring.h>
#include <qfile.h>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include "qdebug.h"

#include <QCoreApplication>
#include <QDebug>
#include <string>
#include <iostream>
#include <sstream>
#include <QTextStream>
#include <QObject>

using namespace std;

ftp::ftp()
{
    qDebug() << "ZSS Server Program Running Now ...";

    QTextStream cin(stdin);
    QString username;
    QString password;
    QString ip;
    QString port;

    cout << "Username : ";
    username = cin.readLine();

    cout << "Password : ";
    password = cin.readLine();

    cout << "IP Address : ";
    ip = cin.readLine();

    cout << "Port Number : ";
    port = cin.readLine();

    QUrl url;
    url.setScheme("ftp");
    url.setHost("10.228.7.74");
    url.setPath("/try/hello.txt");
    url.setPort(21);
    url.setUserName("ftpTest");
    url.setPassword("ftpTest");
    QNetworkRequest request(url);
    //request.setUrl(url);

    qDebug() << url;

    //cout << "Downloading File...";

    manager = new QNetworkAccessManager;
    reply = manager->get(request);
    QByteArray buffer = reply->readAll();
    qDebug() << buffer;

//    connect(reply , SIGNAL(error(QNetworkReply::NetworkError)), this,SLOT(slotError(QNetworkReply::NetworkError)));
//    connect(manager, SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));
}

void ftp::slotError(QNetworkReply::NetworkError code)
{
    qDebug() << reply->errorString();
}

void ftp::replyFinished(QNetworkReply *reply)
{
//    QTextCodec *codec = QTextCodec::codecForName("utf8");
//    QString all = codectoUniCode(reply->readAll());
//    cout >> all;
//    reply->deleteLater();
}

After I run my code , inside my folder , I can't see any file is there. 运行代码后,在我的文件夹内,看不到任何文件。 So I qDebug() out all the response and buffer , I found that the buffer and response is empty , is there any part I was missing or doing other things wrong ? 所以我qDebug()取出了所有响应和缓冲区,我发现缓冲区和响应为空,是否有我缺少的任何部分或做错了其他事情? Can I get a simple example for this ?? 我可以为此得到一个简单的例子吗?

Here is the console output screenshots, 这是控制台输出的屏幕截图,

代码输出

I qDebug() the buffer , but the buffer doesn't have anythings on it , I create this ftp local server by using 3CDaemon , and I was point to the correct folder and my ftp server is running , I can use command prompt to ftp inside and get the file , just can't make it by using Qt. 我使用qDebug()缓冲区,但是缓冲区上没有任何内容,我使用3CDaemon创建了该ftp本地服务器,并且我指向了正确的文件夹并且我的ftp服务器正在运行,我可以使用命令提示符进行ftp在内部并获取文件,只是无法使用Qt使其生成。

Please help me , this have been trouble me for couple of days..Thanks.. 请帮助我,这已经困扰了我两天了。谢谢。

QNetworkAccessManager::get is an asynchronous function. QNetworkAccessManager::get是一个异步函数。 You can use the QNetworkReply it returns to monitor the progress of the download, but isn't ready to read immediately. 您可以使用它返回的QNetworkReply来监视下载进度,但还不能立即读取。 You have to wait until the finished signal is emitted (either by the QNetworkAccessManager or the QNetworkReply ) before you can read data from it. 您必须等到发出finished信号(由QNetworkAccessManagerQNetworkReply发出)之后,才能从中读取数据。

If you really want the download to be synchronous, you can spin your own local event loop until the download finishes. 如果您确实希望下载是同步的,则可以旋转自己的本地事件循环,直到下载完成。 For example 例如

QEventLoop loop;
QNetworkAccessManager mgr;
connect(&mgr, &QNetworkAccessManager::finished, &loop, &QEventLoop::quit);

QNetworkReply *reply = mgr.get(...);
loop.exec();
qDebug() << reply.readAll();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM