基于HTTP协议的网络应用程序
2021/9/9 17:03:56
本文主要是介绍基于HTTP协议的网络应用程序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QFile>
#include <QDir>
#include <QMessageBox>
#include <QDesktopServices>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
QNetworkAccessManager networkManager; //网络管理
QNetworkReply *reply; //网络响应
QFile *downloadedFile; //下载保存的临时文件
private slots:
void on_finished();
void on_readyRead();
void on_downloadProgress(qint64 bytesRead ,qint64 totalBytes);
void on_pushButton_2_clicked();
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->progressBar->setValue(0);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_finished()
{
QFileInfo fileInfo;
fileInfo.setFile(downloadedFile->fileName());
downloadedFile->close();
delete downloadedFile;
downloadedFile = Q_NULLPTR;
reply->deleteLater();
reply = Q_NULLPTR;
if(ui->checkBox->isChecked())
{
QDesktopServices::openUrl(QUrl::fromLocalFile(fileInfo.absoluteFilePath()));
ui->pushButton->setEnabled(true);
}
}
void MainWindow::on_readyRead()
{
downloadedFile->write(reply->readAll());
}
void MainWindow::on_downloadProgress(qint64 bytesRead, qint64 totalBytes)
{
ui->progressBar->setMaximum(totalBytes);
ui->progressBar->setValue(bytesRead);
}
void MainWindow::on_pushButton_2_clicked()
{
//缺省路径
QString curPath = QDir::currentPath();
QDir dir(curPath);
QString sub = "temp";
dir.mkdir(sub);
ui->lineEdit_2->setText(curPath+"/"+sub+"/");
}
void MainWindow::on_pushButton_clicked()
{
//下载
QString urlSpec =ui->lineEdit->text().trimmed();
if(urlSpec.isEmpty())
{
QMessageBox::information(this,"错误","请指定需要下载的URL");
return;
}
QUrl newurl = QUrl::fromUserInput(urlSpec);
if(!newurl.isValid())
{
QMessageBox::information(this,"错误",QString("无效URL:%1\n错误信息:%2").arg(urlSpec,newurl.errorString()));
return;
}
QString tempDir = ui->lineEdit_2->text().trimmed();
if(tempDir.isEmpty())
{
QMessageBox::information(this,"错误","请指定保存下载文件的路径");
return;
}
QString fullFileName = tempDir +newurl.fileName();
if(QFile::exists(fullFileName))
{
QFile::remove(fullFileName);
}
downloadedFile = new QFile(fullFileName);
if(!downloadedFile->open(QIODevice::WriteOnly))
{
QMessageBox::information(this,"错误","临时文件打开错误");
return;
}
ui->pushButton->setEnabled(false);
reply = networkManager.get(QNetworkRequest(newurl));
connect(reply,SIGNAL(finished()),this,SLOT(on_finished()));
connect(reply,SIGNAL(readyRead()),this,SLOT(on_readyRead()));
connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(on_downloadProgress(qint64,qint64)));
}
这篇关于基于HTTP协议的网络应用程序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26Mybatis官方生成器资料详解与应用教程
- 2024-11-26Mybatis一级缓存资料详解与实战教程
- 2024-11-26Mybatis一级缓存资料详解:新手快速入门
- 2024-11-26SpringBoot3+JDK17搭建后端资料详尽教程
- 2024-11-26Springboot单体架构搭建资料:新手入门教程
- 2024-11-26Springboot单体架构搭建资料详解与实战教程
- 2024-11-26Springboot框架资料:新手入门教程
- 2024-11-26Springboot企业级开发资料入门教程
- 2024-11-26SpringBoot企业级开发资料详解与实战教程
- 2024-11-26Springboot微服务资料:新手入门全攻略