c语言从url下载文件,c-如何在Qt中从互联网下载文件?

我不知道问题出在哪里…编译器(Qt)运行程序时没有错误,但文件未下载…

您能告诉我什么地方出问题了吗?

我通过示例“下载”进行了下载,该文件位于qt文件夹中.唯一的区别是它们有一个控制台,而我有Windows应用程序.

主窗口

#ifndef MAINWINDOW_H

#define MAINWINDOW_H

#include

#include

namespace Ui {

class MainWindow;

}

class MainWindow : public QMainWindow

{

Q_OBJECT

public:

explicit MainWindow(QWidget *parent = 0);

~MainWindow();

private slots:

void on_pushButton_clicked();

private:

Ui::MainWindow *ui;

};

class DownloadManager: public QObject

{

Q_OBJECT

QNetworkAccessManager manager;

QList currentDownloads;

public:

DownloadManager();

void doDownload(const QUrl &url);

QString saveFileName(const QUrl &url);

bool saveToDisk(const QString &filename, QIODevice *data);

public slots:

void execute();

void downloadFinished(QNetworkReply *reply);

};

#endif // MAINWINDOW_H

main.cpp

#include

#include "mainwindow.h"

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

int main(int argc, char *argv[])

{

QApplication a(argc, argv);

MainWindow w;

#if defined(Q_WS_S60)

w.showMaximized();

#else

w.show();

#endif

return a.exec();

}

DownloadManager::DownloadManager()

{

connect(&manager, SIGNAL(finished(QNetworkReply*)),

SLOT(downloadFinished(QNetworkReply*)));

}

void DownloadManager::doDownload(const QUrl &url)

{

QNetworkRequest request(url);

QNetworkReply *reply = manager.get(request);

currentDownloads.append(reply);

}

QString DownloadManager::saveFileName(const QUrl &url)

{

QString path = url.path();

QString basename = QFileInfo(path).fileName();

if (basename.isEmpty())

basename = "download";

if (QFile::exists(basename)) {

// already exists, don't overwrite

int i = 0;

basename += '.';

while (QFile::exists(basename + QString::number(i)))

++i;

basename += QString::number(i);

}

return basename;

}

bool DownloadManager::saveToDisk(const QString &filename, QIODevice *data)

{

QFile file(filename);

if (!file.open(QIODevice::WriteOnly)) {

fprintf(stderr, "Could not open %s for writing: %s

",

qPrintable(filename),

qPrintable(file.errorString()));

return false;

}

file.write(data->readAll());

file.close();

return true;

}

void DownloadManager::execute()

{

QStringList args = QCoreApplication::instance()->arguments();

args[0]="http://www.google.ru/images/srpr/logo3w.png";

QString arg=args[0];

QUrl url = QUrl::fromEncoded(arg.toLocal8Bit());

doDownload(url);

}

void DownloadManager::downloadFinished(QNetworkReply *reply)

{

QUrl url = reply->url();

if (reply->error()) {

fprintf(stderr, "Download of %s failed: %s

",

url.toEncoded().constData(),

qPrintable(reply->errorString()));

} else {

QString filename = saveFileName(url);

if (saveToDisk(filename, reply))

printf("Download of %s succeded (saved to %s)

",

url.toEncoded().constData(), qPrintable(filename));

}

currentDownloads.removeAll(reply);

reply->deleteLater();

if (currentDownloads.isEmpty())

// all downloads finished

QCoreApplication::instance()->quit();

}

主窗口

#include "mainwindow.h"

#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :

QMainWindow(parent),

ui(new Ui::MainWindow)

{

ui->setupUi(this);

}

MainWindow::~MainWindow()

{

delete ui;

}

void MainWindow::on_pushButton_clicked()

{

DownloadManager manager;

manager.execute();

QTimer::singleShot(0, &manager, SLOT(execute()));

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值