目录
1、定位
以zip文件为例,先看一下上传时抓的包:
2、形式1 —— 文件流
文件流形式上传zip文件
void SNetWork::uploadFile(QString filePath, QByteArray rand, QByteArray dateTime)
{
QFile file(filePath);
file.open(QIODevice::ReadOnly);
QByteArray data = file.readAll();
file.close();
m_requestGetEquipmentStatus->setUrl(QUrl(PRODUCT_UPLOAD));
m_requestGetEquipmentStatus->setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream");
QByteArray rand_array = QCryptographicHash::hash(rand+"mac|2017", QCryptographicHash::Sha1).toHex().toUpper();
QByteArray key_array = QCryptographicHash::hash(dateTime +"-ev|"+rand_array, QCryptographicHash::Sha256).toHex().toLower();
m_requestGetEquipmentStatus->setRawHeader("t", dateTime);
m_requestGetEquipmentStatus->setRawHeader("salt", rand);
m_requestGetEquipmentStatus->setRawHeader("key", key_array);
QNetworkReply* m_netReply = m_manager->post(*m_requestGetEquipmentStatus, data.toBase64()); // 这里是把文件转成base64的,好处是,防止文件传的过程因为符号,空格的原因,导致文件数据失真。
connect(m_netReply, &QNetworkReply::uploadProgress, [=](qint64 a,qint64 b){
qDebug() << "m_netReply uploadProgress" << a << b;
});
connect(m_netReply, &QNetworkReply::finished, [=](){
qDebug() << "m_netReply finished";
});
}
3、形式2 —— 表单
表单形式上传zip文件
void SNetWork::uploadFile(QString filePath, QByteArray rand, QByteArray dateTime)
{
QFile file(filePath);
file.open(QIODevice::ReadOnly);
QByteArray data = file.readAll();
QFileInfo fineInfo(file);
QString fileName = fineInfo.fileName();
file.close();
QString boundary = "----weasdashDsdesd";
QString startBoundary = "--" + boundary; //起始边界
QString endBoundary = "\r\n--" + boundary + "--\r\n"; //结束边界
QString qContentType = "multipart/form-data; boundary=" + boundary; //设置传输类型
QByteArray content;
QString tempStr = startBoundary;
tempStr += "\r\nContent-Disposition: form-data; name=\"data\"; filename=\""+fileName+"\"\r\n";
tempStr += "Content-Type: application/zip \r\n\r\n";
content.append(tempStr.toLatin1());
content.append(data);
content.append(endBoundary);
m_requestGetEquipmentStatus->setUrl(QUrl(PRODUCT_UPLOAD));
m_requestGetEquipmentStatus->setHeader(QNetworkRequest::ContentTypeHeader, qContentType);
QByteArray rand_array = QCryptographicHash::hash(rand+"mac|2017", QCryptographicHash::Sha1).toHex().toUpper();
QByteArray key_array = QCryptographicHash::hash(dateTime +"-ev|"+rand_array, QCryptographicHash::Sha256).toHex().toLower();
m_requestGetEquipmentStatus->setRawHeader("t", dateTime);
m_requestGetEquipmentStatus->setRawHeader("salt", rand);
m_requestGetEquipmentStatus->setRawHeader("key", key_array);
QNetworkReply* m_netReply = m_manager->post(*m_requestGetEquipmentStatus, content);
connect(m_netReply, &QNetworkReply::uploadProgress, [=](qint64 a,qint64 b){
qDebug() << "m_netReply uploadProgress" << a << b;
});
connect(m_netReply, &QNetworkReply::finished, [=](){
qDebug() << "m_netReply finished";
});
}
4、注意事项
注意第2种以表单(报表)形式上传zip文件的方式比较特殊,文中的boundary 值最好是随机生成的,我只是举例。
5、随机生成规定长度
下面附一个随机生成字符串的例子:
QString setRandStr(int strLen)
{
QString tmp = QString("0123456789ABCDEFGHIJKLMNOPQRSTUVWZYZ");
QString str = QString();
QTime t = QTime::currentTime();
qsrand(t.msec()+t.second()*1000);
for(int i=0 ; i<strLen; i++) {
int ir = qrand()%tmp.length();
str[i] = tmp.at(ir);
}
return str;
}