本文主要是介绍qt post请求 c++ 解压缩文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
qt post请求 c++下载代码:
默认下载保存路径:
linux qt调用 unzip进行解压缩
pro文件配置:
QT += network
qt post请求 c++下载代码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QUrl>// int main(int argc, char *argv[])
// {
// QApplication a(argc, argv);
// MainWindow w;
// w.show();
// return a.exec();
// }int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);QNetworkAccessManager manager;QUrl url("http://192.168.1.5:8099/external/inside/user/gAvatarZip");QNetworkRequest request(url);request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");QJsonObject json;json["macAddress"] = "4564646841";QJsonArray userIds;userIds.append(10324);json["userIds"] = userIds;QJsonDocument doc(json);QByteArray data = doc.toJson();QNetworkReply *reply = manager.post(request, data);QObject::connect(reply, &QNetworkReply::finished, [&]() {if (reply->error() == QNetworkReply::NoError) {QByteArray responseData = reply->readAll();QFile file("00101.zip");if (file.open(QIODevice::WriteOnly)) {file.write(responseData);file.close();qDebug() << "请求成功!文件已保存。";} else {qDebug() << "文件写入失败!";}} else {qDebug() << "请求失败:" << reply->errorString();}reply->deleteLater();});return a.exec();
}
默认下载保存路径:
build\Desktop_Qt_6_7_1_MinGW_64_bit-Debug
linux qt调用 unzip进行解压缩
#include <QCoreApplication>
#include <QProcess>
#include <QDebug>int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);// 创建 QProcess 实例QProcess process;// 定义要解压的 ZIP 文件路径和目标目录QString zipFilePath = "00101.zip";QString targetDirectory = "001";// 调用 unzip 命令process.start("unzip", QStringList() << zipFilePath << "-d" << targetDirectory);// 等待命令执行结束if (process.waitForFinished(-1)) { // -1 表示无超时qDebug() << "解压成功。";qDebug() << "Output:" << process.readAllStandardOutput();} else {qDebug() << "解压失败。";qDebug() << "Error:" << process.errorString();}return a.exec();
}
这篇关于qt post请求 c++ 解压缩文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!