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;
    }
  • 相关阅读:
    aaa
    https://download.csdn.net/download/qq_33200967/10679367
    hadoop修改
    xa
    commit
    mybatis
    centos7 部署openstf
    selenium api docs
    Chrome浏览器在自动化中的应用
    selenium自动化测试各浏览器驱动下载地址
  • 原文地址:https://www.cnblogs.com/hebaichuanyeah/p/6083036.html
Copyright © 2011-2022 走看看