From 5ad594a028993f0bbb9b83f8470b1d73902a800d Mon Sep 17 00:00:00 2001 From: metanoia1989 Date: Sun, 10 Jan 2021 23:51:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=8F=98=E9=94=81=E7=9A=84=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=EF=BC=8C=E8=A7=A3=E5=86=B3=E5=A4=9A=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MultiplethreadDownload/widget.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/MultiplethreadDownload/widget.cpp b/MultiplethreadDownload/widget.cpp index 7b2b2f8..e4cd93e 100644 --- a/MultiplethreadDownload/widget.cpp +++ b/MultiplethreadDownload/widget.cpp @@ -149,6 +149,7 @@ void Widget::multiDownload(const QString &url, qint64 fileSize, const QString &f vec[threadCount-1].second = fileSize; // 余数部分加入最后一个 qint64 bytesReceived = 0; // 下载接收的总字节数 + QMutex lock; // 任务队列 auto mapCaller = [&, this](const QPair& pair) -> qint64 { QEventLoop event; @@ -159,16 +160,15 @@ void Widget::multiDownload(const QString &url, qint64 fileSize, const QString &f request.setRawHeader("Range", QString("bytes=%1-%2").arg(pair.first).arg(pair.second).toLocal8Bit()); QNetworkReply *reply = mgr.get(request); qint64 writePos = pair.first; - QMutex lock; qDebug() << "开始下载数据:" << QString(" %1~%2 -> writePos Start %3") .arg(pair.first).arg(pair.second).arg(writePos); connect(reply, &QNetworkReply::readyRead, [&lock, &writePos, &file, &bytesReceived, reply](){ QByteArray data = reply->readAll(); { QMutexLocker mLock(&lock); - // qDebug() << "调整文件的数据指针:" << writePos; -// file.seek(writePos); -// file.write(data); +// qDebug() << "调整文件的数据指针:" << writePos; + file.seek(writePos); + file.write(data); bytesReceived += data.size(); qDebug() << QString("%1, %2, %3") .arg(writePos).arg(bytesReceived).arg(data.size());