三天来,我一直试图想出一个可行的代码,简单地从Url下载文件,但到目前为止,我遇到了不少问题,现在我看不到错误。
首先,我的代码:
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
w.downloadArchive(QString("https://bitbucket.org/BattleClinic/evemon/downloads/EVEMon-binaries-1.8.1.4016.zip"), QCoreApplication::applicationDirPath(), QString("evemon.zip"));
return a.exec();
}
主窗口.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFile>
#include <QFileInfo>
#include <QNetworkAccessManager>
#include <QNetworkReply>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
bool downloadArchive(QString archiveUrl, QString saveToPath, QString archiveName);
private slots:
void downloadReadyRead();
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
void downloadFinished();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
主窗口.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
QNetworkAccessManager manager;
QFile *file;
QNetworkReply *reply;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->downloadLabel->hide();
ui->downloadProgressBar->hide();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::downloadReadyRead()
{
if(file)
{
file->write(reply->readAll());
}
}
void MainWindow::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
ui->downloadProgressBar->setMaximum(bytesTotal);
ui->downloadProgressBar->setValue(bytesReceived);
}
void MainWindow::downloadFinished()
{
downloadReadyRead();
file->flush();
file->close();
if(reply->error())
{
QMessageBox::information(this, "Download failed", tr("Failed: %1").arg(reply->errorString()));
}
reply->deleteLater();
reply = NULL;
delete file;
file = NULL;
ui->downloadLabel->hide();
ui->downloadProgressBar->hide();
}
bool MainWindow::downloadArchive(QString archiveUrl, QString saveToPath, QString archiveName)
{
QUrl url(archiveUrl);
if(archiveName.count() <= 0)
{
return false;
}
if(QFile::exists(saveToPath + "/" + archiveName))
{
QFile::remove(saveToPath + "/" + archiveName);
}
file = new QFile(saveToPath + "/" + archiveName);
if(!file->open(QIODevice::WriteOnly))
{
delete file;
file = NULL;
return false;
}
reply = manager.get(QNetworkRequest(url));
connect(reply, SIGNAL(finished()), this, SLOT(downloadFinished()));
connect(reply, SIGNAL(readyRead()), this, SLOT(downloadReadyRead()));
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
ui->downloadLabel->setText(QString("Downloading %1...").arg(archiveName));
ui->downloadLabel->show();
ui->downloadProgressBar->show();
return true;
}
Downloader.pro
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Downloader
TEMPLATE = app
SOURCES += main.cpp
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
在mainwindow.cpp中,我正在检查错误并将其打印到MessageBox中,以防出现错误。回复中当前代码的第一个错误是"无法初始化SSL上下文",因为我正在从https站点请求数据。我在谷歌上搜索了一段时间,发现解决方案是两个名为"libeay32.dll"one_answers"ssley32.dll"的文件,我必须复制到可执行文件的目录中。
现在回复中没有更多错误(至少MessageBox没有显示)。。。。然而,在执行downloadFinished函数后,下载的文件大小为0kb,这让我认为,他们根本没有下载。
我跳过了"if(reply->error())"语句,并显示了MessageBox。。。。我得到一个:"未知错误"。"未知错误"是否应将"if(reply->Error())"语句设置为true?
权限也没有问题。。。。我以管理员的身份尝试过。。。。因此创建文件本身没有问题。有人能帮我把代码写出来吗?我错过了任何dll或#include吗?
感谢
使用SSL下载:
connect(&http, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedHttp(QNetworkReply*)));
QNetworkRequest *req = new QNetworkRequest();
req->setUrl( QUrl("https://...") );
QSslConfiguration configSsl = QSslConfiguration::defaultConfiguration();
configSsl.setProtocol(QSsl::AnyProtocol);
req->setSslConfiguration(configSsl);
http.get(*req);
您需要:libeay32.dll、libssl32.dll、ssley32.dll(来自OpenSSL)和Visual c++2008 SP1 redist包。
确保它适用于非ssl文件。然后,要添加ssl支持,请尝试按照以下问题答案中的说明进行操作:
Qt SSL支持缺少
也可以尝试在代码中使用qDebug
,而不是大部分时间使用QMessageBox。它使添加调试行变得更容易,而且不会中断代码或用QMessageBoxes填充屏幕。