• Qt FTP文件上传下载简单例子


    一、简述

          记--Qt5 默认已经不支持QFtp了,需要自己编译。例子使用QNetworkAccessManager实现简单的ftp文件上传和下载。

          例子打包:外链:https://wwm.lanzouv.com/b0caushud 密码:1puy

    二、效果

    例子中在虚拟机Ubuntu16.04 搭建了ftp服务器,windwos主机跟Ubuntu虚拟机能ping通。

    用户名密码可以使用Ubuntu的账号,也可以自己添加新用户。

    三、工程结构

    四、源文件 

    mainwindow.h文件

    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #define FTP_PORT 21
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17. private slots:
    18. void on_pushButtonDownload_clicked();
    19. void slot_NetworkAccessManagerUploadProcess(QNetworkReply*);
    20. void slot_NetworkAccessManagerDownloadProcess(QNetworkReply*);
    21. void on_pushButtonUploadFile_clicked();
    22. private:
    23. Ui::MainWindow *ui;
    24. QNetworkAccessManager mNetworkAccessManagerUpload;
    25. QNetworkAccessManager mNetworkAccessManagerDownload;
    26. };
    27. #endif // MAINWINDOW_H

    main.cpp文件

    1. #include "mainwindow.h"
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. MainWindow w;
    7. w.show();
    8. return a.exec();
    9. }

    mainwindow.cpp文件

    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include
    4. #include
    5. #include
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. connect(&mNetworkAccessManagerUpload, SIGNAL(finished(QNetworkReply*)), SLOT(slot_NetworkAccessManagerUploadProcess(QNetworkReply*)));
    12. connect(&mNetworkAccessManagerDownload, SIGNAL(finished(QNetworkReply*)), SLOT(slot_NetworkAccessManagerDownloadProcess(QNetworkReply*)));
    13. //mNetworkAccessManagerDownload.downloadProgress 进度
    14. }
    15. MainWindow::~MainWindow()
    16. {
    17. delete ui;
    18. }
    19. //下载文件
    20. void MainWindow::on_pushButtonDownload_clicked()
    21. {
    22. QUrl url;
    23. url.setScheme("ftp");
    24. url.setHost(ui->lineEditHost->text());
    25. url.setPort(ui->lineEditPort->text().toInt());
    26. url.setUserName(ui->lineEditUserName->text());
    27. url.setPassword(ui->lineEditPwd->text());
    28. url.setPath(ui->lineEditDownloadUrl->text());
    29. mNetworkAccessManagerDownload.get((QNetworkRequest(url)));
    30. }
    31. //下载结果处理
    32. void MainWindow::slot_NetworkAccessManagerDownloadProcess(QNetworkReply *reply)
    33. {
    34. QNetworkReply::NetworkError error = reply->error();
    35. switch(error){
    36. case QNetworkReply::NoError:
    37. {
    38. QByteArray data = reply->readAll();//大文件可以使用QIODevice或reply->readData()
    39. QFileInfo fileInfo(ui->lineEditDownloadUrl->text());
    40. QFile file(fileInfo.fileName());
    41. if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) {
    42. QMessageBox::critical(this,"错误", "保存失败!");
    43. } else {
    44. //将内容写入文件
    45. file.write(data);
    46. file.close();
    47. QMessageBox::information(this,"提示", "下载成功!已保存到" + file.fileName());
    48. }
    49. }
    50. break;
    51. case QNetworkReply::HostNotFoundError:
    52. QMessageBox::critical(this,"错误", "Host Not Found!");
    53. break;
    54. case QNetworkReply::AuthenticationRequiredError:
    55. QMessageBox::critical(this,"错误", "AuthenticationRequiredError!");
    56. break;
    57. default:
    58. QMessageBox::critical(this,"错误", "未知错误! error:" + error);
    59. break;
    60. }
    61. }
    62. //上传文件
    63. void MainWindow::on_pushButtonUploadFile_clicked()
    64. {
    65. QString fileName = QFileDialog::getOpenFileName(this, "选择文件", ".", "文本文件(*.txt)");
    66. if (fileName.isEmpty()) {
    67. return;
    68. }
    69. QFile file(fileName);
    70. if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
    71. QMessageBox::critical(this,"错误", "打开文件失败!");
    72. return;
    73. }
    74. QByteArray data = file.readAll();
    75. file.close();
    76. QUrl url;
    77. url.setScheme("ftp");
    78. url.setHost(ui->lineEditHost->text());
    79. url.setPort(ui->lineEditPort->text().toInt());
    80. url.setUserName(ui->lineEditUserName->text());
    81. url.setPassword(ui->lineEditPwd->text());
    82. QFileInfo fileInfo(fileName);
    83. url.setPath(ui->lineEditUploadUrl->text().append("/").append(fileInfo.fileName()));
    84. mNetworkAccessManagerUpload.put(QNetworkRequest(url), data);
    85. }
    86. //上传结果处理
    87. void MainWindow::slot_NetworkAccessManagerUploadProcess(QNetworkReply *reply)
    88. {
    89. QNetworkReply::NetworkError error = reply->error();
    90. switch(error){
    91. case QNetworkReply::NoError:
    92. QMessageBox::information(this,"提示", "上传成功!");
    93. break;
    94. case QNetworkReply::HostNotFoundError:
    95. QMessageBox::critical(this,"错误", "Host Not Found!");
    96. break;
    97. case QNetworkReply::AuthenticationRequiredError:
    98. QMessageBox::critical(this,"错误", "Authentication Required Error!");
    99. break;
    100. default:
    101. QMessageBox::critical(this,"错误", "未知错误!");
    102. break;
    103. }
    104. }

    五、附

    例子中在虚拟机Ubuntu16.04 搭建了ftp服务器,windwos主机跟Ubuntu虚拟机能ping通。

    5.1 确认能ping通:

    5.2 确认ftp服务器已经启动

     5.3 Ubuntu16.04 安装ftp服务器

    1. //在线安装,一般安装成功后ftp服务器就自动启动了,使用ps -ef | grep vsftpd确认
    2. sudo apt-get install vsftpd
    3. //编辑配置文件, 启用write_enable=YES 不然不能上传文件
    4. sudo /etc/vsftpd.conf
    5. 修改配置后保存然后重启vsftpd,执行sudo /etc/init.d/vsftpd restart
    6. //如果之前安装过但是有问题,可以使用这一句进行移除
    7. sudo apt-get purge vsftpd

  • 相关阅读:
    核壳二氧化钛纳米颗粒修饰DNA|二氢杨梅素修饰DNA药物|相关介绍
    OpenCV官方教程中文版 —— 直方图的计算,绘制与分析
    通用深度模型UniverSeg:分割医疗图像
    企业知识库系统:从0开始,部署企业知识库模型
    使用easyexcel导出\入excel
    优维产品最佳实践:主机合规性检查
    抖音矩阵系统,抖音矩阵系统源码,抖音SEO源码、
    C#中+=的用法
    前端导出Excel并下载到本地
    食品级接触材料的检测标准有哪些?
  • 原文地址:https://blog.csdn.net/nanfeibuyi/article/details/126692569