-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathFtpClient.cpp
More file actions
252 lines (230 loc) · 7.54 KB
/
FtpClient.cpp
File metadata and controls
252 lines (230 loc) · 7.54 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "FtpClient.h"
bool FtpClient::downloadHTTPFile(QString net_file_path, QString local_file_path)
{
m_isDownload = true;
QEventLoop loop;
QUrl tUrl = QUrl(net_file_path);
// if(isExistFile(local_file_path)) return false;
m_pFile = new QFile(local_file_path);
if(!m_pFile->open(QIODevice::WriteOnly))
{
delete m_pFile;
return false;
}
QNetworkRequest request;
request.setUrl(tUrl);
// request.setHeader(QNetworkRequest::ContentTypeHeader, "application/zip");
connect(m_pManager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
this, SLOT(slotAuthenticationRequired(QNetworkReply*,QAuthenticator*)));
m_pReply = m_pManager->get(request);
connect(m_pReply, SIGNAL(finished()), this, SLOT(http_finished()));
connect(m_pReply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(ftp_downloadProgress(qint64, qint64)));
connect(m_pReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(ftp_error(QNetworkReply::NetworkError)));
connect(m_pReply, SIGNAL(finished()),&loop, SLOT(quit()));
loop.exec();
return m_isDownload;
}
// Authentication
void FtpClient::slotAuthenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator)
{
authenticator->setUser("xiaogongwei**");
authenticator->setPassword("XiaoGongWei***");
}
/*
* example:
* FtpClient myftp;
* char *json_data = "{\"datastreams\": [{\"id\": \"temp0\", \"datapoints\": [{\"at\": \"2019-06-22T10:55:18.889416\", \"value\": 0.12}]}]}";
* QString put_url("http://api.heclouds.com/devices/531879860/datapoints");
* myftp.pushData2Http(put_url, json_data);
*/
bool FtpClient::pushData2Http(QString http_url, char *json_format)
{
QEventLoop loop;
QUrl tUrl = QUrl(http_url);
QNetworkRequest request;
request.setUrl(tUrl);
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
request.setRawHeader("api-key", "FOydn37wU3eESYzMuJ2=8XaeqzY=");
QByteArray postArry(json_format);
m_pReply = m_pManager->post(request, postArry);
connect(m_pReply, SIGNAL(finished()), this, SLOT(http_finished()));
connect(m_pReply, SIGNAL(finished()),&loop, SLOT(quit()));
loop.exec();
return true;
}
void FtpClient::http_finished()
{
QNetworkReply *pReply = qobject_cast<QNetworkReply *>(sender());
// QVariant status_code = pReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
// qDebug() << "status_code: " << status_code << endl;
switch (pReply->error())
{
case QNetworkReply::NoError:
m_pFile->write(pReply->readAll());
m_pFile->flush();
qDebug()<< "Success!" << endl;
break;
case QNetworkReply::HostNotFoundError:
qDebug()<< "Host Not Found!" << endl;
break;
case QNetworkReply::AuthenticationRequiredError:
qDebug()<< "Login Failure!" << endl;
break;
default:
qDebug()<< "Can not find this download File." << endl;
break;
}
if(m_pFile->isOpen()) m_pFile->close();
if(m_pFile->exists() && m_pFile->size() == 0)
{
m_isDownload = false;
m_pFile->remove();
}
delete m_pFile;
pReply->deleteLater();
}
FtpClient::FtpClient()
{
m_pManager = new QNetworkAccessManager();
m_pUrl = new QUrl();
m_isDownload = true;
m_isPush = true;
}
FtpClient::~FtpClient()
{
}
bool FtpClient::isExistFile(QString file_path)
{
if(QFile::exists(file_path))
return true;
else
return false;
}
void FtpClient::ftp_error(QNetworkReply::NetworkError net_error)
{
m_isDownload = false;
m_isPush = false;
//Check for errors
switch( net_error ){//Determine the status after the connection
case QNetworkReply::NoError:
qDebug()<< "Critical Error!" << endl;
m_isDownload = true;
break;
case QNetworkReply::HostNotFoundError:
qDebug()<< "Host Not Found!" << endl;
break;
case QNetworkReply::AuthenticationRequiredError:
qDebug()<< "Login Failure!" << endl;
break;
default:
qDebug()<< "Can not find this download File." << endl;
break;
}
}
void FtpClient::replay_finished()
{
QNetworkReply *pReply = qobject_cast<QNetworkReply *>(sender());
switch (pReply->error())
{
case QNetworkReply::NoError :
m_pFile->write(pReply->readAll());
m_pFile->flush();
break;
default:
break;
}
if(m_pFile->isOpen()) m_pFile->close();
pReply->deleteLater();
}
//Set the FTP server username and password
void FtpClient::FtpSetUserInfor(QString user, QString pwd)
{
m_pUrl->setUserName(user);
m_pUrl->setPassword(pwd);
}
//Set address and port
void FtpClient::FtpSetHostPort(QString host_str, int port )
{
m_pUrl->setHost(host_str);
m_pUrl->setPort(port);
}
//download file
bool FtpClient::FtpGet(QString net_file_path, QString local_file_path)
{
m_isDownload = true;
QEventLoop loop;
//If there is a deletion and the byte is 0
if(isExistFile(local_file_path))
{
QFile file(local_file_path);
if (file.exists() && file.size() == 0)
{
file.remove();
}
}
m_pFile = new QFile(local_file_path);
if(!m_pFile->open(QIODevice::WriteOnly))
{
qDebug()<< "can't open" << local_file_path;
return false;
}
m_pUrl->setScheme("ftp");
m_pUrl->setPath(net_file_path);
m_pReply = m_pManager->get(QNetworkRequest(*m_pUrl));
connect(m_pReply, SIGNAL(finished()), this, SLOT(replay_finished()));
connect(m_pReply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(ftp_downloadProgress(qint64, qint64)));
connect(m_pReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(ftp_error(QNetworkReply::NetworkError)));
connect(m_pReply, SIGNAL(finished()),&loop, SLOT(quit()));
loop.exec();
if(m_isDownload)
qDebug()<< net_file_path << "has download!";
if(m_pFile->exists() && m_pFile->size() == 0)
{
m_isDownload = false;
qDebug()<< net_file_path << "is zero bit, will be remove";
if(m_pFile->isOpen())
m_pFile->close();
m_pFile->remove();
}
return m_isDownload;
}
//upload files
bool FtpClient::FtpPut(QString local_file_path, QString net_file_path)
{
m_isPush = true;
QEventLoop loop;
QFile file(local_file_path);
if(!file.open(QIODevice::ReadOnly))
{
qDebug()<< "can't open" << local_file_path;
file.close();
return false;
}
QByteArray data = file.readAll();
m_pUrl->setScheme("ftp");
m_pUrl->setPath(net_file_path);
m_pReply = m_pManager->put(QNetworkRequest(*m_pUrl), data);
connect(m_pReply, SIGNAL(uploadProgress(qint64, qint64)), this, SLOT(ftp_uploadProgress(qint64, qint64)));
connect(m_pReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(ftp_error(QNetworkReply::NetworkError)));
connect(m_pReply, SIGNAL(finished()),&loop, SLOT(quit()));
loop.exec();
if(file.isOpen())
file.close();
if(m_isPush)
qDebug()<< local_file_path << "has push!";
else
qDebug()<< local_file_path << "hasn't push!";
return m_isPush;
}
// Upload progress
void FtpClient::ftp_uploadProgress(qint64 bytesSent, qint64 bytesTotal)
{
qDebug() << "Sent/Total is :" << bytesSent
<< "/" << bytesTotal <<endl;
}
// Download progress
void FtpClient::ftp_downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
qDebug() << "Received/Total is :" << bytesReceived
<< "/" << bytesTotal <<endl;
}