QtFtp.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "QtFtp.h"
  2. #include "LightLog.h"
  3. #include <QEventLoop>
  4. /* 构造函数 */
  5. QtFtp::QtFtp(QObject *parent)
  6. {
  7. /* 设置协议 */
  8. m_url.setScheme("ftp");
  9. /* 设置超时定时器 */
  10. m_timer.setTimerType(Qt::PreciseTimer);
  11. m_timer.setSingleShot(true);
  12. }
  13. QtFtp::~QtFtp()
  14. {
  15. if(m_file.isOpen())
  16. {
  17. m_file.close();
  18. }
  19. if(m_reply != nullptr)
  20. {
  21. m_reply->deleteLater();
  22. m_reply = nullptr;
  23. }
  24. }
  25. /* 设置目标主机IP和端口 */
  26. void QtFtp::setHostAndPort(const QString &host, int port)
  27. {
  28. m_url.setHost(host);
  29. m_url.setPort(port);
  30. }
  31. /* 设置目标设备的用户名和密码 */
  32. void QtFtp::setUserPasswd(const QString &user, const QString &pw)
  33. {
  34. m_url.setUserName(user);
  35. m_url.setPassword(pw);
  36. }
  37. /**
  38. * @brief 上传文件
  39. * @param file 要上传的文件名
  40. * @param path 目标计算机存放文件的路径
  41. */
  42. void QtFtp::putFile(const QString &fileName, const QString &farPath)
  43. {
  44. m_isFinished = false;
  45. m_result = false;
  46. QFile file(fileName);
  47. file.open(QFile::ReadOnly);
  48. QByteArray ba = file.readAll();
  49. file.close();
  50. m_url.setPath(farPath);
  51. m_reply = m_manager.put(QNetworkRequest(m_url),ba);
  52. /* 将信号进行一次转发 */
  53. connect(m_reply,SIGNAL(uploadProgress(qint64,qint64)),this,SIGNAL(signal_uploadProgress(qint64,qint64)));
  54. /* 连接信号和槽 */
  55. connect(m_reply,SIGNAL(finished()),this,SLOT(do_uploadFinished()));
  56. connect(m_reply,SIGNAL(uploadProgress(qint64,qint64)),this,SLOT(do_uploadProgress(qint64,qint64)));
  57. #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
  58. connect(m_reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SIGNAL(signal_error(QNetworkReply::NetworkError)));
  59. #endif
  60. }
  61. /**
  62. * @brief QtFtp::getFile
  63. * @param fileName 本机文件路径和文件名
  64. * @param path 目标文件路径
  65. */
  66. void QtFtp::getFile(const QString &fileName,const QString &srcPath)
  67. {
  68. m_isFinished = false;
  69. m_result = false;
  70. m_file.setFileName(fileName);
  71. if(!m_file.open(QFile::WriteOnly | QFile::Truncate))
  72. {
  73. LOG_WARN("open file failed , file path : " + fileName);
  74. return;
  75. }
  76. m_url.setPath(srcPath);
  77. m_reply = m_manager.get(QNetworkRequest(m_url));
  78. /* 转发信号 */
  79. connect(m_reply,SIGNAL(downloadProgress(qint64,qint64)),this,SIGNAL(signal_downloadProgress(qint64,qint64)));
  80. /* 连接信号和槽 */
  81. connect(m_reply,SIGNAL(finished()),this,SLOT(do_downloadFinished()));
  82. connect(m_reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(do_downloadProgress(qint64,qint64)));
  83. #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
  84. connect(m_reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SIGNAL(signal_error(QNetworkReply::NetworkError)));
  85. #endif
  86. }
  87. /**
  88. * @brief 等待完成,同时设置超时时间,超时后,断开reply的信号和槽的连接
  89. * 参数设置为0表示查询是否完成,设置为-1表示一直等待
  90. *
  91. * @param msecs 超时时间
  92. * @arg -1 一直等待,将会阻塞在这里
  93. * @arg 0 查询是否完成
  94. * @arg 正整数,等待完成的时间,设置完成之后再次设置,会覆盖之前的设置
  95. * @return true 上传或下载完成
  96. * @return false 上传或下载进行中
  97. */
  98. bool QtFtp::waitFinished(int msecs)
  99. {
  100. if(msecs == 0)
  101. {
  102. return m_isFinished;
  103. }
  104. else if(msecs == -1)
  105. {
  106. QEventLoop loop;
  107. connect(this, &QtFtp::signal_uploadState, &loop, &QEventLoop::quit);
  108. connect(this, &QtFtp::signal_downloadState, &loop, &QEventLoop::quit);
  109. loop.exec();
  110. return true;
  111. }
  112. else if (msecs > 0)
  113. {
  114. m_timer.start(msecs);
  115. return m_isFinished;
  116. }
  117. return m_isFinished;
  118. }
  119. /**
  120. * @brief 获取结果
  121. *
  122. * @return int
  123. * @arg -1 上传或下载失败
  124. * @arg 0 上传或下载成功
  125. */
  126. bool QtFtp::getResult()
  127. {
  128. return m_result;
  129. }
  130. /* 上传完成 */
  131. void QtFtp::do_uploadFinished()
  132. {
  133. if(m_reply->error() == QNetworkReply::NoError)
  134. {
  135. m_result = true;
  136. emit signal_uploadState(true);
  137. LOG_INFO("----- FTP upload success -----");
  138. }
  139. else
  140. {
  141. LOG_WARN("FTP上传失败:" + m_reply->errorString());
  142. emit signal_uploadState(false);
  143. }
  144. m_reply->deleteLater();
  145. m_reply = nullptr;
  146. m_isFinished = true;
  147. }
  148. /* 完成,保存文件 */
  149. void QtFtp::do_downloadFinished()
  150. {
  151. if(m_reply->error() == QNetworkReply::NoError)
  152. {
  153. m_file.write(m_reply->readAll());
  154. m_file.flush(); /* 刷新文件,写入硬盘 */
  155. emit signal_downloadState(true);
  156. m_result = true;
  157. LOG_DEBUG("----- FTP download success -----");
  158. }else
  159. {
  160. LOG_WARN("FTP download failed:" + m_reply->errorString());
  161. /* 下载失败就删除创建的文件 */
  162. m_file.remove();
  163. emit signal_downloadState(false);
  164. }
  165. m_file.close();
  166. m_reply->deleteLater();
  167. m_reply = nullptr;
  168. m_isFinished = true;
  169. }
  170. /* 上传进度 */
  171. void QtFtp::do_uploadProgress(qint64 bytesSent, qint64 bytesTotal)
  172. {
  173. LOG_DEBUG("sent / total : " + QString::number(bytesSent) + " / " + QString::number(bytesTotal));
  174. }
  175. /* 下载进度 */
  176. void QtFtp::do_downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
  177. {
  178. LOG_DEBUG("received / total : " + QString::number(bytesReceived) + " / " + QString::number(bytesTotal));
  179. }
  180. /* ftp发送错误槽函数 */
  181. #if defined (QT_VERSION) && QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
  182. void QtFtp::do_ftpReplyError(QNetworkReply::NetworkError error)
  183. {
  184. if(error == QNetworkReply::NoError)
  185. {
  186. LOG_INFO("FTP发送成功");
  187. }
  188. else
  189. {
  190. LOG_WARN("FTP发送失败:" + QString::number(error));
  191. }
  192. }
  193. #endif