// 获取当前的时间戳,设置下载的临时文件名称
QDateTime dateTime =
QDateTime::currentDateTime();
QString date = dateTime.toString(
"yyyy-MM-dd-hh-mm-ss-zzz");
m_strFileName = QString(
"E:/%1.tmp").arg(date);
connect(this, SIGNAL(finished(QNetworkReply *)),
this, SLOT(replyFinished(QNetworkReply *
)));
}
DownloadNetworkManager::~
DownloadNetworkManager()
{
// 终止下载
if (m_pReply !=
NULL)
{
m_pReply->
abort();
m_pReply->
deleteLater();
}
}
// 设置URL及消息头,开始请求
void DownloadNetworkManager::execute()
{
m_url = QUrl(
"http://192.168.*.*/download/2.0.0.zip");
QNetworkRequest request;
request.setUrl(m_url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/zip");
connect(this, SIGNAL(authenticationRequired(QNetworkReply *, QAuthenticator *)),
this, SLOT(onAuthenticationRequest(QNetworkReply *, QAuthenticator *
)));
m_pReply =
get(request);
connect(m_pReply, SIGNAL(downloadProgress(qint64, qint64)), this, SIGNAL(downloadProgress(qint64, qint64)));
connect(m_pReply, SIGNAL(readyRead()), this, SLOT(readyRead()));
}
void DownloadNetworkManager::replyFinished(QNetworkReply *
reply)
{
// 获取响应的信息,状态码为200表示正常
QVariant statusCode = reply->
attribute(QNetworkRequest::HttpStatusCodeAttribute);
// 无错误返回
if (reply->error() ==
QNetworkReply::NoError)
{
// 重命名临时文件
QFileInfo fileInfo(m_strFileName);
QFileInfo newFileInfo = fileInfo.absolutePath() +
m_url.fileName();
QDir dir;
if (dir.exists(fileInfo.absolutePath()))
{
if (newFileInfo.exists())
newFileInfo.dir().remove(newFileInfo.fileName());
QFile::rename(m_strFileName, newFileInfo.absoluteFilePath());
}
}
else
{
QString strError = reply->
errorString();
qDebug() <<
"Error:" <<
strError;
}
emit replyFinished(statusCode.toInt());
}
// 用户认证
void DownloadNetworkManager::onAuthenticationRequest(QNetworkReply *reply, QAuthenticator *
authenticator)
{
QByteArray password;
password.append("123456");
password =
QByteArray::fromBase64(password);
QString strPassword(password);
authenticator->setUser(
"wang");
authenticator->
setPassword(strPassword);
}
// 本地写文件
void DownloadNetworkManager::readyRead()
{
QFileInfo fileInfo(m_strFileName);
QFileInfo newFileInfo = fileInfo.absolutePath() +
m_url.fileName();
QString strFileName =
newFileInfo.absoluteFilePath();
emit fileName(strFileName);
// 写文件-形式为追加
QFile file(m_strFileName);
if (file.open(QIODevice::Append))
file.write(m_pReply->
readAll());
file.close();
}
使用
调用download()接口开始下载,关联downloadProgress信号和槽,可以实时获取下载大小、速度、剩余时间等信息。
// 开始下载
void MainWindow::download()
{
if (m_pNetworkManager == NULL)
{
m_pNetworkManager = new DownloadNetworkManager(this);
connect(m_pNetworkManager, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgress(qint64, qint64)), Qt::QueuedConnection);
connect(m_pNetworkManager, SIGNAL(replyFinished(int)), this, SLOT(replyFinished(int)), Qt::QueuedConnection);
connect(m_pNetworkManager, SIGNAL(fileName(QString)), m_pFileInfoLabel, SLOT(setText(QString)), Qt::QueuedConnection);
}
m_pNetworkManager->execute();
downloadTime.start();
}
// 计算下载大小、速度、剩余时间
void MainWindow::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
// 总时间
int nTime = downloadTime.elapsed();
// 本次下载所用时间
nTime -= m_nTime;
// 下载速度
double dBytesSpeed = (bytesReceived * 1000.0) / nTime;
double dSpeed = dBytesSpeed;
//剩余时间
qint64 leftBytes = (bytesTotal - bytesReceived);
double dLeftTime = (leftBytes * 1.0) / dBytesSpeed;
m_pSpeedInfoLabel->setText(speed(dSpeed));
m_pLeftTimeInfoLabel->setText(timeFormat(qCeil(dLeftTime)));
m_pFileSizeInfoLabel->setText(size(bytesTotal));
m_pDownloadInfoLabel->setText(size(bytesReceived));
m_pProgressBar->setMaximum(bytesTotal);
m_pProgressBar->setValue(bytesReceived);
// 获取上一次的时间
m_nTime = nTime;
}
// 下载完成
void MainWindow::replyFinished(int statusCode)
{
m_nStatusCode = statusCode;
QString strStatus = (statusCode == 200) ? QStringLiteral("下载成功") : QStringLiteral("下载失败");
m_pStatusLabel->setText(strStatus);
}
http://blog.csdn.net/liang19890820/article/details/50814339
Qt之HTTP上传/下载(继承QNetworkAccessManager,包括使用了authenticationRequired认证信号)
标签: