我已有用于固件更新的C++/Qt
源代码。 HTTP客户端用于下载文件。
如果我提供http://<path to file>
,则这段代码可以正常工作
但是 http 客户端无法从ftp://<path to down load>
下载。 ftp显示下载的文件大小为零。
按照StackOverflow,它对两个都适用。我尝试使用setScheme("ftp")
和其他小的更改。但是不起作用。
void HttpClient::onDownloadProgress(qint64 bytesReceived,
qint64 bytesTotal)
{
emit downloadUpdateProgress(
static_cast<int>((bytesReceived * 1.0f / bytesTotal * 1.0f) * 100));
}
void HttpClient::onReadyRead()
{
auto reply = qobject_cast<QNetworkReply *>(QObject::sender());
if (reply) {
QNetworkRequest request(reply->request());
auto device = qobject_cast<QIODevice *>(request.originatingObject());
if (device) {
device->write(reply->readAll());
}
}
}
bool HttpClient::post(const QUrl &url, const QByteArray &data,
QIODevice *device)
{
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/x-www-form-urlencoded");
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute,
QNetworkRequest::AlwaysNetwork);
request.setSslConfiguration(getSslConfig());
request.setOriginatingObject(device);
QNetworkReply *reply = _networkAccessManager->post(request, data);
connect(reply, &QNetworkReply::readyRead, this, &HttpClientImpl::onReadyRead);
bool result = waitForFinished(reply, 30 * 1000);
return result;
}
bool HttpClientImpl::get(const QUrl &url, QIODevice *device)
{
QNetworkRequest request(url);
request.setSslConfiguration(getSslConfig());
request.setOriginatingObject(device);
QNetworkReply *reply = _networkAccessManager->get(request);
connect(reply, &QNetworkReply::downloadProgress, this,
&HttpClientImpl::onDownloadProgress);
connect(reply, &QNetworkReply::readyRead, this, &HttpClientImpl::onReadyRead);
bool result = waitForFinished(reply);
return result;
}
它应该下载正确的文件大小。