当前位置: 首页 > news >正文

QT通过url下载http地址下的文件(文件夹)

前言

之前只写过通过http协议通信,没有写过下载http地址中的文件或者文件夹,了解一下在QT下如何下载。

其实很简单,同使用协议通信相同的是,创建QNetworkAccessManager和QNetworkRequest,设置QNetworkRequest的url,通过get请求接收QNetworkReply中数据,利用downloadProgress信号接收每次下载下来的内容,直到下载完成。

代码如下:

 .pro

主要是加上network模块

#-------------------------------------------------
#
# Project created by QtCreator 2023-10-26T18:29:32
#
#-------------------------------------------------QT       += core gui networkgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = load_http_file
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.0SOURCES += \main.cpp \widget.cpp \HEADERS += \widget.h \FORMS += \widget.ui

界面:

 widget.h

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QFile>
#include <QUrl>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QEventLoop>
#include <QTimer>namespace Ui {
class Widget;
}class Widget : public QWidget
{Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();bool downloadFile(QString url);
private slots:void on_load_zip_clicked();private:Ui::Widget *ui;private:bool flag_download = false;
};#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include <QDir>
#include <QThread>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{}Widget::~Widget()
{delete ui;
}void Widget::on_load_zip_clicked()
{downloadFile(ui->lineEdit_url->text());
}bool Widget::downloadFile(QString url)
{if(flag_download){qDebug()<<"有正在下载中的文件,已停止当前下载。";return false;}int timeout = 1000 * 60; //* 3;QString path = "3D";QDir dir(path);if(!dir.exists()){if(!dir.mkdir(dir.absolutePath())){qDebug()<<"创建3D文件夹失败!";return false;}}path = path + "/" + url.section('/',-1,-1);qDebug()<<"url:"<<url<<" path:"<<path;QFile file(path);if (!file.open(QIODevice::WriteOnly)){qDebug()<<"打开待下载文件失败!";return false;}flag_download = true;QNetworkAccessManager networkManager;QNetworkRequest request;request.setUrl(QUrl(url));QNetworkReply *reply = networkManager.get(request);QTimer timer;QEventLoop eventLoop;connect(reply, &QNetworkReply::downloadProgress, [=, &file, &timer](qint64 bytesReceived,qint64 bytesTotal){qDebug()<<"当前下载的文件大小:"<<bytesReceived<<"   总文件大小:"<<bytesTotal;if (timer.isActive())timer.start(timeout);file.write(reply->readAll());});connect(reply, &QNetworkReply::finished, &timer, &QTimer::stop);connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);connect(&timer, &QTimer::timeout, &eventLoop, &QEventLoop::quit);timer.start(timeout);eventLoop.exec();//QEventLoop::ExcludeUserInputEventsflag_download = false;if (reply->error() != QNetworkReply::NoError){qDebug()<<"请求失败!失败原因:"<<reply->error();file.close();delete reply;return false;}if(timer.isActive()){qDebug()<<"请求超时!";timer.stop();file.close();delete reply;return false;}qDebug()<<"下载3D文件成功!";file.close();delete reply;return true;
}
http://www.lryc.cn/news/213967.html

相关文章:

  • 测试实施运维必备知识点
  • RTSP/Onvif安防视频平台EasyNVR接入EasyNVS,出现Login error报错的解决方法
  • 在Linux环境下远程访问MeterSphere开源测试平台
  • ARPG----C++学习记录02 Section6位置,偏移,函数
  • 怎么在现货黄金交易过程中高效设置止损?
  • centos做个登录提醒
  • 由QTableView/QTableWidget显示进度条和按钮,理解qt代理delegate用法
  • pthread_cond_timedwait 修改系统时间竟会导致其提前结束
  • Linux命令超详细
  • 物理机、虚拟机、容器
  • CSS画三角形(三种方法)
  • (一)、ts 基础类型 及class类举例字符雨和实现vue的挂在#app
  • C++对象的内存分布和虚函数表
  • 小白怎么学习性能测试?一文7个知识点带你成功入门!
  • Orcad属性过滤器的使用技巧
  • 腾讯云向量数据库正式对外全量开放公测
  • Linux新建普通用户无法使用退格键与tab键
  • 【湘粤鄂车牌】
  • 华大-HC32L130F8UA 内存使用注意事项
  • 怎样才知道一个单片机的性能到极限了?
  • Android Studio的笔记--SerialPort串口通讯学习和使用
  • MySQL 启动选项和字符集
  • 社区投稿|解码Big Vector,开启Sui超扩展性的新篇章
  • Linux根目录下的目录结构及其作用详解
  • 源码和SaaS账号:租房与自建房的区别
  • Docker容器设置为自动重启
  • 速卖通卖家如何通过自己搭建测评补单系统,提高产品权重和排名?
  • 香港金融科技周2023:AIGC重塑金融形态
  • 6G关键新兴技术-智能超表面(RIS)技术演进
  • 怎么让小程序排名靠前?小程序搜索排名问题