12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- #ifndef QTFTP_H
- #define QTFTP_H
- #include <QObject>
- #include <QNetworkReply>
- #include <QNetworkAccessManager>
- #include <QUrl>
- #include <QFile>
- #include <QTimer>
- /**
- * @brief 封装一个FTP类,这里没有单开一个线程,在调用线程中工作
- */
- class QtFtp : public QObject
- {
- Q_OBJECT
- public:
- explicit QtFtp(QObject* parent = nullptr);
- ~QtFtp();
- void setHostAndPort(const QString& host,int port = 21); /* 设置目标主机IP和端口 */
- void setUserPasswd(const QString& user,const QString& pw); /* 设置目标设备的用户名和密码 */
- void putFile(const QString& fileName,const QString& farPath); /* 上传文件 */
- void getFile(const QString& fileName,const QString &srcPath); /* 下载文件 */
- bool waitFinished(int msecs = 30000); /* 等待完成 */
- bool getResult(); /* 获取结果 */
- signals:
- void signal_uploadState(bool flag); /* 文件上传成功或失败 */
- void signal_downloadState(bool flag); /* 文件下载成功或失败 */
- void signal_uploadProgress(qint64 bytesSent, qint64 bytesTotal); /* 上传进度 */
- void signal_downloadProgress(qint64 bytesReceived, qint64 bytesTotal); /* 下载进度 */
- #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
- /* 将QNetworkReply的一些信号转换成自定义的信号 */
- void signal_error(QNetworkReply::NetworkError);
- #endif
- private slots:
- void do_uploadFinished(); /* 上传完成 */
- void do_downloadFinished(); /* 下载完成,保存文件 */
- void do_uploadProgress(qint64 bytesSent, qint64 bytesTotal); /* 上传进度 */
- void do_downloadProgress(qint64 bytesReceived, qint64 bytesTotal); /* 下载进度 */
- #if defined (QT_VERSION) && QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
- void do_ftpReplyError(QNetworkReply::NetworkError error); /* ftp发送错误槽函数 */
- #endif
- private:
- bool m_isFinished = false; /* 上传或下载是否完成 */
- bool m_result = false; /* 上传或下载是否成功 */
- QTimer m_timer; /* 定时器,用于等待上传或下载完成 */
- QUrl m_url;
- QFile m_file; /* 存储下载的文件 */
- QNetworkAccessManager m_manager;
- QNetworkReply* m_reply = nullptr;
- };
- #endif /* QTFTP_H */
|