Download files using QFTP or qhttp

Source: Internet
Author: User
Tags emit ftp client
Download files using qftp or qhttp 2010-03-18 16:11

Download files using QFTP or qhttp
Http://hi.baidu.com/ys%5Fshuoshu

The project needs to bulk download files, the beginning of the FTP, and then consider the client installed FTP server will be more trouble, just as there are Web services must be built, rewritten the program to download the HTTP way. Just did not summarize the FTP download program, here together to fill up.
The qftp and qhttp provided in Qt are inherited from Qnetworkprotocol, and they are used in a similar way, only slightly different in detail.

Some basic code:
1. There is nothing special about declaring objects.
Qhttp *m_http = new Qhttp (this);
Qftp *m_ftp = new Qftp (this);
2. Establish a connection, completely different naming method, let a person confused.
M_http->sethost (Host,port);
M_ftp->connecttohost (Host,port);
3. Start downloading files, the return value is the download number, in fact, this is not synchronized, just to explain those files to download, basically downloaded before this part of the end.
int id = m_http->get (path_source);
int id = m_ftp->get (path_source);
4. Close the connection
M_http->closeconnection ();
M_ftp->close ();

In fact, this is not the case, but also to deal with QFTP and qhttp signal:
1. Get Connection Status:
statechanged (int state)
Qftp and qhttp states are not exactly the same, you can refer to the API.
2. Process the download file:
Qftp is Readyread ()
Qhttp is readyread (const Qhttpresponseheader & RESP)
This is the point, where you have to get the download number through the CurrentID () method, and judge the file being downloaded, using the WriteBlock (char*, ulong) method to get the contents of the file.
3. The end of the download processing, where return true to indicate an error, return false indicates that the download was successful.
QFTP is Ftpdone (bool error)
Qhttp is Httpdone (bool error)
Remember to return true here to determine if the return value of state () equals qftp::connected or qhttp::connected, and then close the connection.
4. Some other signals
Qhttp's datasendprogress (int, total) is emitted when the message is sent, and the parameter is the amount of bytes that has been sent and the amount of bytes emitted.
The datareadprogress (int, total), Qhttp, is issued when receiving information, and the parameter is the bytes amount of the received information and the gross acceptance bytes amount.
Qhttp requeststarted (int id), starting when downloading a file, the parameter is the number of the download file
Qhttp requestfinished (int ID, bool error), download a file at the end of the issue, the parameter is the number of downloaded files, and the success of the identity (true indicates error, false indicates success)

Add: The QT version used here is 3.3.8, OS Linux, kernel 2.6,gcc version is 4.3.2.

Here are two encapsulated classes that enable you to download and download progress in bulk.

================================cftpclient.h=====================================
#ifndef Cftpclient_h
#define Cftpclient_h

#include <qobject.h>
#include <qftp.h>
#include <qprogressdialog.h>
#include <qapplication.h>
#include <qfile.h>

Class Cftpclient:public Qwidget
{
Q_object
Public
Cftpclient ();
~cftpclient ();
void Download (QString host, q_uint16 Port, Qstringlist Path_source, qstringlist path_target, QString user, QString passwo RD);

Private Slots:
void Ftpdone (bool error);
void Cancel ();
void statechanged (int state);
void Readyread ();

Private
Qftp *m_ftp;
Qprogressdialog *M_QPD;
int m_total;
int m_step;
qstringlist* M_path_target;
Qmap<int,int> M_QM;

Signals:
void success ();
void fail ();
};
#endif

================================cftpclient.cpp=====================================
/************************************************************
**NAME:FTP Client Class
**note:
**create date:2010-01-25
**creater:yushuo
**edit date:2010-01-25
************************************************************/
#include "Cftpclient.h"

