zoukankan      html  css  js  c++  java
  • Qt利用FTP下载文件

    自己封装的ftpCtrl类,测试正常下载文件。

    原文:默默地EEer
    http://www.cnblogs.com/hebaichuanyeah/p/6083036.html

    ftpCtrl.h

    #ifndef FTPCTRL_H
    #define FTPCTRL_H
    
    #include <QFile>
    #include <QFtp>
    
    
    class FtpCtrl : public QObject
    {
        Q_OBJECT
    
    public:
        FtpCtrl(QObject *parent = 0);
    
        bool getFile(QString path,QString name);
    
        void setHost(QString host,int port=21);
        int getDone();
    signals:
        void done();
    
    private slots:
        void ftpDone(bool error);
    
    
    private:
        QFtp ftp;
        QFile file;
        QString ftpHost;
        int ftpPort;
        int doneFlag;
    };
    
    #endif

    ftpCtrl.cpp

    #include <QtCore>
    #include <QtNetwork>
    #include <iostream>
    
    #include "ftpCtrl.h"
    #include <QDebug>
    
    FtpCtrl::FtpCtrl(QObject *parent)
        : QObject(parent)
    {
        connect(&ftp, SIGNAL(done(bool)), this, SLOT(ftpDone(bool)));
    }
    
    bool FtpCtrl::getFile(QString path,QString name)
    {
        QDir temp;
        if(!temp.exists(path))
        {
            temp.mkpath(path);
        }
        file.setFileName(path+"/"+name);
        if (!file.open(QIODevice::WriteOnly))
        {
            qDebug() << "Error: Cannot write file "<< qPrintable(file.fileName()) << ": "<< qPrintable(file.errorString());
            return false;
        }
        ftp.connectToHost(ftpHost, ftpPort);
        ftp.login();
        doneFlag = 0;
        ftp.get(path+"/"+name, &file);
        ftp.close();
        return true;
    }
    
    void FtpCtrl::setHost(QString host,int port)
    {
        ftpHost = host;
        ftpPort = port;
    }
    
    
    void FtpCtrl::ftpDone(bool error)
    {
        if (error)
        {
            doneFlag = -1;
            std::cerr << "Error: " << qPrintable(ftp.errorString())
                      << std::endl;
        }
        else
        {
            doneFlag = 1;
            std::cerr << "File downloaded as "
                      << qPrintable(file.fileName()) << std::endl;
        }
        file.close();
        emit done();
    }
    int FtpCtrl::getDone()
    {
        return doneFlag;
    }

    创建ftpCtrl对象,通过setHost函数指定ftp地址和端口(默认端口21)。

    FtpCtrl ftpCtrl;
    ftpCtrl.setHost("192.168.3.105");

    调用getFile函数下载指定路径文件。

    if(!ftpCtrl.getFile(path,name))
        return false;

    调用getDone函数返回下载是否成功/完成。

    int FtpThread::waitForDone()
    {
        while(ftpCtrl.getDone()==0)
        {
            QThread::msleep(100);
        }
        if(ftpCtrl.getDone()>0)
            return true;
        else
            return false;
    }
  • 相关阅读:
    [HDU 3038] How Many Answers Are Wrong
    [BZOJ 4977][Lydsy1708月赛]跳伞求生
    [BZOJ4974] 字符串大师
    总结-exCRT
    [luogu 4777] exCRT
    [AHOI 2009] 中国象棋
    JavaScript MVC框架PK:Angular、Backbone、CanJS与Ember
    十一黄金周 加班加点随笔
    从两个设计模式到前端MVC-洪宇
    Todo&Rocket
  • 原文地址:https://www.cnblogs.com/hebaichuanyeah/p/6083036.html
Copyright © 2011-2022 走看看