三天以来,我试图提出一个可行的代码来简单地从 URL 下载文件,但到目前为止我遇到了很多问题,现在我处于看不到错误的地步。
首先,我的代码:
主文件
#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;
}
下载器.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”和“ssleay32.dll”的文件,我必须将其复制到可执行文件的目录中。
现在回复中不再有错误(至少 MessageBox 没有出现)....但是,在执行 downloadFinished 函数后,下载的文件大小为 0 kb,这让我觉得,他们不是根本没有发生下载。
我跳过了“if(reply->error())”语句并无论如何都显示了 MessageBox....我得到了一个:“未知错误”。“未知错误”是否应该不将“if(reply->error())”语句设置为 true?
权限也没有问题......我以管理员身份尝试过......所以创建文件本身没有问题。任何人都可以帮助我使该代码正常工作吗?我会错过任何 dll 或 #include 吗?
谢谢