• 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"

  • 相关阅读:
    S5pv210 android VGA 1440*900 视频播放/3D 演示效果实拍视频
    毕业3年,工资从5k到20k的经历——真的还是假的啊?
    转载.WinCE6.0 Camera驱动整体结构
    半夜来认识一下S5PV210 的VBPDE和VFPDE
    微软的windows 8授权真的要这么贵?谁要?!
    基于S5PC100的FIMC的部分解释——一篇让我理解透彻2440和S5PV210 摄像头camera控制器的文章
    转.Buffer Management by the Camera Driver (Windows Embedded CE 6.0)
    微软?想干掉苹果,自己做平板,还想做手机,有优秀的合作伙伴诺基亚?简直是笑话,难怪鲍ceo 被评为最应该下台的CEO了
    魅族经过M8——》M9——》MX 已经走向国际?
    wince6.0 S5pv210 之Sate210 VGA 镜像1440*900@60HZ/1280*1024@70HZ分辨率测试镜像(南嵌电子科技作品)
  • 原文地址:https://www.cnblogs.com/zzxap/p/2175731.html
Copyright © 2020-2023  润新知