QT中使用Http下载文件夹下的所有文件

   QT中基于HTTP协议的网络文件下载是无法像FTP一样直接拉取文件夹下所有文件的列表的,但是由于我的项目中是给了我一个保存所有文件的QStringList,因此我们也可做到下载文件夹中的所有文件(必须要获取到所有文件的列表才能使用Http下载),不多说上代码。
#ifndef DOWNLOADTHREAD_H
#define DOWNLOADTHREAD_H

#include 
#include 

class DownLoadThread : public QObject
{
    Q_OBJECT
public:
    explicit DownLoadThread(QString FilePath, QObject *parent = nullptr);
    void DownloadPic(QStringList t_pic);
private slots:
    void on_finished();
    void on_readyRead();


private:
    QNetworkAccessManager *myNetworkManager;
    QNetworkRequest myNetworkRequest;
    QNetworkReply *myNetworkReply;
    QFile *myDownloadFile;
    QString myFilePath;
    QString myPic;
};

#endif // DOWNLOADTHREAD_H

.cpp文件

/*!
 * \brief DownLoadThread::DownloadPic 下载图片
 * \param t_pic 图片列表
 * \param
 */
void DownLoadThread::DownloadPic(QStringList t_pic)
{
    qDebug() << QThread::currentThreadId() <<__FILE__<<__LINE__;
    //qDebug()<open(QIODevice::WriteOnly))
        {
            qDebug()<<"打开失败";
        }
        myNetworkRequest.setUrl(newUrl);
        myNetworkReply = myNetworkManager->get(myNetworkRequest);
        connect(myNetworkReply,SIGNAL(finished()),this,SLOT(on_finished()));
        connect(myNetworkReply,SIGNAL(readyRead()),this,SLOT(on_readyRead()));
        QEventLoop eventLoop;
        connect(myNetworkManager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
        eventLoop.exec();
    }
    qDebug()<close();
    delete myDownloadFile;
    myDownloadFile = Q_NULLPTR;
    myNetworkReply->deleteLater();
    myNetworkReply = Q_NULLPTR;
    qDebug()<write(myNetworkReply->readAll());
}

你可能感兴趣的:(QT中使用Http下载文件夹下的所有文件)