当前位置:网站首页>Qt通过Url下载文件
Qt通过Url下载文件
2022-06-30 08:33:00 【艾米莉亚糖】
加入网络模块
在pro文件中加入:QT += network
添加头文件
需要依赖写文件以及网络的头文件:
#include <QFile>
#include <QUrl>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QEventLoop>
#include <QTimer>
具体实现
是以阻塞的方式去获取文件的,根据需要看是否要放到线程中执行。
如果需要实现断点续传的功能的话则需要的请求的头中指定文件偏移的位置,以及记录本地文件的偏移位置。
/**
* @brief 从网址下载一个文件保存
* @param url 需要下载文件的url地址
* @param filePath 需要保存的文件名
* @param timeout 超时多长时间后强制退出
* @return 返回true成功,返回false则下载失败
*/
bool downloadFileFromURL(const QString &url, const QString &filePath, int timeout = -1)
{
QFile f(filePath);
if (!f.open(QIODevice::WriteOnly)) {
return false;
}
QNetworkAccessManager m;
QNetworkRequest req;
req.setUrl(QUrl(url));
QNetworkReply *reply = m.get(req);
QEventLoop loop;
QTimer t;
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
QObject::connect(reply, &QNetworkReply::downloadProgress,
[=, &f, &t](){
f.write(reply->readAll());
if (t.isActive()) {
t.start(timeout);
}
});
if (timeout > 0) {
QObject::connect(&t, &QTimer::timeout, &loop, &QEventLoop::quit);
t.start(timeout);
}
loop.exec();
if (reply->error() != QNetworkReply::NoError) {
return false;
}
f.close();
delete reply;
return true;
}
边栏推荐
- 国债逆回购绝对安全吗 网上怎么开户
- Vite project require syntax compatibility problem solving require is not defined
- vite項目require語法兼容問題解决require is not defined
- [untitled]
- Unity basic lighting model
- Cesium learning notes (IV) visual image & Terrain
- 小心transmittable-thread-local的这个坑
- Implementation of remote monitoring by camera in Experiment 5
- Interference source current spectrum test of current probe
- Unit Test
猜你喜欢
【NVMe2.0b 14-1】Abort、Asynchronous Event Request、Capacity Management command
Deploy the cow like customer network project on the ECS
【NVMe2.0b 14-3】Doorbell Buffer Config command、Device Self-test command
【kotlin 协程】万字协程 一篇完成kotlin 协程进阶
Self made GIF dynamic graph -gifcam
Redis design and Implementation (VI) | cluster (sharding)
示波器探头对测量电容负荷有影响吗?
Cesium learning notes (VI) particle system
Wsl2 using GPU for deep learning
1. Problems related to OpenGL window and environment configuration
随机推荐
【NVMe2.0b 14-5】Firmware Download/Commit command
Redis design and Implementation (III) | interaction between server and client (event IO model)
[untitled]
【NVMe2.0b 14】NVMe Admin Command Set
Sword finger offer II 074 Merge interval (sort, array)
vite項目require語法兼容問題解决require is not defined
Redis design and Implementation (VII) | publish & subscribe
Unity简单shader
File upload component on success event, add custom parameters
Detectron2 source code reading 2--- using the configurable decorator to build the dataloader
Does the oscilloscope probe affect the measurement of capacitive load?
电流探头电路分析
layer.open 当传值为数组或值太长时处理方法
2021-02-22
C# Console. Writeline() function output format
Circuit analysis of current probe
Conversion between map, string and JSON
Cesium learning notes (IV) visual image & Terrain
End-to-end 3D Point Cloud Instance Segmentation without Detection
Graffiti Wi Fi & ble SoC development slide strip