代码如下所示:
void UploadZipFileToServer(const QString& strPath)
{
m_pFile = new QFile(strPath);
// 创建HTTP多部份请求
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart keyPart;
keyPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=\"%1\"").arg("id")));
keyPart.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain;charset=UTF-8");
keyPart.setBody(m_strModelID.toUtf8());
multiPart->append(keyPart);
QString strMdlName = QFileInfo(strPath).fileName();
QHttpPart modelNamePart;
modelNamePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=\"%1\"").arg("name")));
modelNamePart.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain;charset=UTF-8");
modelNamePart.setBody(strMdlName.toUtf8());
multiPart->append(modelNamePart);
QHttpPart filePart;
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=\"%1\"; filename=\"%2\"").arg("file").arg(m_pFile->fileName())));
filePart.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain;charset=UTF-8");
m_pFile->open(QIODevice::ReadOnly);
filePart.setBodyDevice(m_pFile);
m_pFile->setParent(multiPart);
multiPart->append(filePart);
QHttpPart tokenPart;
tokenPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=\"%1\"").arg("level")));
tokenPart.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain;charset=UTF-8");
tokenPart.setBody(m_strClassification.toUtf8());
multiPart->append(tokenPart);
QHttpPart authorizationPart;
authorizationPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=\"%1\"").arg("model")));
authorizationPart.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain;charset=UTF-8");
authorizationPart.setBody(m_strType.toUtf8());
multiPart->append(authorizationPart);
QHttpPart metafiled;
metafiled.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=\"%1\"").arg("zone")));
metafiled.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain;charset=UTF-8");
metafiled.setBody(m_strCooperationZone.toUtf8());
multiPart->append(metafiled);
//QUrl url("http://192.168.2.59:9090/fileservice/fileupload.do");
QUrl url(m_strIp);
// 请求URL
QNetworkRequest request(url);
QNetworkAccessManager *manager = new QNetworkAccessManager();
m_pReply = manager->post(request, multiPart);
multiPart->setParent(m_pReply);
connect(m_pReply, SIGNAL(finished()), this, SLOT(httpUploadFinished()));
connect(m_pReply, SIGNAL(uploadProgress(qint64, qint64)), this, SLOT(uploadProgress(qint64, qint64)));
QEventLoop eventLoop;
connect(manager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
eventLoop.exec();
}
void httpUploadFinished()
{
// 测试代码
//QString jsonString = u8R"({
//"code":"1001",
//"Data": "打完为Joi",
//"Message": "测试测试",
//"Title": "返回报错信息"
//})";
//QByteArray byteResonse = jsonString.toUtf8();
// 服务器采用UTF-8编码返回数据
QByteArray byteResonse = m_pReply->readAll();
// 将服务器返回的数据写入Log文件便于观察
QString responseString = QString::fromUtf8(byteResonse);
{
QFile file("ServerResLog.txt");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
return;
}
QTextStream out(&file);
out.setCodec("UTF-8");
out << responseString;
file.close();
}
// 处理服务器返回的数据
QJsonParseError error;
QJsonDocument jsonDoc = QJsonDocument::fromJson(byteResonse, &error);
if (!jsonDoc.isNull())
{
QJsonObject jsonObj = jsonDoc.object();
QString strCode = jsonObj["code"].toString(); // 获取 code 字段的值
if ("1000" == strCode)
{
QMessageBox::information(nullptr, QStringLiteral("上传成功"), QStringLiteral("上传成功!"));
return;
}
else if ("1001" == strCode)
{
QString dataValue = jsonObj["Data"].toString(); // 获取 Data 字段的值
QString message = jsonObj["Message"].toString().toUtf8(); // 获取 Message 字段的值
QString title = jsonObj["Title"].toString().toUtf8(); // 获取 Title 字段的值
QMap<QString, QString> mapTemp;
mapTemp.insert("code", strCode);
mapTemp.insert("Data", dataValue);
mapTemp.insert("Message", message);
mapTemp.insert("Title", title);
ReplyMessageShow dlg(mapTemp);
if (dlg.exec() == QDialog::Accepted) {}
}
else
{
QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("解析返回json数据失败!"));
return;
}
}
else
{
QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("未连接到正确服务器或服务器返回json数据为空!"));
return;
}
// 测试JSON解析代码
//QString jsonString = u8R"({
//"code":1001,
//"Data": null,
//"Message": "Error!",
//"Title": "返回报错信息"
//})";
//QByteArray byteResonse = jsonString.toUtf8();
//QString strTemp = QString::fromUtf8(byteResonse);
//QJsonParseError error;
//QJsonDocument jsonDoc = QJsonDocument::fromJson(utf8Data, &error);
获取 JSON 对象
//QJsonObject jsonObj = jsonDoc.object();
从 JSON 对象中提取数据
//int code = jsonObj["code"].toInt();
//QString data = jsonObj["Data"].toString();
//QString message = jsonObj["Message"].toString().toUtf8();
//QString title = jsonObj["Title"].toString().toUtf8();
//QMap<QString, QString> mapTemp;
//mapTemp.insert("code", QString::number(code));
//mapTemp.insert("Data", data);
//mapTemp.insert("Message", message);
//mapTemp.insert("Title", title);
//ReplyMessageShow dlg(mapTemp);
//if (dlg.exec() == QDialog::Accepted) {}
}
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » QT实现上传服务器功能
发表评论 取消回复