发布网友 发布时间:2022-04-11 08:18
共2个回答
懂视网 时间:2022-04-11 12:40
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
热心网友 时间:2022-04-11 09:48
用QFtp应该可以做到吧, 摘自Qt的帮助文档, QFtp::get()时将次二哥参数设置为0,之后收到 readyRead()后可以通过read()或readAll()读取到内存里。