原创

Qt如何上传zip文件(两种形式),及随机生成规定长度的字符串的小方法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://shazhenyu.blog.csdn.net/article/details/78193440

以zip文件为例:

先看一下上传时抓的包:



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";
    });
}

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";
    });
}


注意第二种以表单(报表)形式上传zip文件的方式比较特殊,文中的boundary 值最好是随机生成的,我只是随便举了一个小例子。下面附一个随机生成字符串的例子:

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;
}


文章最后发布于: 2017-10-10 15:44:50
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 代码科技 设计师: Amelia_0503

分享到微信朋友圈

×

扫一扫,手机浏览