前言
QFtp 这个很早便被qt抛弃了,我去找了源码发现怎么都编译不成功
qt源代码:QFtp
后面只能一点点看源码修改,然后发现bug挺多的,花了三天时间终于跑起来了
为了方便调试和阅读,我把其中的的类全部拆分出来了,修改了一些变量名
加了一些我自己的东西,并且都打上了中文注释
gitee项目地址: XQFtp
使用
1.把其中的XQFtp这个目录拷贝下来,其中用到一些其他依赖在这个项目里也有也考下来即可
2.其实就是制作静态库了,安装静态库到Qt
文档
Gitee XQFtp文档 在gitee上文档更详细 使用typora查看文档比较好,网页发现格式比较乱
以下是摘取的一些文档说明
public(静态)
查询指定目录的列表信息
static QList<XQUrlInfo> List(const QString& host, quint16 port = 21, const QString& dir = QString(), const QString& user = QString(), const QString& password = QString());
下载
static void Get(const QString& host, quint16 port, const QString& remote_file, const QString& local_file, Ftp::TransferType type = Ftp::Binary, const QString& user = QString(), const QString& password = QString());
上传
static void Put(const QString& host, quint16 port, const QString& remote_file, const QString& local_file, Ftp::TransferType type = Ftp::Binary, const QString& user = QString(), const QString& password = QString());
public(获取)
获取可以从套接字中读取的字节数
qint64 bytesAvailable() const;
读取数据
qint64 read(char *data, qint64 maxlen);
QByteArray readAll();
返回目录列表信息
const QList<XQUrlInfo>& listInfo()const;
当前的id
int currentId() const;
返回当前的QIODevice
QIODevice* currentDevice() const;
返回当前命令
Ftp::Command currentCommand() const;
是否还有等待的命令没处理
bool hasPendingCommands() const;
清空剩余的等待命令
void clearPendingCommands();
获取状态
Ftp::State state() const;
获取错误码
Ftp::Error error() const;
获取错误信息
QString errorString() const;
是否是指定Debug模式
bool isDebugModel(Ftp::Debug model);
public(设置)
设置调试模式输出信息
void setDebugModel(int model);
void setDebugModel(Ftp::Debug model, bool open = true);
中断,终止连接
void abort();
public(发送命令)
返回值都是id
设置主机地址和端口
int setProxy(const QString &host, quint16 port);
连接主机
int connectToHost(const QString &host, quint16 port=21);
登录
int login(const QString &user = QString(), const QString &password = QString());
关闭连接
int close();
设置传输模式
int setTransferMode(Ftp::TransferMode mode);
查询指定目录的列表信息
int list(const QString &dir = QString());
进入目录
int cd(const QString &dir);
下载
//下载
int get(const QString &file, QIODevice *dev=0, Ftp::TransferType type = Ftp::Binary);
//下载到指定路径
int get(const QString& remote_file, const QString& local_file, Ftp::TransferType type = Ftp::Binary);
上传
//上传
int put(const QByteArray &data, const QString &file, Ftp::TransferType type = Ftp::Binary);
//上传
int put(QIODevice *dev, const QString &file, Ftp::TransferType type = Ftp::Binary);
//上传指定文件
int put(const QString& remote_file, const QString& local_file, Ftp::TransferType type = Ftp::Binary);
删除
int remove(const QString &file);
创建目录
int mkdir(const QString &dir);
删除目录
int rmdir(const QString &dir);
重命名
int rename(const QString &oldname, const QString &newname);
移动文件
int move(const QString& oldfile, const QString& newfile);
原始命令
int rawCommand(const QString &command);
public(等待)
等待状态变更
bool waitForStateChange(Ftp::State state, int msecs = 3000);
等待连接上服务器
bool waitForConnected(int msecs = 3000);
等待登录
bool waitForLoggedIn(int msecs = 3000);
等待传输结束 //get put list结束
bool waitForFinished(int msecs = -1);
signals
状态改变信号
void stateChanged(Ftp::State);
列表信息信号
void listInfo(const XQUrlInfo&);
准备读取信号
void readyRead();
数据传输进度
void dataTransferProgress(qint64 current, qint64 total);
原始命令准备完毕
void rawCommandReply(int, const QString&);
命令开始
void commandStarted(int id);
命令结束
void commandFinished(int id, bool);
是否都处理完成了
void done(bool);
测试代码
#include<QApplication>
#include"XQFtp.h"
#include"XQAlgorithm.h"
#include"XQLog.hpp"
#include<QFile>
auto ftp=new XQFtp ;
using Debug = Ftp::Debug;
ftp->setDebugModel(Debug::CommandRead| Debug::CommandWrite| Debug::error);
QObject::connect(qApp, &QApplication::aboutToQuit, ftp, &QObject::deleteLater);
// 连接到 FTP 服务器
ftp->connectToHost("192.168.1.3",21); // 替换为你的FTP服务器地址
// 登录到 FTP 服务器
ftp->login("FtpServer", "M1aSK4[z"); // 替换为你的FTP服务器登录凭据
QObject::connect(ftp, &XQFtp::stateChanged, [=](Ftp::State state) { XQDebug << "状态改变" << QDebug::toString(ftp->state()); });
// 检查连接状态
if (!ftp->waitForConnected())
XQDebug << "无法连接到FTP服务器:" << ftp->errorString();
else
XQDebug << "连接上服务器";
// 检查登录状态
if (!ftp->waitForLoggedIn())
XQDebug << "无法登录到FTP服务器:" << ftp->errorString();
else
XQDebug << "连接上服务器";
//不是登录状态不进行下一步
if (ftp->state() != Ftp::State::LoggedIn)
return;
ftp->cd("/FtpServer");
ftp->mkdir("test");
ftp->rmdir("test");
//ftp->rename("MyQt.exe","note/MyQt.exe");
ftp->list();
ftp->waitForFinished();
for (auto& info:ftp->listInfo())
{
qDebug() << info.name();
}
//静态成员方法
for (auto& info : XQFtp::List("192.168.1.3", 21, "/FtpServer", "FtpServer", "M1aSK4[z"))
{
qDebug() << info.name();
}
//静态上传
XQFtp::Put("192.168.1.3", 21, "/FtpServer/MyQt.exe", "MyQt.exe", Ftp::Binary, "FtpServer", "M1aSK4[z");
//静态下载
XQFtp::Get("192.168.1.3", 21, "/FtpServer/MyQt.exe", "Download/MyQt.exe", Ftp::Binary, "FtpServer", "M1aSK4[z");
//进度
QObject::connect(ftp, &XQFtp::dataTransferProgress, [](qint64 current, qint64 total)
{
XQDebug << "传输中:"<<readableFileSize( current)<<"/"<< readableFileSize(total);
});
//上传
ftp->put(QString("MyQt.exe"), "MyQt.exe");
ftp->waitForFinished();
XQDebug << "上传完成";
//下载
ftp->get("MyQt.exe","Download/MyQt.exe");
ftp->waitForFinished();
XQDebug << "下载完成";
// 断开与FTP服务器的连接
ftp->close();