#include "downloadfile.h" #include #include #include "GlobalSetting.h" #include QMutex downloadMutex; IDownloadFile *IDownloadFile::m_downloadFile = NULL; IDownloadFile::IDownloadFile() { httpRequestAborted = false; reply = NULL; file = NULL; downloadNum = 0; downloadFlag = false; connect(this, SIGNAL(startDownloadFile()), this, SLOT(startDownload())); } IDownloadFile *IDownloadFile::Instance() { if (!m_downloadFile) m_downloadFile = new IDownloadFile(); return m_downloadFile; } void IDownloadFile::addDownloadFile(QUrl url,QString filename,int fid) { downloadNum++; if(downloadNum >= 20) { downloadNum = 19; return; } downFileMsg[downloadNum].url = url; downFileMsg[downloadNum].filename = filename; downFileMsg[downloadNum].fid = fid; } void IDownloadFile::startDownload() { if(downloadNum > 0) { downloadFile(downFileMsg[downloadNum].url,downFileMsg[downloadNum].filename,downFileMsg[downloadNum].fid); downloadNum--; } else { emit downloadFileEnd(); } } /********************************************** * 作者姓名:袁爱玲 * 函数介绍: * 定义的参数含义说明 * 输入参数: * 输出参数:void * 返回值 :void **********************************************/ void IDownloadFile::downloadFile(QUrl url,QString filename,int fid) { QMutexLocker locker(&downloadMutex); if(downfileMap.contains(fid)) { return; } downfileMap.insert(fid, filename); httpRequestAborted = false; reply = NULL; file = NULL; this->url = url; this->fid = fid; this->filename = filename; QDir my_dir; QFileInfo my_info(filename); if(!my_dir.exists(my_info.absoluteDir().path())) { my_dir.mkpath(my_info.absoluteDir().path()); } file = new QFile(filename); if(!file->open(QIODevice::WriteOnly)) { qDebug()<<"Unable to save the file"<< filename << file->errorString(); delete file; file = 0; return; } reply = qnam.get(QNetworkRequest(url)); connect(reply, SIGNAL(finished()), this, SLOT(httpFinished())); connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(errorReturn(QNetworkReply::NetworkError))); connect(reply, SIGNAL(readyRead()), this, SLOT(httpReadyRead())); } void IDownloadFile::errorReturn(QNetworkReply::NetworkError error) { qDebug()<<"errorReturn"<canReadLine(); reply->abort(); reply->deleteLater(); } if(file != NULL) { delete file; file = NULL; } } /********************************************** * 作者姓名:袁爱玲 * 函数介绍: * 定义的参数含义说明 * 输入参数: * 输出参数:void * 返回值 :void **********************************************/ void IDownloadFile::httpFinished() { //D if (httpRequestAborted) { if (file) { file->close(); file->remove(); delete file; file = 0; } if(reply != NULL) { reply->deleteLater(); reply = NULL; } return; } file->flush(); file->close(); QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute); if (reply->error()) { file->remove(); emit downloadError(fid); qDebug()<<"PC客户端下载部分:"<errorString() << file->fileName(); } else if (!redirectionTarget.isNull()) { url= url.resolved(redirectionTarget.toUrl()); if(reply != NULL) { reply->deleteLater(); reply = NULL; } // file->open(QIODevice::WriteOnly); // file->resize(0); //startRequest(url); return; } else { downfileMap.remove(fid); emit writeok(fid); } if(reply != NULL) { reply->deleteLater(); reply = NULL; } delete file; file = NULL; // this->deleteLater(); startDownloadFile(); } /********************************************** * 作者姓名:袁爱玲 * 函数介绍: * 定义的参数含义说明 * 输入参数: * 输出参数:void * 返回值 :void **********************************************/ void IDownloadFile::httpReadyRead() { if (file) file->write(reply->readAll()); }