void Mystack::getFtpImg(QString ftpFilePath, QString curFileName)
{
QNetworkAccessManager* myNetworkManager = new QNetworkAccessManager(this);
QNetworkRequest myNetworkRequest;
QNetworkReply *myNetworkReply;
QFile* downloadedFile = new QFile("./"+curFileName);
downloadedFile->open(QIODevice::WriteOnly);
QUrl url;
url.setScheme("ftp");
url.setUserName(config.getSet()->value("FTP_USER_NAME").toString());
url.setPassword(config.getSet()->value("FTP_PASS_WORD").toString());
url.setHost(config.getSet()->value("FTP_HOST").toString());
url.setPort(config.getSet()->value("FTP_PORT").toInt());
url.setPath(ftpFilePath);
Mymethod::record("try to download file by:"+url.toString(),PRINT_INFO);
myNetworkRequest.setUrl(url);
myNetworkReply = myNetworkManager->get(myNetworkRequest);
QObject::connect(myNetworkReply,&QNetworkReply::readyRead,[=]{
downloadedFile->write(myNetworkReply->readAll());
});
QObject::connect(myNetworkManager,&QNetworkAccessManager::finished,[=]{
if(NULL==myNetworkReply)
{
Mymethod::record(getCodeLocate()+"myNetworkReply is null.",PRINT_ERR);
return;
}
if(myNetworkReply->error() == QNetworkReply::NoError)
{
downloadedFile->close();
Mymethod::record("download file success:"+curFileName,PRINT_INFO);
}
else
{
Mymethod::record(getCodeLocate()+QString("download file:%1 failed,err:%2").arg(curFileName).arg(myNetworkReply->errorString()),PRINT_ERR);
downloadedFile->close();
}
myNetworkReply->deleteLater();
downloadedFile->deleteLater();
QObject::disconnect(myNetworkManager,0,0,0);
myNetworkManager->deleteLater();
});
}
来源:oschina
链接:https://my.oschina.net/u/4365833/blog/4273367