QNetworkAccessManager QNetworkRequest QNetworkReplyダウンロードネットワークリソースのネットワークモジュール()

A、レンダリング
ここに画像を挿入説明
2つのメインコード

#ifndef DOWNLOADWORK_H
#define DOWNLOADWORK_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QUrl>
#include <QDebug>
#include <QTime>
#include <QFile>
#include <QCoreApplication>
class DownLoadWork : public QObject
{
    Q_OBJECT
public:
    DownLoadWork(QObject *parent = nullptr,QUrl url = QUrl());
    void pause();
    void start();
    void resume();
private slots:
    void on_readyRead();
    void on_error(QNetworkReply::NetworkError error);
    void on_finished();
    void on_downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
signals:
    void progress(qint64 bytesReceived, qint64 bytesTotal,qint64 speed);
    void sig_finished();
private:
    QNetworkAccessManager *pNetWorkMgr;
    qint64 m_bytesReceived;
    QTime m_timeAdd;
    QUrl m_url;
    QNetworkReply *pNetReply;
    qint64 m_fileBytes;
    QString m_filePath;
    QFile m_file;
};

#endif // DOWNLOADWORK_H

#include "downloadwork.h"

DownLoadWork::DownLoadWork(QObject *parent, QUrl url):
    QObject(parent),
    m_bytesReceived(0),
    m_url(url),
    pNetWorkMgr(new QNetworkAccessManager),
    m_fileBytes(0)
{
    start();
}

void DownLoadWork::pause()
{
    if(nullptr != pNetReply){
        disconnect(pNetReply,SIGNAL(readyRead()),this,SLOT(on_readyRead()));
        disconnect(pNetReply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(on_error(QNetworkReply::NetworkError)));
        disconnect(pNetReply,SIGNAL(finished()),this,SLOT(on_finished()));
        disconnect(pNetReply,SIGNAL(downloadProgress(qint64, qint64)),this,SLOT(on_downloadProgress(qint64, qint64)));
        pNetReply->abort();
        pNetReply->deleteLater();
        pNetReply = nullptr;
    }
    m_file.close();
}

void DownLoadWork::start()
{
    m_filePath = QString(QCoreApplication::applicationDirPath()+"/"+m_url.toString().split("/").last()).replace("\\","/");
    m_file.setFileName(m_filePath);
    if(m_file.exists())
        m_file.remove();
    if(!m_file.open(QIODevice::WriteOnly | QIODevice::Append)){
        return;
    }

    QNetworkRequest req;
    req.setUrl(m_url);

    pNetReply = pNetWorkMgr->get(req);

    connect(pNetReply,SIGNAL(readyRead()),this,SLOT(on_readyRead()));
    connect(pNetReply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(on_error(QNetworkReply::NetworkError)));
    connect(pNetReply,SIGNAL(finished()),this,SLOT(on_finished()));
    connect(pNetReply,SIGNAL(downloadProgress(qint64, qint64)),this,SLOT(on_downloadProgress(qint64, qint64)));

    m_timeAdd = QTime::currentTime();
}

void DownLoadWork::resume()
{
    m_filePath = QString(QCoreApplication::applicationDirPath()+"/"+m_url.toString().split("/").last()).replace("\\","/");
//    qDebug()<<"m_filePath"<<m_filePath;

    m_file.setFileName(m_filePath);
    if(!m_file.open(QIODevice::WriteOnly | QIODevice::Append)){
        return;
    }
    m_fileBytes = m_file.size();

    QNetworkRequest req;
    req.setUrl(m_url);
    QString strRange = QString("bytes=%1-").arg(m_file.size());
    req.setRawHeader("Range", strRange.toLatin1());

    pNetReply = pNetWorkMgr->get(req);

    connect(pNetReply,SIGNAL(readyRead()),this,SLOT(on_readyRead()));
    connect(pNetReply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(on_error(QNetworkReply::NetworkError)));
    connect(pNetReply,SIGNAL(finished()),this,SLOT(on_finished()));
    connect(pNetReply,SIGNAL(downloadProgress(qint64, qint64)),this,SLOT(on_downloadProgress(qint64, qint64)));

    m_timeAdd = QTime::currentTime();
}
void DownLoadWork::on_readyRead()
{
    m_file.write(pNetReply->readAll());
}
void DownLoadWork::on_error(QNetworkReply::NetworkError error)
{
    qDebug()<<"on_error"<<error;
}

void DownLoadWork::on_finished()
{
    m_file.close();
    emit sig_finished();
}

void DownLoadWork::on_downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
    qint64 speed = m_timeAdd.elapsed() == 0 ? 0:1000*(bytesReceived - m_bytesReceived)/m_timeAdd.elapsed();
    emit progress(bytesReceived+m_fileBytes,bytesTotal+m_fileBytes,speed);
    m_timeAdd.restart();
    m_bytesReceived = bytesReceived;
}

第三に、要約
1.プロジェクトを理解し、レスポンスクラスのクラスのように、ネットワーク管理要求のクラスに焦点を当て
2.サポートブレークポイントのダウンロード機能が要求を復元するときにセットQStringのstrRange =のQString(「バイト。 =%1 - 」)引数(m_file.size());
req.setRawHeader(「レンジ」、strRange.toLatin1());
3に対応する開口部のために、ファイルを閉じる開閉操作することを忘れない
覚えに、4書き込みファイルダウンロード時間のかかるプロセスが属しインタフェースの更新の影響を防止するため、サブスレッド処理、

公開された30元の記事 ウォンの賞賛1 ビュー1148

おすすめ

転載: blog.csdn.net/u010906468/article/details/104770479