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());