码迷,mamicode.com
首页 > 数据库 > 详细

Qt使用QNetworkAccessManager获取FTP文件函数

时间:2020-05-09 18:47:53      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:locate   pre   col   文件   finish   path   ring   nload   loaded   

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

 

Qt使用QNetworkAccessManager获取FTP文件函数

标签:locate   pre   col   文件   finish   path   ring   nload   loaded   

原文地址:https://www.cnblogs.com/judes/p/12859318.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!