-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFTPSocket.cpp
534 lines (447 loc) · 14.3 KB
/
FTPSocket.cpp
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#include "FTPSocket.h"
#define CHAR_CR "\r\n"
FTPSocket::FTPSocket(QObject* parent) : QObject(parent)
{
qRegisterMetaType<QPair<qint64, qint64>>("QPair<qint64, qint64>");
}
FTPSocket::~FTPSocket()
{
qInfo() << "析构 FTP";
if (m_FtpServerSocket)
{
m_FtpServerSocket->close();
m_FtpServerSocket->deleteLater();
m_FtpServerSocket = nullptr;
}
if (m_FtpDataSocket)
{
m_FtpDataSocket->close();
m_FtpDataSocket->deleteLater();
m_FtpDataSocket = nullptr;
}
}
void FTPSocket::setFtpUserNamePasswd(QByteArray userName, QByteArray passwd)
{
m_ftpUserName = "USER " + userName + CHAR_CR;
m_ftpPasswd = "PASS " + passwd + CHAR_CR;
}
void FTPSocket::connectFtpServer(QString hostName, quint16 port)
{
if (m_FtpServerSocket == nullptr)
{
m_FtpServerSocket = new QTcpSocket;
}
else
{
return;
}
m_hostName = hostName;
m_hostPort = port;
m_FtpServerSocket->abort();
m_FtpServerSocket->connectToHost(m_hostName, port);
connect(m_FtpServerSocket, &QTcpSocket::connected, this, &FTPSocket::onFtpConnectFinish);
connect(m_FtpServerSocket, &QTcpSocket::readyRead, this, &FTPSocket::onReadData);
connect(this, QOverload<const QByteArray&>::of(&FTPSocket::sigFtpSocketWrite), m_FtpServerSocket, QOverload<const QByteArray&>::of(&QTcpSocket::write));
connect(m_FtpServerSocket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), this,
[=](QAbstractSocket::SocketError socketError) {
qInfo() << "socketError" << socketError;
if (socketError == QAbstractSocket::RemoteHostClosedError)
{
return;
}
emit sigFTPConnectFailed();
});
qInfo() << "状态:" << m_loginSuccess;
}
QStringList FTPSocket::listFtpFile()
{
qInfo() << "连接状态: " << m_loginSuccess;
m_ftpEnumStatus = FTPSocket::FTP_LISTFile;
if (m_loginSuccess)
{
m_reconnections = 0;
pasvMode();
writeCommand(QString("NLST\r\n"));
return m_ftpFileList;
}
else
{
// 如果主机名不为空,说明登录过,启动重连
if (!m_hostName.isEmpty())
{
// 三次重连机会,如果都失败,结束返回
if (m_reconnections > 3)
{
emit sigFTPReconnectFailed();
return QStringList("");
}
m_reconnections++;
// 开始重连,只有登录状态为false时才调用,这里做这个判断,是由于listFtpFile调用太快,可能已经登录成功有延时
if (!m_loginSuccess)
{
connectFtpServer(m_hostName, m_hostPort);
QEventLoop loop;
connect(this, &FTPSocket::sigFTPConnectFinish, &loop, &QEventLoop::quit);
loop.exec();
}
// 递归调用
return listFtpFile();
}
}
return m_ftpFileList;
}
void FTPSocket::pasvMode()
{
QByteArray comm = "PASV\r\n";
emit sigFtpSocketWrite(comm);
QEventLoop loop;
connect(this, &FTPSocket::sigFTPPasvFinish, &loop, &QEventLoop::quit);
loop.exec();
}
int FTPSocket::splitPasvIpPort(const QString& PASVPassiveStr)
{
int ip = 0;
QString str = PASVPassiveStr;
if (str.contains("("))
{
QStringList strlist = str.split("(");
if (strlist.length() >= 2)
{
strlist = strlist.at(1).split(")");
QString ipString = strlist.at(0);
//qInfo() << "IP的数值: " << ipString;
strlist = ipString.split(",");
ip = strlist.at(4).toInt();
int ip2 = strlist.at(5).toInt();
ip = ip * 256 + ip2;
}
}
return ip;
}
QPair<qint64, QString> FTPSocket::formatFileSize(const qint64 &filesizebyte, int precision)
{
QPair<qint64, QString> sizePair;
if (filesizebyte == 0)
{
sizePair.first = 0;
sizePair.second = "0B";
return sizePair;
}
double sizeAsDouble = filesizebyte;
QStringList measures = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
int index = 0;
while (sizeAsDouble >= 1024.0)
{
index++;
sizeAsDouble /= 1024.0;
}
QString fileSize = QString::number(sizeAsDouble, 'f', precision) + measures[index];
sizePair.first = static_cast<qint64>(filesizebyte);
sizePair.second = fileSize;
return sizePair;
}
qint64 FTPSocket::ftpFileSize(const QString& fileName)
{
pasvMode();
writeCommand(QString("SIZE %0\r\n").arg(fileName));
qInfo() << "FtpFileSize: " << m_dwFileSizePair.second;
return m_dwFileSizePair.first;
}
void FTPSocket::downloadFtpFile(const QString &fileName, QString &localPwd)
{
// 获取文件大小
ftpFileSize(fileName);
// 设置状态
m_ftpEnumStatus = FTPSocket::FTP_DOWNFile;
qInfo() << "文件的后缀:" << m_dwfilesuffix;
writeCommand(QByteArray("TYPE I\r\n"), false);
// 如果是文件模式则进行文件预处理
if (m_ftpEnumMode == FTPSocket::MODE_FILE)
{
qInfo() << "文件模式,打开文件";
// 设置下载的位置
// 这个位置是本地系统的临时文件夹
QString dirPath = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
dirPath = dirPath + "/downFile";
qInfo() << "dirPath:" << dirPath;
// 检测临时文件夹里是否存在downFile文件夹,如果没有则创建
QDir dir(dirPath);
if (!dir.exists())
{
if (!dir.mkpath(dirPath))
{
qInfo() << "下载路径创建失败" << dirPath;
}
}
QFileInfo fileInfo(fileName);
m_dwfileName = fileInfo.fileName();
m_dwfilesuffix = fileInfo.suffix();
m_dwfileName.remove(m_dwfilesuffix);
m_dwfileName = m_dwfileName + DOWNLOAD_FILE_SUFFIX;
qInfo() << "准备下载的文件的名字" << m_dwfileName;
QFileInfo info;
localPwd = localPwd + "/" + m_dwfileName;
info.setFile(dir, localPwd);
if (m_pFile == nullptr)
{
m_pFile = new QFile(info.filePath());
}
qInfo() << "localPwd:" << localPwd;
m_fileCacheOffset = info.size();
qInfo() << "本地文件大小:" << m_fileCacheOffset;
qInfo() << "下载文件的大小:" << m_dwFileSizePair.first;
if (m_fileCacheOffset > 0 && m_fileCacheOffset != m_dwFileSizePair.first)
{
m_pFile->open(QIODevice::Append);
qInfo() << "启动断点续传下载";
writeCommand(QString("REST %1\r\n").arg(m_fileCacheOffset));
}
else
{
if (!m_pFile->open(QIODevice::WriteOnly))
{
qInfo() << "文件打开失败";
}
}
}
pasvMode();
qInfo() << "准备下载的文件:" << fileName;
writeCommand(QString("RETR %1\r\n").arg(fileName));
}
void FTPSocket::saveDownloadFile()
{
if (m_ftpEnumMode == FTPSocket::MODE_Stream)
{
emit sigFTPStreamFileFinish(m_fileCacheData);
}
else
{
if (m_pFile)
{
if (m_pFile->isOpen())
{
qInfo() << "文件打开,写入文件";
m_pFile->write(m_fileCacheData);
m_pFile->flush();
m_pFile->close();
m_isFileDownFinish = true;
}
if (m_isFileDownFinish)
{
m_isFileDownFinish = false;
QFileInfo fileInfo(*m_pFile);
QString filePath = fileInfo.absolutePath();
qInfo() << "filePath:" << filePath;
QString fileOldSuffix = fileInfo.suffix();
qInfo() << "fileOldSuffix:" << fileOldSuffix;
m_dwfileName = m_dwfileName.remove(fileOldSuffix);
qInfo() << "m_dwfileName:" << m_dwfileName;
QString fileNewName = filePath + "/" + m_dwfileName + m_dwfilesuffix;
qInfo() << "fileNewName:" << fileNewName;
qInfo() << "oldName:" << m_pFile->fileName();
// 如果文件已存在,则覆盖
if (QFile::exists(fileNewName))
{
QFile::remove(fileNewName);
}
m_pFile->rename(m_pFile->fileName(), fileNewName);
emit sigFTPDownloadFinish(fileNewName);
}
}
delete m_pFile;
m_pFile = nullptr;
}
////关闭FTP的连接,延时500毫秒
//QTimer::singleShot(DELAY_TIME, this, [=] {
// qInfo() << "进来关闭Ftp.";
// closeFTPConnect();
//});
}
void FTPSocket::listenFtpDataPort(quint16 port)
{
if (m_FtpDataSocket == nullptr)
{
m_FtpDataSocket = new QTcpSocket();
}
m_FtpDataSocket->close();
m_FtpDataSocket->abort();
m_FtpDataSocket->connectToHost(m_hostName, port);
connect(m_FtpDataSocket, &QTcpSocket::readyRead, this, &FTPSocket::onFtpDataRead, Qt::DirectConnection);
emit sigFTPPasvFinish();
}
void FTPSocket::closeFTPConnect(bool synchronization)
{
//qInfo() << "关闭FTP连接,isLoginSuccess状态为" << m_loginSuccess;
// 如果没有连接,直接返回
if (!m_loginSuccess)
{
return;
}
if (m_FtpServerSocket)
{
m_loginSuccess = false;
writeCommand(QByteArray("QUIT\r\n"), synchronization);
}
}
void FTPSocket::FtpCWD(QString dirName)
{
writeCommand(QString("CWD %0\r\n").arg(dirName));
FtpPWD();
}
void FTPSocket::FtpPWD()
{
writeCommand(QString("PWD\r\n"));
}
void FTPSocket::onReadData()
{
QByteArray array = m_FtpServerSocket->readAll();
//qInfo() << "ftpServer接收到的数据: " << array;
if (array.contains("230 User") || array.contains("230 Login successful") || array.contains("230 User logged in"))
{
emit this->sigFTPCommandFinish();
QTimer::singleShot(DELAY_TIME, this, [=] {
//qInfo() << "登录成功";
m_loginSuccess = true;
emit sigFTPConnectFinish();
});
}
else if (array.contains("331 Please specify the password") || array.contains("331 Password required") || array.contains("331 Anonymous access allowed, send identity (e-mail name) as password"))
{
emit this->sigFTPCommandFinish();
//qInfo() << "输入密码";
QTimer::singleShot(DELAY_TIME, this, [=] {
writeCommand(m_ftpPasswd);
});
}
else if (array.contains("213"))
{
QString filesize(array);
filesize = filesize.simplified();
filesize = filesize.mid(4);
m_dwFileSizePair = formatFileSize(filesize.toLongLong());
emit this->sigFTPCommandFinish();
}
else if (array.contains("257"))
{
int indexA = 5;
int indexB = array.indexOf("is");
currentDir = array.mid(indexA, indexB - indexA - 2);
//qInfo() << "当前的路径为: " << currentDir;
emit this->sigFTPCommandFinish();
QTimer::singleShot(DELAY_TIME, this, [=] {
//qInfo() << "当前的路径为: " << currentDir;
emit sigFTPPWDFinish(currentDir);
});
}
// 切换目录成功
else if (array.contains("250 CWD"))
{
emit this->sigFTPCommandFinish();
QTimer::singleShot(DELAY_TIME, this, [=] {
emit sigFTPCWDSuccess();
});
}
else if (array.contains("227 Entering Passive Mode"))
{
emit this->sigFTPCommandFinish();
QTimer::singleShot(DELAY_TIME, this, [=] {
//qInfo() << "准备连接到数据通道";
listenFtpDataPort(static_cast<quint16>(splitPasvIpPort(array)));
});
}
else if (array.contains("221 Goodbye") || array.contains("221 goodbye"))
{
emit this->sigFTPCommandFinish();
QTimer::singleShot(DELAY_TIME, this, [=] {
if (m_FtpServerSocket)
{
m_FtpServerSocket->close();
m_FtpServerSocket->deleteLater();
m_FtpServerSocket = nullptr;
}
if (m_FtpDataSocket)
{
m_FtpDataSocket->close();
m_FtpDataSocket->deleteLater();
m_FtpDataSocket = nullptr;
}
// 延迟触发关闭信号
emit sigFTPCloseConnect();
});
}
else if (array.contains("226 Transfer complete"))
{
// 命令完成后触发信号,阻塞同步解除
emit this->sigFTPCommandFinish();
QTimer::singleShot(DELAY_TIME, this, [=] {
//m_isFileDownFinish = true;
if (m_ftpEnumMode != FTPMODE::MODE_ThreadStream)
{
saveDownloadFile();
}
});
}
else if (array.contains("550-The filename, directory name, or volume label syntax is incorrect"))
{
qInfo() << "要切换的目录为非文件夹,切换失败";
emit sigFTPCommandFinish();
}
}
void FTPSocket::onFtpDataRead()
{
QByteArray array = m_FtpDataSocket->readAll();
if (array.isEmpty())
{
qInfo() << "数据通道中的数据为空";
return;
}
qInfo() << "数据通道接收到的数据:" << array;
QString ftpFileStr;
QPair<qint64, QString> pairFileSize;
switch (static_cast<int>(m_ftpEnumStatus))
{
case FTPSocket::FTP_LISTFile:
//qInfo() << "FTPList 进入";
ftpFileStr = QString(array);
m_ftpFileList = ftpFileStr.split("\r\n");
m_ftpFileList.removeAll("");
emit sigFTPListFileFinish();
break;
case FTPSocket::FTP_DOWNFile:
m_fileCacheData += array;
pairFileSize = formatFileSize(m_fileCacheOffset+m_fileCacheData.length());
if (pairFileSize.first == m_dwFileSizePair.first)
{
emit sigDownloadComplete();
}
if (m_ftpEnumMode == FTPSocket::MODE_FILE)
{
emit sigDownloadProgress(pairFileSize.first, m_dwFileSizePair.first);
}
break;
}
}
void FTPSocket::writeCommand(const QByteArray &comm, bool synchronization)
{
qInfo() << "开始 comm" << comm;
emit sigFtpSocketWrite(comm);
if (synchronization)
{
QEventLoop loop;
connect(this, &FTPSocket::sigFTPConnectFinish, &loop, &QEventLoop::quit);
// 1s之后退出去
QTimer::singleShot(500, &loop, &QEventLoop::quit);
loop.exec();
}
}
void FTPSocket::writeCommand(const QString &comm, bool synchronization)
{
QString strComm = comm;
QByteArray commArray = strComm.toUtf8();
writeCommand(commArray, synchronization);
}
void FTPSocket::onFtpConnectFinish()
{
//qInfo() << "连接成功,输入用户名";
writeCommand(m_ftpUserName);
}