183 lines
5.9 KiB
C++
183 lines
5.9 KiB
C++
#include "threadupgradeapk.h"
|
||
#include "QFileInfo"
|
||
#include <QHostAddress>
|
||
#include <QIODevice>
|
||
#include <QDir>
|
||
#include <QJsonArray>
|
||
#include <QDirIterator>
|
||
#include <QMessageBox>
|
||
#include <QCoreApplication>
|
||
#include <QUuid>
|
||
#include <QHostAddress>
|
||
#include <globaldefine.h>
|
||
#include <QHttpMultiPart>
|
||
ThreadUpgradeApk::ThreadUpgradeApk(QString strApkPathAndName,QString url,int iType)
|
||
{
|
||
m_strApkName=strApkPathAndName;
|
||
m_strUrl=url;
|
||
m_iType=iType;
|
||
}
|
||
ThreadUpgradeApk::~ThreadUpgradeApk()
|
||
{
|
||
|
||
}
|
||
void ThreadUpgradeApk::run()
|
||
{
|
||
// QCoreApplication::processEvents();
|
||
if(bSendCloseFlag==true)
|
||
bSendCloseFlag=false;
|
||
else {
|
||
return;
|
||
}
|
||
QFileInfo file1(m_strApkName);
|
||
QFile file(m_strApkName);
|
||
QMap<QString,QString> params_send; //上传的普通参数 在本程序中 需要上传一个普通参数为"username"
|
||
params_send.insert("username","10005");
|
||
//emit sigSetLableText(tr("UpLoadForm"));
|
||
UpLoadForm(m_strUrl,params_send,file1.fileName(),&file,file1.fileName(),m_iType);
|
||
|
||
}
|
||
|
||
|
||
//void ThreadUpgradeApk::UpLoadForm(QString strUrl,QString strFilePathName)
|
||
//{
|
||
//QString xlname = "text.xlsx";
|
||
// QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
|
||
// QHttpPart imagePart;
|
||
// imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/xlsx"));
|
||
// imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\""+ xlname + "\"")); // file为后端定义的key,filename即为excel文件名
|
||
|
||
// QFile *file = new QFile(strFilePathName);//pathbuf1 为文件路径
|
||
// file->open(QIODevice::ReadOnly);
|
||
// imagePart.setBodyDevice(file);
|
||
// file->setParent(multiPart);
|
||
// multiPart->append(imagePart);
|
||
|
||
// QUrl url(strUrl);
|
||
// QNetworkRequest netReq;
|
||
// netReq.setUrl(url);
|
||
// QNetworkAccessManager *_uploadManager=new QNetworkAccessManager();
|
||
|
||
// connect(_uploadManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(send_xlsdata(QNetworkReply*)));
|
||
// _uploadManager->post(netReq,multiPart);
|
||
//}
|
||
void ThreadUpgradeApk::UpLoadForm(QString Path,QMap<QString,QString> params,QString fileFormName,QFile *uploadFile,QString newFileName,int iType)
|
||
{
|
||
QString BOUNDARY=QUuid::createUuid().toString();
|
||
QByteArray sb=QByteArray();
|
||
//先上传普通的表单数据
|
||
// for(QMap<QString,QStringIteratort=params.begin();t!=params.end();t++)
|
||
for(QMap<QString,QString>::iterator t=params.begin();t!=params.end();t++)
|
||
{
|
||
sb.append("--"+BOUNDARY+"\r\n");
|
||
sb.append(QString("Content-Disposition: form-data;name=\"")+t.key()+QString("\"")+QString("\r\n"));
|
||
sb.append("\r\n");
|
||
sb.append(t.value()+"\r\n");
|
||
}
|
||
//上传文件的头部
|
||
sb.append("--"+BOUNDARY+"\r\n");
|
||
sb.append(QString("Content-Disposition: form-data;name=\"")+fileFormName+QString("\";filename=\"")+newFileName+QString("\"")+QString("\r\n"));
|
||
sb.append("\r\n");
|
||
//上传文件内容
|
||
if(!uploadFile->open(QIODevice::ReadOnly)){
|
||
return;
|
||
}
|
||
sb.append(uploadFile->readAll());
|
||
sb.append("\r\n");
|
||
sb.append("--"+BOUNDARY+"\r\n");
|
||
|
||
//编辑HTTP头部
|
||
QNetworkAccessManager *_uploadManager=new QNetworkAccessManager();
|
||
QNetworkRequest request=QNetworkRequest(QUrl(Path));
|
||
request.setRawHeader(QString("Content-Type").toLatin1(),QString("multipart/form-data;boundary="+BOUNDARY).toLatin1());
|
||
request.setRawHeader(QString("Content-Length").toLatin1(),QString::number(sb.length()).toLatin1());
|
||
//执行post请求
|
||
// if(m_PostingDlg==nullptr)
|
||
{
|
||
//m_PostingDlg = new LoEmptyDialog();
|
||
//connect(m_PostingDlg,SIGNAL(sigClose()),this,SLOT(DeletePostingDlg()));
|
||
//connect(m_pGetAskTimer,SIGNAL(timeout()),m_PostingDlg,SLOT(TimerOutUnlock()));
|
||
//m_PostingDlg->lock(tr("UpgradeSoftware"),tr("Success"),tr("UpgradeSoftware")+tr("failed"));
|
||
//m_pGetAskTimer->start(60000);
|
||
QNetworkReply * m_netReply =_uploadManager->post(request,sb);
|
||
connect(m_netReply, SIGNAL(uploadProgress(qint64,qint64)), this, SLOT(slotUploadProgress(qint64,qint64)));
|
||
// connect(this, SIGNAL(sigSetTipTextContent ( QString )), m_PostingDlg, SLOT( SetTipTextContent(QString)));
|
||
if(iType==0)//apk
|
||
{
|
||
connect(m_netReply, SIGNAL(finished()), this, SLOT(slotUploadFinished()));
|
||
|
||
}
|
||
else if(iType==1)//fpga.rpd
|
||
{
|
||
connect(m_netReply, SIGNAL(finished()), this, SLOT(slotUploadFpgaFinished()));
|
||
|
||
}
|
||
QEventLoop eventLoop;
|
||
QTimer timer;
|
||
|
||
connect(m_netReply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
|
||
connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
|
||
timer.start(600 * 1000);
|
||
eventLoop.exec(); // block until finish
|
||
|
||
if (timer.isActive()) {
|
||
timer.stop();
|
||
} else {
|
||
disconnect(m_netReply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
|
||
m_netReply->abort();
|
||
m_netReply->deleteLater();
|
||
return ;
|
||
}
|
||
//m_PostingDlg->exec();
|
||
}
|
||
|
||
|
||
}
|
||
void ThreadUpgradeApk::slotTimerOutUnlock()
|
||
{
|
||
emit sigTimerOut();
|
||
bSendCloseFlag=true;
|
||
}
|
||
void ThreadUpgradeApk::slotUploadProgress(qint64 A,qint64 B)
|
||
{
|
||
if(B!=0)
|
||
{
|
||
QString strbaifenbi=tr("Uploadding")+":"+QString("%1%").arg(A*100/B);
|
||
emit sigSetProgressValue(A*100/B);
|
||
emit sigSetLableText(strbaifenbi);
|
||
// m_pGetAskTimer->stop();
|
||
// m_pGetAskTimer->start(30000);
|
||
}
|
||
}
|
||
void ThreadUpgradeApk::slotUploadFinished()
|
||
{
|
||
emit sigFinished();
|
||
// MACRO_ASKTIME_STOP
|
||
// m_pGetAskTimer->start(40000);
|
||
|
||
qDebug()<<"slotUploadFinished";
|
||
|
||
}
|
||
void ThreadUpgradeApk::slotUploadFpgaFinished()
|
||
{
|
||
emit sigFpgaFinished();
|
||
// MACRO_ASKTIME_STOP
|
||
// m_pGetAskTimer->start(40000);
|
||
|
||
qDebug()<<"slotUploadFinished";
|
||
|
||
}
|
||
|
||
void ThreadUpgradeApk::OnUpgradeSuccess()
|
||
{
|
||
// MACRO_ASKTIME_STOP
|
||
bSendCloseFlag=true;
|
||
|
||
}
|
||
void ThreadUpgradeApk::OnUpgradeFail()
|
||
{
|
||
// MACRO_ASKTIME_STOP
|
||
bSendCloseFlag=true;
|
||
|
||
}
|