diff --git a/QtHttpEx/ExHttp.cpp b/QtHttpEx/ExHttp.cpp new file mode 100644 index 0000000..33f04eb --- /dev/null +++ b/QtHttpEx/ExHttp.cpp @@ -0,0 +1,108 @@ +#include "ExHttp.h" +#include "ui_ExHttp.h" +#include +#include +#include + +ExHttp::ExHttp(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::ExHttp) +{ + ui->setupUi(this); + setWindowTitle("QNetworkAccessManager 网络管理使用 Http 协议下载"); + + m_networkManager = new QNetworkAccessManager(this); +} + +ExHttp::~ExHttp() +{ + delete ui; +} + +//下载文件 +void ExHttp::on_btnDown_clicked() +{ + QString urlSpec = ui->lineEditUrl->text().trimmed(); //去掉字符串的首尾的空格 + if (urlSpec.isEmpty()) { + QMessageBox::information(this, "提示", "下载地址URL为NULL"); + return; + } + + QUrl url = QUrl::fromUserInput(urlSpec); + if (!url.isValid()) { + QMessageBox::information(this, "提示", QString("无效URL: %1 \n 错误信息: %2").arg(urlSpec, url.errorString())); + return; + } + + QString dir = ui->lineEditFile->text().trimmed(); + if (dir.isEmpty()) { + QMessageBox::information(this, "提示", "保存地址为空"); + return; + } + + QString fileFileName = dir + url.fileName(); //文件保存地址 + 文件名 + if (QFile::exists(fileFileName)) + QFile::remove(fileFileName); + + m_file = new QFile(fileFileName); //创建临时文件 + if (!m_file->open(QIODevice::WriteOnly)) { + QMessageBox::information(this, "提示", "打开临时文件错误"); + return; + } + + ui->btnDown->setEnabled(false); + + m_reply = m_networkManager->get(QNetworkRequest(url)); //发送get网络请求,创建网络响应 + connect(m_reply, SIGNAL(finished()), this, SLOT(onFinished())); + connect(m_reply, SIGNAL(readyRead()), this, SLOT(onReadyRead())); + connect(m_reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(onDownloadProgress(qint64,qint64))); +} + +//默认的保存路径 +void ExHttp::on_btnFile_clicked() +{ + QString currPath = QDir::currentPath(); + QDir dir(currPath); + dir.mkdir("temp"); + + + ui->lineEditFile->setText(currPath + "/temp/"); +} + +//网络响应结束 +void ExHttp::onFinished() +{ + QFileInfo fileInfo; + fileInfo.setFile(m_file->fileName()); + + m_file->close(); + delete m_file; + m_file = nullptr; + + m_reply->deleteLater(); + m_reply = nullptr; + + if (ui->checkBox->isChecked()) //勾选了,下载完成之后,打开下载的文件 //absoluteFilePath() 返回包含文件名的绝对路径。 + QDesktopServices::openUrl(QUrl::fromLocalFile(fileInfo.absoluteFilePath())); //使用默认软件的打开下载的文件 + + ui->btnDown->setEnabled(true); +} + +//读取下载的数据 +void ExHttp::onReadyRead() +{ + m_file->write(m_reply->readAll()); //将返回的数据进行读取,写入到临时文件中 +} + +//下载进程 +void ExHttp::onDownloadProgress(qint64 bytesRea, qint64 totalBytes) +{ + ui->progressBar->setMaximum(totalBytes); + ui->progressBar->setValue(bytesRea); +} + +void ExHttp::on_lineEditUrl_textChanged(const QString &arg1) +{ + ui->progressBar->setMaximum(100); + ui->progressBar->setValue(0); +} diff --git a/QtHttpEx/ExHttp.cpp.autosave b/QtHttpEx/ExHttp.cpp.autosave new file mode 100644 index 0000000..dda83b2 --- /dev/null +++ b/QtHttpEx/ExHttp.cpp.autosave @@ -0,0 +1,109 @@ +#include "ExHttp.h" +#include "ui_ExHttp.h" +#include +#include +#include + +ExHttp::ExHttp(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::ExHttp) +{ + ui->setupUi(this); + setWindowTitle("QNetworkAccessManager 网络管理使用 Http 协议下载"); + + m_networkManager = new QNetworkAccessManager(this); + m_reply = nullptr; + m_file = nullptr; +} + +ExHttp::~ExHttp() +{ + delete ui; +} + +//下载文件 +void ExHttp::on_btnDown_clicked() +{ + QString urlSpec = ui->lineEditUrl->text().trimmed(); //去掉字符串的首尾的空格 + if (urlSpec.isEmpty()) { + QMessageBox::information(this, "提示", "下载地址URL为NULL"); + return; + } + + QUrl url = QUrl::fromUserInput(urlSpec); + if (!url.isValid()) { + QMessageBox::information(this, "提示", QString("无效URL: %1 \n 错误信息: %2").arg(urlSpec, url.errorString())); + return; + } + + QString dir = ui->lineEditFile->text().trimmed(); + if (dir.isEmpty()) { + QMessageBox::information(this, "提示", "保存地址为空"); + return; + } + + QString fileFileName = dir + url.fileName(); //文件保存地址 + 文件名 + if (QFile::exists(fileFileName)) + QFile::remove(fileFileName); + + m_file = new QFile(fileFileName); //创建临时文件 + if (!m_file->open(QIODevice::WriteOnly)) { + QMessageBox::information(this, "提示", "打开临时文件错误"); + return; + } + + ui->btnDown->setEnabled(false); + + m_reply = m_networkManager->get(QNetworkRequest(url)); //发送get网络请求,创建网络响应 + connect(m_reply, SIGNAL(finished()), this, SLOT(onFinished())); + connect(m_reply, SIGNAL(readyRead()), this, SLOT(onReadyRead())); + connect(m_reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(onDownloadProgress(qint64,qint64))); +} + +//默认的保存路径 +void ExHttp::on_btnFile_clicked() +{ + QString currPath = QDir::currentPath(); + QDir dir(currPath); + dir.mkdir("temp"); + + ui->lineEditFile->setText(currPath + "/temp/"); +} + +//网络响应结束 +void ExHttp::onFinished() +{ + QFileInfo fileInfo; + fileInfo.setFile(m_file->fileName()); + + m_file->close(); + delete m_file; + m_file = nullptr; + + m_reply->deleteLater(); + m_reply = nullptr; + + if (ui->checkBox->isChecked()) //勾选了,下载完成之后,打开下载的文件 //absoluteFilePath() 返回包含文件名的绝对路径。 + QDesktopServices::openUrl(QUrl::fromLocalFile(fileInfo.absoluteFilePath())); //使用默认软件的打开下载的文件 + + ui->btnDown->setEnabled(true); +} + +//读取下载的数据 +void ExHttp::onReadyRead() +{ + m_file->write(m_reply->readAll()); //将返回的数据进行读取,写入到临时文件中 +} + +//下载进程 +void ExHttp::onDownloadProgress(qint64 bytesRea, qint64 totalBytes) +{ + ui->progressBar->setMaximum(totalBytes); + ui->progressBar->setValue(bytesRea); +} + +void ExHttp::on_lineEditUrl_textChanged(const QString &arg1) +{ + ui->progressBar->setMaximum(100); + ui->progressBar->setValue(0); +} diff --git a/QtHttpEx/ExHttp.h b/QtHttpEx/ExHttp.h new file mode 100644 index 0000000..e65f327 --- /dev/null +++ b/QtHttpEx/ExHttp.h @@ -0,0 +1,40 @@ +#ifndef EXHTTP_H +#define EXHTTP_H + +#include +#include +#include +#include +#include +#include + +namespace Ui { +class ExHttp; +} + +class ExHttp : public QMainWindow +{ + Q_OBJECT + +public: + explicit ExHttp(QWidget *parent = nullptr); + ~ExHttp(); + +private slots: + void on_btnDown_clicked(); //下载文件 + void on_btnFile_clicked(); //默认的保存路径 + void on_lineEditUrl_textChanged(const QString &arg1); + + void onFinished(); //网络响应结束 + void onReadyRead(); //读取下载的数据 + void onDownloadProgress(qint64 bytesRea, qint64 totalBytes); //下载进程 + +private: + Ui::ExHttp *ui; + + QNetworkAccessManager* m_networkManager; //网络管理 + QNetworkReply* m_reply; //网络响应 + QFile* m_file; //下载保存的临时文件 +}; + +#endif // EXHTTP_H diff --git a/QtHttpEx/ExHttp.ui b/QtHttpEx/ExHttp.ui new file mode 100644 index 0000000..a9dd415 --- /dev/null +++ b/QtHttpEx/ExHttp.ui @@ -0,0 +1,89 @@ + + + ExHttp + + + + 0 + 0 + 555 + 111 + + + + ExHttp + + + + + + + + + + + + 默认路径 + + + + + + + http://download.qt.io/archive/vsaddin/2.4.2/md5sums.txt + + + + + + + URL: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + 下载 + + + + + + + 保存路径: + + + + + + + 0 + + + true + + + QProgressBar::TopToBottom + + + + + + + 结束之后打开 + + + + + + + + + + + + diff --git a/QtHttpEx/QtHttpEx.pro b/QtHttpEx/QtHttpEx.pro new file mode 100644 index 0000000..58c3cec --- /dev/null +++ b/QtHttpEx/QtHttpEx.pro @@ -0,0 +1,56 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2019-12-05T21:58:28 +# +#------------------------------------------------- + +QT += core gui network + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = QtHttpEx +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +CONFIG += c++11 + +SOURCES += \ + main.cpp \ + ExHttp.cpp + +HEADERS += \ + ExHttp.h + +FORMS += \ + ExHttp.ui + +macx { +ICON = images/icon.icns +} + +unix:!macx{ +# linux only +} + +win32 { +RC_ICONS = images/icon.ico +} + + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target + +RESOURCES += \ + resources.qrc diff --git a/QtHttpEx/images/Image1.png b/QtHttpEx/images/Image1.png new file mode 100644 index 0000000..c2c4e0d Binary files /dev/null and b/QtHttpEx/images/Image1.png differ diff --git a/QtHttpEx/images/Image2.jpg b/QtHttpEx/images/Image2.jpg new file mode 100644 index 0000000..46070af Binary files /dev/null and b/QtHttpEx/images/Image2.jpg differ diff --git a/QtHttpEx/images/Image3.png b/QtHttpEx/images/Image3.png new file mode 100644 index 0000000..c9045da Binary files /dev/null and b/QtHttpEx/images/Image3.png differ diff --git a/QtHttpEx/images/Image4.png b/QtHttpEx/images/Image4.png new file mode 100644 index 0000000..0319033 Binary files /dev/null and b/QtHttpEx/images/Image4.png differ diff --git a/QtHttpEx/images/Image5.png b/QtHttpEx/images/Image5.png new file mode 100644 index 0000000..95ecf80 Binary files /dev/null and b/QtHttpEx/images/Image5.png differ diff --git a/QtHttpEx/images/Image6.png b/QtHttpEx/images/Image6.png new file mode 100644 index 0000000..d246ada Binary files /dev/null and b/QtHttpEx/images/Image6.png differ diff --git a/QtHttpEx/images/icon.icns b/QtHttpEx/images/icon.icns new file mode 100755 index 0000000..4f20853 Binary files /dev/null and b/QtHttpEx/images/icon.icns differ diff --git a/QtHttpEx/images/icon.ico b/QtHttpEx/images/icon.ico new file mode 100755 index 0000000..0ee0568 Binary files /dev/null and b/QtHttpEx/images/icon.ico differ diff --git a/QtHttpEx/main.cpp b/QtHttpEx/main.cpp new file mode 100644 index 0000000..a05d5d6 --- /dev/null +++ b/QtHttpEx/main.cpp @@ -0,0 +1,11 @@ +#include "ExHttp.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + ExHttp w; + w.show(); + + return a.exec(); +} diff --git a/QtHttpEx/resources.qrc b/QtHttpEx/resources.qrc new file mode 100644 index 0000000..143a7fb --- /dev/null +++ b/QtHttpEx/resources.qrc @@ -0,0 +1,6 @@ + + + images/Image6.png + images/Image3.png + + diff --git a/README.md b/README.md index 7306c9f..d0a0a1b 100644 --- a/README.md +++ b/README.md @@ -75,8 +75,9 @@ ## 第四部分:网络编程 -- 主机信息查询QHostInfo和QNetworkInterface查询IP等【QtQHostInfoEx】 -- TCP通信之QTcpServer和QTcpSocket,服务器和客户端通讯【QtTcpEx】 +- 主机信息查询`QHostInfo`和`QNetworkInterface`查询IP等【QtQHostInfoEx】 +- `TCP`通信之`QTcpServer`和`QTcpSocket`,服务器和客户端通讯【QtTcpEx】 +- `QNetworkAccessManager`/`QNetworkReply`/`QNetworkRequest`实现高层网络的操作