您好,登錄后才能下訂單哦!
為了方便網(wǎng)絡(luò)編程,Qt 提供了 Network 模塊。該模塊包含了許多類,本文介紹了Qt實(shí)現(xiàn)FTP的上傳和下載,分享給大家
本來(lái)想簡(jiǎn)單抄抄書,隨便手寫個(gè)Ftp客戶端的,結(jié)果發(fā)現(xiàn)教材上的是基于Qt4的QFtp類庫(kù),而在Qt5中取消了這一個(gè)類庫(kù)(同時(shí)也取消了QHttp等的類),取而代之的是QNetworkAccessManager 這個(gè)類,把這些雜貨全都攬下來(lái)了,據(jù)說(shuō)是因?yàn)橹暗膬蓚€(gè)類有重復(fù)而且效率有問(wèn)題balabala。于是就百度了一下,發(fā)現(xiàn)百度上要么講的不全,要么就是要去下一個(gè)由熱心網(wǎng)民重新封裝的QFtp類。顯然我并不喜歡無(wú)腦復(fù)制粘貼,想好好看下Qt官方提供的東西的用法,深入的理解下Qt網(wǎng)絡(luò)編程,于是就果斷自行g(shù)oogle(話說(shuō)google真好用),加上查看幫助文檔,終于把一個(gè)簡(jiǎn)版的Ftp客戶端大概框架弄清楚了。
不多說(shuō),上源碼:
Dialog.pro
#------------------------------------------------- # # Project created by QtCreator 2015-10-29T23:52:56 # #------------------------------------------------- QT += core gui QT += network #這里要添加這個(gè)庫(kù) greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = QFtp TEMPLATE = app SOURCES += main.cpp dialog.cpp HEADERS += dialog.h
dialog.h
#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;//這個(gè)是重點(diǎn) protected slots: //處理按鈕的點(diǎn)擊信號(hào) void slotPut(); void slotGet(); //處理網(wǎng)絡(luò)連接的信號(hào) 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/myths/1.txt"); LeUser=new QLineEdit("myths"); LePasswd=new QLineEdit("123456"); LePasswd->setEchoMode(QLineEdit::Password);//設(shè)置加密顯示 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);//固定大小 //按鈕點(diǎn)擊事件信號(hào)槽的連接 connect(PbPut,SIGNAL(clicked()),this,SLOT(slotPut())); connect(PbGet,SIGNAL(clicked()),this,SLOT(slotGet())); } void Dialog::managePut(QNetworkReply * reply){ qDebug()<<reply->error();//輸出調(diào)試信息 switch(reply->error()){//判斷連接后的狀態(tài) 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中讀取文件內(nèi)容,輸出到data中(也可以再將數(shù)據(jù)寫入到文件中,為了方便,這里就權(quán)且打印一下吧) 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 ; } //重點(diǎn)!將之前的槽清空并重新連接至需要的 manager.disconnect(SIGNAL(finished(QNetworkReply*))); //完全清空某對(duì)象連接的槽可以用manager.disconnect(); connect(&manager,SIGNAL(finished(QNetworkReply*)),SLOT(managePut(QNetworkReply*))); //設(shè)置登錄信息 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將觸發(fā)網(wǎng)絡(luò)的連接信號(hào) } 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=*/ manager.get((QNetworkRequest(url))); }
main.cpp
#include "dialog.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Dialog w; w.show(); return a.exec(); }
5、運(yùn)行截圖
權(quán)且只顯示主界面:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。