qftp的源码,看我上一篇文章,或者自行搜索
qftp作为客户端上传文件
头文件
#include "qftp.h"
//这个头文件描述文件信息,暂时没用上
#include <QFileInfo>
class A
{
private slots:
//这两个必须实现
void ftpStateChanged(int state);
void ftpCommandFinish(int id, bool en);
//上传文件到客户端
QString ftp_put_file(QString filename);
private:
QFtp* ftp = nullptr;
};
实现这两个信号槽
void A::ftpStateChanged(int state)
{
if (state == QFtp::Unconnected)
{
// IsLogin = false;
qDebug() << "state change to QFtp::Unconnected" << endl;
}
if (state == QFtp::Connected)
{
qDebug() << "state change to QFtp::Connected" << endl;
}
if (state == QFtp::HostLookup)
{
qDebug() << "state change to QFtp::HostLookup" << endl;
}
if (state == QFtp::LoggedIn)
{
qDebug() << "state change to QFtp::LoggedIn" << endl;
// IsLogin = true;
}
if (state == QFtp::Closing)
{
qDebug() << "state change to QFtp::Closing" << endl;
}
}
void A::ftpCommandFinish(int id, bool en)
{
Q_UNUSED(id)
if (ftp->currentCommand() == QFtp::ConnectToHost)
{
if (en)
{
qDebug() << "QFtp::ConnectToHost ftp->errorString: " << ftp->errorString() << endl;
}
else
{
}
}
if (ftp->currentCommand() == QFtp::Login)
{
if (en)
{
qDebug() << "QFtp::Login ftp->errorString: " << ftp->errorString() << endl;
}
else
{
// ftp->cd(toSpecialEncoding("/"));
// ftp->list();
}
}
if (ftp->currentCommand() == QFtp::Cd ||
ftp->currentCommand() == QFtp::List)
{
}
if (ftp->currentCommand() == QFtp::Get)
{
if (en)
{
qDebug() << "ftp->errorString: " << ftp->errorString() << endl;
}
else
{
}
}
else if (ftp->currentCommand() == QFtp::Put)
{
if (en)
{
qDebug() << "QFtp::Put ftp->errorString: " << ftp->errorString() << endl;
QMessageBox::information(nullptr, JString("上传失败"), ftp->errorString());
ftp->abort();
}
else
{
qDebug() << "QFtp::Put successed" << endl;
}
}
else if (ftp->currentCommand() == QFtp::Close)
{
}
else if (ftp->currentCommand() == QFtp::Unconnected)
{
}
else if (ftp->currentCommand() == QFtp::Rename)
{
}
else if (ftp->currentCommand() == QFtp::Mkdir)
{
if(en)
{
qDebug()<<"QFtp::Mkdir failed: "<< ftp->errorString() <<endl;
}
else
{
qDebug()<<"QFtp::Mkdir successed"<<endl;
}
}
else if (ftp->currentCommand() == QFtp::Remove)
{
}
else if (ftp->currentCommand() == QFtp::Rmdir)
{
}
}
main函数,初始化
void main()
{
ftp = new QFtp(this);
connect(ftp, SIGNAL(stateChanged(int)), this, SLOT(ftpStateChanged(int)));
connect(ftp, SIGNAL(commandFinished(int, bool)), SLOT(ftpCommandFinish(int, bool)));
ftp->connectToHost( "127.0.0.1", 21);
ftp->login("Software Department", "zhizhou666");
}
上传file到客户端
//上传file
QString ftp_put_file(QString filename);
QString Home::ftp_put_file(QString filename)
{
if (ftp->state() != QFtp::LoggedIn)
{
QMessageBox::information(nullptr, JString("无法上传"), JString("FTP 登录失败,无法上传文件"));
return QString();
}
QFileInfo info(filename);
QString new_dir = "save file"
ftp->mkdir(new_dir);
QString ftp_path = "E:/123.xml";
QFile *m_File = new QFile(filename);
//m_File = new QFile("E:/cache/ETLinkInitPlan-data-base.xml");
//m_File = new QFile("E:/cache/240116T143345.xml");
if (!m_File->isOpen())
{
ftp->put(m_File, ftp_path);
}
else
{
qDebug()<<"ftp file opened, upload failed!"<<endl;
}
m_File->close();
return ftp_path;
}