QT中通过HTTP URL获取并保存网页上的图片

通过HTTP来获得网页上的图片,在MFC中主要是通过类CInternetSession类完成,在QT中则是通过三个类来完成,QNetworkAccessManager,QNetworkRequest,QNetworkReply,三个类的详细用法还需自己多看文档,这里就不多解释了,下面直接贴代码,采用的是两种方式来实现的,一个类和一个函数,别忘了在Qt工程中加入 QT += core network.

#ifndef DOWNLOADER_H
#define DOWNLOADER_H

#include <QFile>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QObject>
#include <QUrl>

class Downloader : public QObject {
    Q_OBJECT
    QFile *m_file;
    bool m_isReady;

public:
    explicit Downloader(QObject *parent = 0) : QObject(parent) {m_isReady = true;}
    virtual ~Downloader() {}

    void downloadFileFromURL(const QString &url, const QString &filePath);

private slots:
    void onDownloadFileComplete(QNetworkReply *reply);
};

QNetworkReply::NetworkError downloadURL(const QString &url, const QString &fileName);

#endif // DOWNLOADER_H


#include "downloader.h"
#include <QDebug>
#include <QEventLoop>

void Downloader::downloadFileFromURL(const QString &url, const QString &filePath)
{
    if (!m_isReady)
        return;
    m_isReady = false;

    const QString fileName = filePath + url.right(url.size() - url.lastIndexOf("/"));
    qDebug() << fileName;
    m_file = new QFile();
    m_file->setFileName(fileName);
    m_file->open(QIODevice::WriteOnly);
    if (!m_file->isOpen()) {
        m_isReady = true;
        return;
    }

    QNetworkAccessManager *manager = new QNetworkAccessManager;

    QNetworkRequest request;
    request.setUrl(QUrl(url));

    connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(onDownloadFileComplete(QNetworkReply *)));

    manager->get(request);
}

void Downloader::onDownloadFileComplete(QNetworkReply *reply)
{
    if (!m_file->isWritable()) {
        m_isReady = true;
        return;
    }

    m_file->write(reply->readAll());
    m_file->close();
    m_isReady = true;
}

bool downloadURL(const QString &url, const QString &fileName)
{
    QNetworkAccessManager manager;
    QNetworkRequest request;
    request.setUrl(url);
    QNetworkReply *reply = manager.get(request);

    QEventLoop loop;
    QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
    loop.exec();

    if (reply->error() != QNetworkReply::NoError)
    {
        return false;
    }

    QFile f(fileName);
    qDebug() << fileName;
    if(!f.open(QIODevice::WriteOnly))
        return false;
    f.write(reply->readAll());
    f.close();
    delete reply;
    return true;
 }

下面是测试代码:

#include <QCoreApplication>
#include "downloader.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

//    Downloader downloader;
//    downloader.downloadFileFromURL("https://img3.doubanio.com/view/photo/photo/public/p449668815.jpg",
//                                   "G:/Documents/picture");

    downloadURL("https://img3.doubanio.com/view/photo/photo/public/p449668815.jpg",
                                                   "G:/Documents/picture/p449668815.<span style="font-family: Arial, Helvetica, sans-serif;">jpg");</span>

    return a.exec();
}

上面的路径G:/Documents/picture/你得先建立这个路径,如果你的需求是路径不存在则先新建,那么需要在程序中加几句代码来实现,也是比较简单的。

你可能感兴趣的:(http,图片,url)