downloadfile.cpp
4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "downloadfile.h"
#include <QDebug>
#include <QDir>
#include "GlobalSetting.h"
#include <QMutexLocker>
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"<<error;
emit writeok(0);
}
/**********************************************
* 作者姓名:袁爱玲
* 函数介绍:
* 定义的参数含义说明
* 输入参数:
* 输出参数:void
* 返回值 :void
**********************************************/
void IDownloadFile::cancelDownload()
{
httpRequestAborted = true;
if(reply != NULL)
{
reply->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客户端下载部分:"<<reply->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());
}