哭泣!多线程下载成功!

This commit is contained in:
metanoia1989 2021-02-16 02:08:00 +08:00
parent 8379d7922a
commit 2d3d35f951
2 changed files with 48 additions and 25 deletions

View File

@ -16,10 +16,13 @@ void DownloadWorker::setIdentifier(int identifier)
this->identifier = identifier;
}
void DownloadWorker::setParamter(const QString &url, QPair<qint64, qint64> range)
void DownloadWorker::setParamter(const QString &url, QPair<qint64, qint64> range, QFile *file)
{
this->url = url;
this->range = range;
// this->range = range;
this->startPos = range.first;
this->endPos = range.second;
this->file = file;
}
void DownloadWorker::doWork()
@ -28,14 +31,15 @@ void DownloadWorker::doWork()
QNetworkRequest request;
request.setUrl(url);
request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
request.setRawHeader("Range", QString("bytes=%1-%2").arg(range.first)
.arg(range.second).toLocal8Bit());
request.setRawHeader("Range", QString("bytes=%1-%2").arg(startPos)
.arg(endPos).toLocal8Bit());
// QNetworkReply *reply = mgr->get(request);
// QNetworkReply *reply = mgr->get(request);
reply = mgr->get(request);
qint64 writePos = range.first;
// qDebug() << "开始下载数据:" << QString(" %1~%2 -> writePos Start %3")
// .arg(range.first).arg(range.second).arg(writePos);
qDebug() << "开始下载数据:" << QString(" %1~%2 -> writePos Start %3")
.arg(range.first).arg(range.second).arg(writePos);
.arg(startPos).arg(endPos).arg(receivedPos);
// connect(reply, &QNetworkReply::readyRead, [reply, this](){
//// qDebug() << "测试是否触发,午安啦啦啦";
// QByteArray data = reply->readAll();
@ -50,18 +54,31 @@ void DownloadWorker::doWork()
connect(reply, &QNetworkReply::finished, mgr, &QNetworkAccessManager::deleteLater);
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::readyRead, this, &DownloadWorker::dataReady);
connect(reply, &QNetworkReply::finished, this, &DownloadWorker::slotFinish);
}
void DownloadWorker::dataReady()
{
qDebug() << QString("测试是否执行: %1 %2").arg(startPos).arg(receivedPos);
QByteArray data = reply->readAll();
qDebug() << "获取到了数据" << " " << data.size();
emit resultReady(identifier, data);
auto controller = qobject_cast<DownloadController*>(parent());
QMetaObject::invokeMethod(controller, "handleResults", Qt::DirectConnection,
Q_ARG(int, identifier), Q_ARG(QByteArray, data));
// QMetaObject::invokeMethod(controller, "handleResults", Qt::QueuedConnection,
// emit resultReady(identifier, data);
// auto controller = qobject_cast<DownloadController*>(parent());
// QMetaObject::invokeMethod(controller, "handleResults", Qt::DirectConnection,
// Q_ARG(int, identifier), Q_ARG(QByteArray, data));
// QMetaObject::invokeMethod(controller, "handleResults", Qt::QueuedConnection,
file->seek(startPos + receivedPos);
file->write(data);
qDebug() << QString("%1, %2, %3").arg(startPos).arg(receivedPos).arg(data.size());
receivedPos += data.size();
// Q_ARG(int, identifier), Q_ARG(QByteArray, data));
}
void DownloadWorker::slotFinish()
{
file->flush();
qDebug() << "数据块下载完毕:" << QString(" %1~%2 -> writePos Start %3")
.arg(startPos).arg(endPos).arg(receivedPos);
}
@ -109,15 +126,15 @@ void DownloadController::startDownload(const QString &url, qint64 fileSize)
qDebug() << QString("查看分段下载:%1 %2").arg(ranges.size()).arg(writePosList.size());
// return;
// 打开文件
// QFile file;
file.setFileName(filename);
if (file.exists())
file.remove();
if (!file.open(QIODevice::WriteOnly)) {
emit errorOccur(file.errorString());
file = new QFile;
file->setFileName(filename);
if (file->exists())
file->remove();
if (!file->open(QIODevice::WriteOnly)) {
emit errorOccur(file->errorString());
return;
}
file.resize(fileSize);
file->resize(fileSize);
// 创建下载线程
// qint64 bytesReceived = 0;
@ -126,8 +143,9 @@ void DownloadController::startDownload(const QString &url, qint64 fileSize)
auto worker = new DownloadWorker(this);
auto range = ranges.at(i);
worker->setIdentifier(i);
worker->setParamter(url, range);
connect(worker, &DownloadWorker::resultReady, this, &DownloadController::handleResults);
worker->setParamter(url, range, file);
// connect(worker, &DownloadWorker::resultReady, this, &DownloadController::handleResults);
worker->doWork();
}
}
@ -158,8 +176,8 @@ void DownloadController::handleResults(int identifier, QByteArray data)
qDebug() << QString("测试哈哈哈: %1 --- %2").arg(writePosList.size()).arg(identifier);
QMutexLocker lock(&mutex);
qint64 writePos = writePosList.at(identifier);
file.seek(writePos);
file.write(data);
file->seek(writePos);
file->write(data);
qDebug() << QString("%1, %2, %3").arg(writePos).arg(bytesReceived).arg(data.size());
writePos += data.size();
writePosList.replace(identifier, writePos);

View File

@ -13,11 +13,12 @@ class DownloadWorker : public QObject
public:
explicit DownloadWorker(QObject *parent = nullptr);
void setIdentifier(int identifier);
void setParamter(const QString &url, QPair<qint64, qint64> range);
void setParamter(const QString &url, QPair<qint64, qint64> range, QFile *flle);
public slots:
void doWork();
void dataReady();
void slotFinish();
signals:
void resultReady(int identifier, QByteArray data);
@ -27,8 +28,12 @@ signals:
private:
int identifier;
QString url;
QPair<qint64, qint64> range;
// QPair<qint64, qint64> range;
qint64 startPos;
qint64 endPos;
qint64 receivedPos = 0;
QNetworkReply *reply;
QFile *file;
};
class DownloadController : public QObject
@ -56,7 +61,7 @@ private:
int threadNum;
QString filename;
QVector<QPair<qint64, qint64>> ranges;
QFile file;
QFile *file;
qint64 bytesReceived;
QVector<qint64> writePosList;
QMutex mutex;