qt可以使用QNetworkAccessManager的put进行上传,get进行下载;
pro需增加network
类源码如下:
FtpClient.h
#ifndef FtpClient_H
#define FtpClient_H
#include <QNetworkAccessManager>
#include <QObject>
#include <QFile>
/** 请求类型 */
enum RequestType
{
/** 没有请求(默认) */
no,
/** 下载 */
get,
/** 上传 */
put
};
/** ftp客户端 */
class FtpClient : public QNetworkAccessManager
{
Q_OBJECT
public:
explicit FtpClient(QObject *parent = nullptr);
~FtpClient();
/** 下载:url-网络路径,path-本地路径 */
void download(const QString &url, const QString &path);
/** 上传:path-本地路径,url-网络路径 */
void upload(const QString &path, const QString &url);
private:
/** 文件 */
QFile m_file;
/** 请求类型 */
RequestType m_RequestType;
/** 请求 */
QNetworkReply *m_NetworkReply;
signals:
/** 下载结果 */
void signal_DownloadResult(const bool &result);
/** 下载进度 */
void signal_DownloadProgress(const int &progress);
/** 上传结果 */
void signal_UploadResult(const bool &result);
/** 上传进度 */
void signal_UploadProgress(const int &progress);
private slots:
/** 结束:reply-请求 */
void slot_this_finished(QNetworkReply *reply);
/** 下载读取 */
void slot_NetworkReply_readyRead();
/** 下载进度:bytesReceived-已接收字节数,bytesTotal-总字节数 */
void slot_NetworkReply_downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
/** 上传进度:bytesSent-已上传字节数,bytesTotal-总字节数 */
void slot_NetworkReply_uploadProgress(qint64 bytesSent, qint64 bytesTotal);
};
#endif
FtpClient.cpp
#include "FtpClient.h"
#include <QNetworkReply>
FtpClient::FtpClient(QObject *parent) : QNetworkAccessManager(parent)
{
m_RequestType = RequestType::no;
m_NetworkReply = nullptr;
connect(this, &FtpClient::finished, this, &FtpClient::slot_this_finished);
}
FtpClient::~FtpClient()
{
}
void FtpClient::download(const QString &url, const QString &path)
{
m_file.setFileName(path);
if(m_file.open(QIODevice::WriteOnly) == false)
{
emit signal_DownloadResult(false);
deleteLater();
}
else
{
m_RequestType = RequestType::get;
m_NetworkReply = get(QNetworkRequest(QUrl(url)));
connect(m_NetworkReply, &QNetworkReply::readyRead, this, &FtpClient::slot_NetworkReply_readyRead);
connect(m_NetworkReply, &QNetworkReply::downloadProgress, this, &FtpClient::slot_NetworkReply_downloadProgress);
}
}
void FtpClient::slot_this_finished(QNetworkReply *reply)
{
switch (m_RequestType)
{
case RequestType::get:
m_file.close();
emit signal_DownloadResult(reply->error() == QNetworkReply::NoError);
break;
case RequestType::put:
m_file.close();
emit signal_UploadResult(reply->error() == QNetworkReply::NoError);
break;
default:
break;
}
reply->deleteLater();
deleteLater();
}
void FtpClient::slot_NetworkReply_readyRead()
{
if(m_file.write(m_NetworkReply->readAll()) == -1)
{
m_NetworkReply->close();
}
}
void FtpClient::slot_NetworkReply_downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
if(bytesReceived >= 0 && bytesReceived <= bytesTotal && bytesTotal > 0)
{
emit signal_DownloadProgress(bytesReceived*100/bytesTotal);
}
}
void FtpClient::upload(const QString &path, const QString &url)
{
m_file.setFileName(path);
if(m_file.open(QIODevice::ReadOnly) == false)
{
emit signal_UploadResult(false);
deleteLater();
}
else
{
m_RequestType = RequestType::put;
m_NetworkReply = put(QNetworkRequest(QUrl(url)), &m_file);
connect(m_NetworkReply, &QNetworkReply::uploadProgress, this, &FtpClient::slot_NetworkReply_uploadProgress);
}
}
void FtpClient::slot_NetworkReply_uploadProgress(qint64 bytesSent, qint64 bytesTotal)
{
if(bytesSent >= 0 && bytesSent <= bytesTotal && bytesTotal > 0)
{
emit signal_UploadProgress(bytesSent*100/bytesTotal);
}
}