Qt实现FTP的上传和下载的实例代码
为了⽅便⽹络编程,Qt 提供了 Network 模块。该模块包含了许多类,本⽂介绍了Qt实现FTP的上传和下载,分享给⼤家
本来想简单抄抄书,随便⼿写个Ftp客户端的,结果发现教材上的是基于Qt4的QFtp类库,⽽在Qt5中取消了这⼀个类库(同时也取消了QHttp等的类),取⽽代之的是QNetworkAccessManager 这个类,把这些杂货全都揽下来了,据说是因为之前的两个类有重复⽽且效率有问题balabala。于是就百度了⼀下,发现百度上要么讲的不全,要么就是要去下⼀个由热⼼⽹民重新封装的QFtp类。显然我并不喜欢⽆脑复制粘贴,想好好看下Qt官⽅提供的东西的⽤法,深⼊的理解下Qt⽹络编程,于是就果断⾃⾏google(话说google真好⽤),加上查看帮助⽂档,终于把⼀个简版的Ftp客户端⼤概框架弄清楚了。
不多说,上源码:
Dialog.pro
#-------------------------------------------------
#
# Project created by QtCreator 2015-10-29T23:52:56
#
#-------------------------------------------------
QT += core gui
QT += network #这⾥要添加这个库
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QFtp
TEMPLATE = app
SOURCES += main.cpp
dialog.cpp
HEADERS += dialog.h
dialog.hconnect下载
#ifndef DIALOG_H
#define DIALOG_H
//注意需要添加的头⽂件
#include<QDialog>
#include<QPushButton>
#include<QFile>
#include<QNetworkReply>
#include<QLineEdit>
#include<QtNetwork/QNetworkAccessManager>
#include<QtNetwork/QNetworkRequest>
#include<QLabel>
#include<QString>
#include<QGridLayout>
#include<QMessageBox>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
public:
QGridLayout *layout;
QLabel *LbServer,*LbUser,*LbPasswd;
QLineEdit *LeServer,*LeUser,*LePasswd;
QPushButton *PbPut,*PbGet;
QNetworkAccessManager manager;//这个是重点
protected slots:
//处理按钮的点击信号
void slotPut();
void slotGet();
//处理⽹络连接的信号
void managePut(QNetworkReply*);
void manageGet(QNetworkReply*);
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
setWindowTitle("My Ftp");
layout=new QGridLayout(this);
LbServer=new QLabel("Server:");
LbUser=new QLabel("User:");
LbPasswd=new QLabel("Passwd:");
LeServer=new QLineEdit("ftp://120.27.41.126/home/");
LeUser=new QLineEdit("myths");
LePasswd=new QLineEdit("123456");
LePasswd->setEchoMode(QLineEdit::Password);//设置加密显⽰
PbPut=new QPushButton("Put");
PbGet=new QPushButton("Get");
layout->addWidget(LbServer,0,0);
layout->addWidget(LeServer,0,1);
layout->addWidget(LbUser,1,0);
layout->addWidget(LeUser,1,1);
layout->addWidget(LbPasswd,2,0);
layout->addWidget(LePasswd,2,1);
layout->addWidget(PbPut,3,0);
layout->addWidget(PbGet,3,1);
setFixedSize(300,200);//固定⼤⼩
//按钮点击事件信号槽的连接
connect(PbPut,SIGNAL(clicked()),this,SLOT(slotPut()));
connect(PbGet,SIGNAL(clicked()),this,SLOT(slotGet()));
}
void Dialog::managePut(QNetworkReply * reply){
qDebug()<<reply->error();//输出调试信息
switch(reply->error()){//判断连接后的状态
case QNetworkReply::NoError:
QMessageBox::information(this,"Put information","Upload Success!");
break;
case QNetworkReply::HostNotFoundError:
QMessageBox::information(this,"Put information","Host Not Found!");
break;
case QNetworkReply::AuthenticationRequiredError:
QMessageBox::information(this,"Put information","Login Failure!");
break;
default:
QMessageBox::information(this,"Put information","Unknown Failure");
break;
}
}
void Dialog::manageGet(QNetworkReply *reply){
//基本和managerPut类似
qDebug()<<reply->error();
QByteArray data;
switch(reply->error()){
case QNetworkReply::NoError:
data=reply->readAll();//从url中读取⽂件内容,输出到data中(也可以再将数据写⼊到⽂件中,为了⽅便,
这⾥就权且打印⼀下吧) QMessageBox::information(this,"Put information","Upload Success!nThe file you've got is :n"+data);
break;
case QNetworkReply::HostNotFoundError:
QMessageBox::information(this,"Put information","Host Not Found!");
break;
case QNetworkReply::AuthenticationRequiredError:
QMessageBox::information(this,"Put information","Login Failure!");
break;
default:
QMessageBox::information(this,"Put information","Unknown Failure");
break;
}
}
Dialog::~Dialog()
{
}
void Dialog::slotPut(){
//判断信息输⼊完整
if(LeUser->text().isEmpty()||LePasswd->text().isEmpty()||LeServer->text().isEmpty()){
QMessageBox::warning(this,"Error","Please fill in the information");
return ;
}
/
/重点!将之前的槽清空并重新连接⾄需要的
manager.disconnect(SIGNAL(finished(QNetworkReply*)));
//完全清空某对象连接的槽可以⽤manager.disconnect();
connect(&manager,SIGNAL(finished(QNetworkReply*)),SLOT(managePut(QNetworkReply*)));
//设置登录信息
QUrl url(LeServer->text());
url.setPort(21);
url.setUserName(LeUser->text());
url.setPassword(LePasswd->text());
QByteArray data="This is the test data.n";
/*QNetworkReply *reply=*/
manager.put(QNetworkRequest(url),data);//将data上传到url中,返回的reply将触发⽹络的连接信号}
void Dialog::slotGet(){
//基本意义与slotPut类似
if(LeUser->text().isEmpty()||LePasswd->text().isEmpty()||LeServer->text().isEmpty()){
QMessageBox::warning(this,"Error","Please fill in the information");
return ;
}
manager.disconnect(SIGNAL(finished(QNetworkReply*)));
connect(&manager,SIGNAL(finished(QNetworkReply*)),SLOT(manageGet(QNetworkReply*)));
QUrl url(LeServer->text());
url.setPort(21);
url.setUserName(LeUser->text());
url.setPassword(LePasswd->text());
/*QNetworkReply *reply=*/
<((QNetworkRequest(url)));
}
main.cpp
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
();
}
5、运⾏截图
权且只显⽰主界⾯:
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。