QT FTP的方式访问其他电脑文件
FTP配置:https://www.jb51.net/article/111054.htm
downLoadFile("ftp://"+ IP +"//db//program.db", "C:/chenhe/db/program.db");
void WeldTool::downLoadFile(QString url, QString strSavePath) {
QFile* tempFile;
QTimer timer;
bool loadFlag = false;
QNetworkRequest request;
QNetworkReply* reply = nullptr;
request.setUrl(QUrl(url));
QNetworkAccessManager* g_manager = new QNetworkAccessManager();
reply = g_manager->get(request);
QEventLoop loop; // 开启循环等待
QObject::connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit); // 超时则退出循环
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit); // 请求成功则退出循环
QObject::connect(reply, &QNetworkReply::finished, [&]() {
if (reply->error() != QNetworkReply::NoError) {
// 错误处理
outPutMsg(QtDebugMsg, "WeldTool::downLoadFile Error String = " + reply->errorString());
outPutMsg(QtDebugMsg, "WeldTool::downLoadFile url = " + url);
return;
}
});
QObject::connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(slotDownLoadSpeed(qint64, qint64))); // 请求成功则退出循环
QObject::connect(reply, &QNetworkReply::readyRead, [&]() {
QByteArray bytes = reply->readAll();
tempFile->write(bytes);
tempFile->flush();
loadFlag = true;
});
if (!strSavePath.isEmpty() && loadFlag) {
tempFile = new QFile(strSavePath);
// [1] 打开下载文件,如果打开文件出错,不进行下载
if (!tempFile->open(QIODevice::WriteOnly | QIODevice::Truncate)) {
tempFile->close();
tempFile->deleteLater();
outPutMsg(QtDebugMsg, "WeldTool::downLoadFile [错误] 打开文件出错: = " + strSavePath);
return;
}
}
loop.exec(); // 启动事件循环
if(loadFlag){
tempFile->close();
tempFile->deleteLater();
}
}