• QT—基于http协议的网络文件下载


    1.常用到的类

    QNetworkAccessManager类用于协调网络操作,负责发送网络请求,创建网络响应

    QNetworkReply类表示网络请求的响应。在QNetworkAccessManager发送一个网络请求后创建一个网络响应。它提供了以下信号:

    finished():完成后发出信号

    readyRead():有数据读的数据发出信号

    downloadProgress():表示网络操作进度的信号,有bytesRead和totalBytes两个参数,表示已读取字节数和总字节数

    QNetworkReply 是QIODevice的子类,所以QNetworkReply支持流读写功能,也支持异步或同步工作。

    2.代码

    .h

    1. #ifndef HTTP_H
    2. #define HTTP_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. QT_BEGIN_NAMESPACE
    9. namespace Ui { class http; }
    10. QT_END_NAMESPACE
    11. class http : public QMainWindow
    12. {
    13. Q_OBJECT
    14. public:
    15. http(QWidget *parent = nullptr);
    16. ~http();
    17. private:
    18. Ui::http *ui;
    19. QNetworkAccessManager networkManager;
    20. QNetworkReply *reply;
    21. QFile *downFile;
    22. public slots:
    23. void on_finished();
    24. void on_readyRead();
    25. void downLoadProgress(qint64 bytesRead,qint64 totalBytes);
    26. private slots:
    27. void on_pushButton_2_clicked();
    28. void on_pushButton_clicked();
    29. };
    30. #endif // HTTP_H
    31. #ifndef HTTP_H
    32. #define HTTP_H
    33. #include
    34. #include
    35. #include
    36. #include
    37. #include
    38. QT_BEGIN_NAMESPACE
    39. namespace Ui { class http; }
    40. QT_END_NAMESPACE
    41. class http : public QMainWindow
    42. {
    43. Q_OBJECT
    44. public:
    45. http(QWidget *parent = nullptr);
    46. ~http();
    47. private:
    48. Ui::http *ui;
    49. QNetworkAccessManager networkManager;
    50. QNetworkReply *reply;
    51. QFile *downFile;
    52. public slots:
    53. void on_finished();
    54. void on_readyRead();
    55. void downLoadProgress(qint64 bytesRead,qint64 totalBytes);
    56. private slots:
    57. void on_pushButton_2_clicked();
    58. void on_pushButton_clicked();
    59. };
    60. #endif // HTTP_H

    .cpp

    1. #include "http.h"
    2. #include "ui_http.h"
    3. #include
    4. http::http(QWidget *parent)
    5. : QMainWindow(parent)
    6. , ui(new Ui::http)
    7. {
    8. ui->setupUi(this);
    9. }
    10. http::~http()
    11. {
    12. delete ui;
    13. }
    14. void http::on_finished()
    15. {
    16. QFileInfo fileInfo;
    17. fileInfo.setFile(downFile->fileName());
    18. downFile->close();
    19. delete downFile;
    20. reply->deleteLater();
    21. reply = nullptr;
    22. ui->pushButton->setEnabled(true);
    23. }
    24. void http::on_readyRead()
    25. {
    26. downFile->write(reply->readAll());
    27. }
    28. void http::downLoadProgress(qint64 bytesRead, qint64 totalBytes)
    29. {
    30. ui->progressBar->setMaximum(totalBytes);
    31. ui->progressBar->setValue(bytesRead);
    32. }
    33. void http::on_pushButton_2_clicked()
    34. {
    35. //缺省路径
    36. QString curPath = QDir::currentPath();
    37. QDir dir(curPath);
    38. QString sub = "temp";
    39. dir.mkdir(sub);
    40. ui->lineEdit_2->setText(curPath + "/" + sub + "/");
    41. }
    42. void http::on_pushButton_clicked()
    43. {
    44. //下载
    45. QString url = ui->lineEdit->text().trimmed();
    46. if(url.isEmpty())
    47. return;
    48. QUrl newUrl = QUrl::fromUserInput(url);
    49. if(!newUrl.isValid())
    50. return;
    51. QString tempDir = ui->lineEdit_2->text().trimmed();
    52. if(tempDir.isEmpty())
    53. return;
    54. QString fullFileName = tempDir+newUrl.fileName();
    55. if(QFile::exists(fullFileName))
    56. QFile::remove(fullFileName);
    57. downFile = new QFile(fullFileName);
    58. if(!downFile->open(QIODevice::WriteOnly))
    59. return;
    60. ui->pushButton->setEnabled(false);
    61. reply = networkManager.get(QNetworkRequest(newUrl));
    62. connect(reply,&QNetworkReply::finished,this,&http::on_finished);
    63. connect(reply,&QNetworkReply::readyRead,this,&http::on_readyRead);
    64. connect(reply,&QNetworkReply::downloadProgress,this,&http::downLoadProgress);
    65. }

    结果:

    点击缺省路径,保存文件

    在地址栏输入下载的文件地址,例:下载网易云音乐logo图片,地址如下

    http://p3.music.126.net/tBTNafgjNnTL1KlZMt7lVA==/18885211718935735.jpg

    原文链接:https://blog.csdn.net/wzz953200463/article/details/123858775 

  • 相关阅读:
    Redis 通用命令(keys,help,mset,exists,expire,ttl,tab补全)
    PaadleInference源码编译操作流程
    移动端适配推荐flexible和 postcss-px2rem
    【Skynet 入门实战练习】开发环境搭建 | 运行第一个项目 | debug console 简单使用
    量子计算(五):量子计算的发展
    MySQL
    Vue3+TypeScript+Vite如何使用require动态引入类似于图片等静态资源
    Conda详细介绍
    应用软件安全编程--21密钥长度应该足够长
    极客时间Kafka - 05 Kafka 生产者发送消息可靠性保障|幂等生产者和事务生产者
  • 原文地址:https://blog.csdn.net/m0_75179090/article/details/132694400