//downloadObject.h #ifndef HTTPGET_H #define HTTPGET_H #include #include class QUrl; class downloadObject : public QObject { Q_OBJECT public: QHttp * http; downloadObject(QObject *parent = 0); bool getFile(const QUrl &url,const qint64 totalSize); void abort(); signals: //void explains(); void downloadDone(); void updateProgressbar(int current, int all); private slots: void httpDone(bool error); void updateProgress(int current,int all); private: QFile file; QHttpResponseHeader *resp; QString localFileName; qint64 beforeSize; qint64 totalSize; }; #endif //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //downloadObject.cpp #include #include #include #include "stdio.h" #include "stdlib.h" #include "time.h" #include "downloadObject.h" downloadObject::downloadObject(QObject *parent) : QObject(parent) { http = new QHttp(this); connect(http, SIGNAL(done(bool)), this, SLOT(httpDone(bool))); connect(http, SIGNAL(dataReadProgress(int,int)),this, SLOT(updateProgress(int,int))); } bool downloadObject::getFile(const QUrl &url,const qint64 totalSizet) { if (!url.isValid()) { std::cerr << "Error: Invalid URL" << std::endl; return false; } if (url.scheme() != "http") { std::cerr << "Error: URL must start with 'http:'" << std::endl; return false; } if (url.path().isEmpty()) { std::cerr << "Error: URL has no path" << std::endl; return false; } localFileName = QFileInfo(url.path()).fileName(); if (localFileName.isEmpty()) localFileName = "httpget.out"; file.setFileName(localFileName); qint64 dwSize = 0; totalSize = totalSizet; if(file.exists()) { dwSize = file.size(); beforeSize = dwSize; } if (!file.open(QIODevice::Append)) { std::cerr << "Error: Cannot write file " << qPrintable(file.fileName()) << ": " << qPrintable(file.errorString()) << std::endl; return false; } QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp; http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port()); QHttpRequestHeader *header = new QHttpRequestHeader("GET", url.path()); header->setValue("Connection", "Keep-Alive"); QString str = "bytes="; QString strTemp = QString("%1").arg(dwSize); str+=strTemp; if (dwSize>totalSize) { dwSize = totalSize; } str+="-"; strTemp = QString("%1").arg(totalSize); str += strTemp; header->setValue("Range", str); strTemp = url.host()+":80"; header->setValue("Host", strTemp); // http->request(*header, 0, &file); http->close(); return true; } void downloadObject::abort() { http->abort(); file.close(); } void downloadObject::httpDone(bool error) { if (error) { QString sre = http->errorString(); std::cerr << "Error: " << qPrintable(http->errorString()) << std::endl; } else { std::cerr << "File downloaded as " << qPrintable(file.fileName()) << std::endl; } file.close(); emit downloadDone(); } void downloadObject::updateProgress(int current, int all) { if(current<0) { current = 0; } if(beforeSize<0) { beforeSize = 0; } current = beforeSize+current; emit updateProgressbar(current, totalSize); } //////////////////////////////////////////////////////////////////////////////////////////////////////// //qtdownloaddemo.h #ifndef QTDOWNLOADDEMO_H #define QTDOWNLOADDEMO_H #include #include "ui_qtdownloaddemo.h" #include "downloadObject.h" class QtDownloadDemo : public QDialog { Q_OBJECT public: QtDownloadDemo(QWidget *parent = 0, Qt::WFlags flags = 0); ~QtDownloadDemo(); private slots: void Download(); void Done(); void Stop(); void updateDataReadProgress(int bytesRead,int totalbytes); private: Ui::QtDownloadDemoClass ui; downloadObject *downloadobject; qint64 totalSize; }; #endif // QTDOWNLOADDEMO_H //////////////////////////////////////////////////////////////////////////////////////////////////// //qtdownloaddemo.cpp #include "qtdownloaddemo.h" #include #include QtDownloadDemo::QtDownloadDemo(QWidget *parent, Qt::WFlags flags) : QDialog(parent, flags) { ui.setupUi(this); downloadobject = new downloadObject(this); connect(downloadobject, SIGNAL(downloadDone()), this, SLOT(Done())); connect(downloadobject, SIGNAL(updateProgressbar(int,int)), this, SLOT(updateDataReadProgress(int,int))); } QtDownloadDemo::~QtDownloadDemo() { } void QtDownloadDemo::Stop() { downloadobject->abort(); } void QtDownloadDemo::Download() { bool ok; QString strUrl = ui.lineEdit->text(); totalSize = ui.lineEdit_2->text().toLongLong(&ok, 10); QUrl url(strUrl); downloadobject->getFile(url,totalSize); } void QtDownloadDemo::Done() { QString str = "download complete"; QMessageBox::warning(this,"Message",str); } void QtDownloadDemo::updateDataReadProgress(int bytesRead,int totalbytes) { ui.progressBar->setMaximum(totalSize); ui.progressBar->setValue(bytesRead); }