FTP(File transfer protocol ,文件传输协议)是一个主要用于浏览远程目录和传输文件的协议。FTP使用两个网络连接,一个用来发送命令,一个用来输出数据。FTP协议有一个状态,并且需要客户端在传输文件之前发送一些命令。FTP客户端建立一个连接,并在整个会话期间一直保持打开。在每个会话期间,可以发生多个传输。
在Qt5中编写FTP应用时,需要使用QNetworkAccessManager等网络访问接口类。实现方式与前面讲到的HTTP应用十分相似,只需在QUrl对象中设置好主机地址、用户名和密码等,然后使用get()、put()等函数完成文件的获取和上传。
myftp.pro,添加network模块
QT += network
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QUrl>
class QNetworkReply;
class QNetworkAccessManager;
class QFile;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
void startRequest(QUrl url);
private:
Ui::MainWindow *ui;
QNetworkAccessManager *manager;
QNetworkReply *reply;
QUrl url;
QFile *file;
private slots:
void httpFinished();
void httpReadyRead();
void updateDataReadProgress(qint64,qint64);
void on_pushButton_clicked();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QNetworkAccessManager>
#include <QtNetwork>
#include <QFile>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
manager = new QNetworkAccessManager(this);
ui->progressBar->hide();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::startRequest(QUrl url,bool bSend)
{
if (!bSend) {
reply = manager->get(QNetworkRequest(url));
connect(reply,&QNetworkReply::readyRead,this,&MainWindow::httpReadyRead);
connect(reply,&QNetworkReply::downloadProgress,this,&MainWindow::updateDataReadProgress);
connect(reply,&QNetworkReply::finished,this,&MainWindow::httpFinished);
} else {
QString fileName = "e:/XunLei.exe";
QFile file(fileName);
QByteArray by_txt;
if (file.open(QIODevice::ReadOnly))
{
by_txt = file.readAll();
file.close();
reply = manager->put(QNetworkRequest(url),by_txt);
}
connect(reply,&QNetworkReply::finished,this,&MainWindow::httpFinished);
}
}
void MainWindow::httpFinished()
{
ui->progressBar->hide();
if (file) {
file->close();
delete file;
file = 0;
}
reply->deleteLater();
reply = 0;
qDebug()<<"send complete";
}
void MainWindow::httpReadyRead()
{
if (file) {
file->write(reply->readAll());
}
}
void MainWindow::updateDataReadProgress(qint64 bytesRead,qint64 totalBytes)
{
ui->progressBar->setMaximum(totalBytes);
ui->progressBar->setValue(bytesRead);
}
void MainWindow::on_pushButton_clicked()
{
//url = ui->lineEdit->text();
QUrl url;
url.setScheme("ftp");
url.setHost("192.168.1.9");
url.setPath("qt-opensource-windows-x86-msvc2015_64-5.7.1.exe");
url.setUserName("adminFtp");
url.setPassword("123456");
QFileInfo info(url.path());
QString fileName(info.fileName());
if (fileName.isEmpty()) {
fileName = "index.html";
}
file = new QFile(fileName);
if (!file->open(QIODevice::WriteOnly)) {
delete file;
file = 0;
return;
}
startRequest(url,false);
ui->progressBar->setValue(0);
ui->progressBar->show();
}
void MainWindow::on_pushButton_2_clicked()
{
QUrl url;
url.setScheme("ftp");
url.setHost("192.168.1.9");
url.setPath("XunLei.exe");// 目标文件名
url.setUserName("adminFtp");
url.setPassword("123456");
startRequest(url,true);
ui->progressBar->setValue(0);
ui->progressBar->show();
}
运行结果:
来源:CSDN
作者:qq_24127015
链接:https://blog.csdn.net/qq_24127015/article/details/104669995