zoukankan      html  css  js  c++  java
  • qt http 下载

    #include <QCoreApplication>
    #include <QFile>
    #include <QFileInfo>
    #include <QList>
    #include <QNetworkAccessManager>
    #include <QNetworkRequest>
    #include <QNetworkReply>
    #include <QStringList>
    #include <QTimer>
    #include <QUrl>

    #include <stdio.h>

    class DownloadManager: public QObject
    {
        Q_OBJECT
        QNetworkAccessManager manager;
        QList<QNetworkReply *> 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);
    };

    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\n",
                    qPrintable(filename),
                    qPrintable(file.errorString()));
            return false;
        }

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

        return true;
    }

    void DownloadManager::execute()
    {
        QStringList args = QCoreApplication::instance()->arguments();
        args.takeFirst();           // skip the first argument, which is the program's name
        if (args.isEmpty()) {
            printf("Qt Download example - downloads all URLs in parallel\n"
                   "Usage: download url1 [url2... urlN]\n"
                   "\n"
                   "Downloads the URLs passed in the command-line to the local directory\n"
                   "If the target file already exists, a .0, .1, .2, etc. is appended to\n"
                   "differentiate.\n");
            QCoreApplication::instance()->quit();
            return;
        }

        foreach (QString arg, args) {
            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\n",
                    url.toEncoded().constData(),
                    qPrintable(reply->errorString()));
        else {
            QString filename = saveFileName(url);
            if (saveToDisk(filename, reply))
                printf("Download of %s succeded (saved to %s)\n",
                       url.toEncoded().constData(), qPrintable(filename));
        }

        currentDownloads.removeAll(reply);
        reply->deleteLater();

        if (currentDownloads.isEmpty())
            // all downloads finished
            QCoreApplication::instance()->quit();
    }

    int main(int argc, char **argv)
    {
        QCoreApplication app(argc, argv);

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

        app.exec();
    }

    #include "main.moc"

  • 相关阅读:
    Dynamics 365 多租户?多实例?
    接口接收gzip压缩数据并解压
    系统检测到在一个调用中尝试使用指针参数时的无效指针地址 问题
    PBI DAX 中GroupBy
    将sql 查询结果导出到excel
    自动生成数据库表分区脚本
    快速生成导入亿级测试数据到sqlserver
    powershell 版本问题
    运行powershell 脚本 在此系统上禁止运行脚本
    python网站收集
  • 原文地址:https://www.cnblogs.com/zzxap/p/2175731.html
Copyright © 2011-2022 走看看