Cftpclient::cftpclient ()
{
   //Create the socket and connect various of its signals
     m_step = 0;
    m_total = 0;
    m_ftp = new Qftp (this);
    m_qpd = new Qprogressdialog (Qobject::trutf8 ("File download, please!... later"), Qobject::trutf8 ("Cancel"), M_total, This,qobject::trutf8 ("hint"), False,wstyle_customize | Wstyle_noborder);
    M_qpd->setpalettebackgroundcolor (Qcolor (222,199,241));
   
    Connect (m_ftp, SIGNAL (Done (BOOL)), this, SLOT (Ftpdone (bool));
    Connect (m_ftp, SIGNAL (statechanged (int)), this, SLOT (statechanged (int)));
    Connect (m_ftp, SIGNAL (Readyread ()), this, SLOT (Readyread ()));
    Connect (M_QPD, SIGNAL (Canceled ()), this, SLOT (Cancel ()));
}

Cftpclient::~cftpclient ()
{
Delete m_ftp;
Delete m_qpd;
}

void Cftpclient::d ownload (QString host, q_uint16 Port, Qstringlist Path_source, qstringlist path_target, QString user, QS Tring password)
{
int id;
M_total = Path_source.count ();
if (m_total>0) {
M_qpd->settotalsteps (m_total);
M_path_target = &path_target;
if (M_ftp->connecttohost (Host,port)) {
if ((User.isnull ()) | | (User.isempty ()) | | (Password.isnull ()) | | (Password.isempty ())) {
if (M_ftp->login ("Tomcat", "Tomcat")) {
for (int i=0;i<m_total;i++) {
id = m_ftp->get (*path_source.at (i));
M_qm.insert (Id,i);
}
}
}else {
if (M_ftp->login (User,password)) {
for (int i=0;i<m_total;i++) {
id = m_ftp->get (*path_source.at (i));
M_qm.insert (Id,i);
}
}
}
}
M_qpd->exec ();
}
}

void Cftpclient::ftpdone (bool error)
{
if (Error) {
Qdebug ("Cftpclient::ftpdone===true");
M_step=m_total;
Emit fail ();
if (m_ftp->state () = = qftp::connected) {
M_ftp->close ();
}
M_qpd->close ();
}else {
Qdebug ("Cftpclient::ftpdone===false");
M_qpd->close ();
Emit success ();
}
}

void Cftpclient::cancel ()
{
M_ftp->abort ();
M_step=m_total;
}

void cftpclient::statechanged (int state)
{
Switch (state)
{
Case qftp::unconnected:
Qdebug ("ftp:unconnected ...");
Break
Case Qftp::hostlookup:
Qdebug ("Ftp:hostlookup ...");
Break
Case qftp::connecting:
Qdebug ("ftp:connecting ...");
Break
Case qftp::connected:
Qdebug ("ftp:connected ...");
Break
Case Qftp::loggedin:
Qdebug ("Ftp:loggedin ...");
Break
Case qftp::closing:
Qdebug ("ftp:closing ...");
Break
}
}

void Cftpclient:: Readyread ()
{
int sort = 0;
int id = M_ftp->currentid ();
Qmap<int,int>::iterator Qmit;
if (M_qm.contains (ID)) {
Qmit = M_qm.find (ID);
sort = Qmit.data ();
}
Qdebug ("readyread...%d ..." +*m_path_target->at (sort), id);
ULONG len = 0;
Char data[1024];
Qfile *file = new Qfile (*m_path_target->at (sort));
M_step = sort;
M_qpd->setprogress (M_step);
if (!file->open (io_writeonly | Io_append)) {
Return
}

while (len = m_ftp->readblock (data, sizeof (data)) >0) {
File->writeblock (data, Len);
}
/*
Qbytearray QbA = M_ftp->readall ();
File->writeblock (QBA);
*/

File->close ();
}

================================chttp.h=====================================
#ifndef Chttp_h
#define Chttp_h

#include <qobject.h>
#include <qhttp.h>
#include <qprogressdialog.h>
#include <qapplication.h>
#include <qfile.h>

Class Chttp:public Qwidget
{
Q_object
Public
Chttp ();
~chttp ();
void Download (QString host, q_uint16 Port, Qstringlist Path_source, qstringlist path_target);

Private Slots:
void Httpdone (bool error);
void Cancel ();
void statechanged (int state);
void Readyread (const qhttpresponseheader & RESP);
void datasendprogress (int total);
void datareadprogress (int total);
void requeststarted (int id);
void requestfinished (int ID, bool error);
void responseheaderreceived (const qhttpresponseheader & RESP);

Private
Qhttp *m_http;
Qprogressdialog *M_QPD;
int m_total;
int m_step;
qstringlist* M_path_target;
Qmap<int,int> M_QM;

Signals:
void success ();
void fail ();
};
#endif

================================chttp.cpp=====================================
/************************************************************
**name:http Download Class
**note:
**create DATE:2010-03-17
**creater:yushuo
**edit DATE:2010-03-17
************************************************************/
#include "Chttp.h"

Chttp::chttp ()
{
Create the socket and connect various of its signals
M_step = 0;
m_total = 0;
M_http = new Qhttp (this);
M_QPD = new Qprogressdialog (Qobject::trutf8 ("File download, please!... later"), Qobject::trutf8 ("Cancel"), M_total,this,qobject::trutf8 (" Hint "), False,wstyle_customize | Wstyle_noborder);
M_qpd->setpalettebackgroundcolor (Qcolor (222,199,241));

Connect (m_http, SIGNAL (Done (BOOL)), this, SLOT (Httpdone (bool));
Connect (M_QPD, SIGNAL (Canceled ()), this, SLOT (Cancel ());
Connect (m_http, SIGNAL (statechanged (int)), this, SLOT (statechanged (int)));
Connect (m_http, SIGNAL (readyread (const qhttpresponseheader&)), this, SLOT (const readyread &)));
Connect (m_http, SIGNAL (datasendprogress (int, int)), this, SLOT (datasendprogress (int, int)));
Connect (m_http, SIGNAL (datareadprogress (int, int)), this, SLOT (datareadprogress (int, int)));
Connect (m_http, SIGNAL (requeststarted (int)), this, SLOT (requeststarted (int)));
Connect (m_http, SIGNAL (requestfinished (int, bool)), this, SLOT (requestfinished (int, bool));
Connect (m_http, SIGNAL (responseheaderreceived (const qhttpresponseheader&)), this, SLOT (responseheaderreceived ( Const qhttpresponseheader&)));
}

Chttp::~chttp ()
{
Delete m_http;
Delete m_qpd;
}

void Chttp::d ownload (QString host, q_uint16 Port, Qstringlist Path_source, Qstringlist path_target)
{
int id;
M_total = Path_source.count ();
if (m_total>0) {
M_qpd->settotalsteps (m_total);
M_path_target = &path_target;
M_http->sethost (Host,port);
for (int i=0;i<m_total;i++) {
id = m_http->get (*path_source.at (i));
M_qm.insert (Id,i);
}
M_http->closeconnection ();
M_qpd->exec ();
}
}

void Chttp::httpdone (bool error)
{
if (Error) {
Qdebug ("Chttp::httpdone===true");
M_step=m_total;
Emit fail ();
if (m_http->state () = = qhttp::connected) {
M_http->closeconnection ();
}
M_qpd->close ();
}else {
Qdebug ("Chttp::httpdone===false");
M_step=m_total;
M_qpd->close ();
Emit success ();
}
}

void Chttp::cancel ()
{
M_http->abort ();
M_step=m_total;
}

void chttp::statechanged (int state)
{
Switch (state)
{
Case qhttp::unconnected:
Qdebug ("qhttp:unconnected ...");
Break
Case Qhttp::hostlookup:
Qdebug ("Qhttp:hostlookup ...");
Break
Case qhttp::connecting:
Qdebug ("qhttp:connecting ...");
Break
Case qhttp::sending:
Qdebug ("qhttp:sending ...");
Break
Case qhttp::reading:
Qdebug ("qhttp:reading ...");
Break
Case qhttp::connected:
Qdebug ("qhttp:connected ...");
Break
Case qhttp::closing:
Qdebug ("qhttp:closing ...");
Break
}
}

void Chttp:: Readyread (const Qhttpresponseheader & RESP)
{
int sort = 0;
int id = M_http->currentid ();
Qmap<int,int>::iterator Qmit;
if (M_qm.contains (ID)) {
Qmit = M_qm.find (ID);
sort = Qmit.data ();
}
Qdebug ("readyread...%d ..." +*m_path_target->at (sort), id);
ULONG len = 0;
Char data[1024];
Qfile *file = new Qfile (*m_path_target->at (sort));
M_step = sort;
M_qpd->setprogress (M_step);
if (!file->open (io_writeonly | Io_append)) {
Return
}

while (len = m_http->readblock (data, sizeof (data)) >0) {
File->writeblock (data, Len);
}
/*
Qbytearray QbA = M_ftp->readall ();
File->writeblock (QBA);
*/

File->close ();
}

void Chttp::d atasendprogress (int done, total int)
{
Qdebug ("sended%d bytes out of%d", done,total);
}

void Chttp::d atareadprogress (int done, total int)
{
Qdebug ("downloaded%d bytes out of%d", done,total);
}

void chttp::requeststarted (int id)
{
Qdebug ("chttp::requeststarted==%d", id);
}

void chttp::requestfinished (int ID, bool error)
{
if (Error) {
Qdebug ("chttp::requestfinished==%d==true", id);
Qdebug (M_http->errorstring ());
}else {
Qdebug ("Chttp::requestfinished==%d==false", id);
}
}

void chttp::responseheaderreceived (const Qhttpresponseheader & RESP)
{
Qdebug ("chttp::responseheaderreceived");
}

Turn from: http://hi.baidu.com/ys_shuoshu/blog/item/b85ac23709048a87a71e1214.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.