zoukankan      html  css  js  c++  java
  • QT QFtp使用实例 从FTP下载一个文件

    1. ftp://ftp.denx.de/pub/u-boot/lowboot-1.0.0.patch.gz  下载文件

    FtpGet.h

    #ifndef FTPGET_H
    #define FTPGET_H
    
    #include <QUrl>
    #include <QFtp>
    #include <QFile>
    
    class Ftpget : public QObject
    {
        Q_OBJECT
    public:
        Ftpget(QObject *parent=0);
        bool getFile(const QUrl &url);
    
    signals:
        void done();
    
    private slots:
        void ftpDone(bool error);
    
    private:
        QFtp ftp;
        QFile file;
    };
    
    #endif // FTPGET_H
    
    FtpGet.cpp

    #include "FtpGet.h"
    #include <iostream>
    #include <QFileInfo>
    
    Ftpget::Ftpget(QObject *parent)
        :QObject(parent)
    {
        connect(&ftp, SIGNAL(done(bool)), this, SLOT(ftpDone(bool)));
    }
    
    bool Ftpget::getFile(const QUrl &url)
    {
        if( !url.isValid() )
        {
            std::cerr << "Error: Invaild URL" << std::endl;
            return false;
        }
        if( url.scheme() != "ftp" )
        {
            std::cerr << "Error: URL must start with 'ftp'" << std::endl;
            return false;
        }
        if( url.path().isEmpty() )
        {
            std::cerr << "Error: URL has no path " << std::endl;
            return false;
        }
    
        QString localFileName = QFileInfo(url.path()).fileName();
        if( localFileName.isEmpty() )
            localFileName = "ftpget.out";
    
        file.setFileName(localFileName);
        if( !file.open(QIODevice::WriteOnly) )
        {
            std::cerr << "Error: cannot write file"
                 << qPrintable(file.fileName()) << ":"
                 << qPrintable(file.errorString()) << std::endl;
            return false;
        }
    
        ftp.connectToHost(url.host(), url.port(21));
        ftp.login();
        ftp.get(url.path(), &file);
        ftp.close();
        return true;
    }
    
    void Ftpget::ftpDone(bool error)
    {
        if( error )
            std::cerr << "Error: " << qPrintable(ftp.errorString()) << std::endl;
        else
            std::cerr << "File downloaded as " << qPrintable(file.fileName()) << std::endl;
        file.close();
        emit done();
    }
    
    main.cpp
    #include <QtGui/QApplication>
    #include <QCoreApplication>
    #include <QStringList>
    #include <iostream>
    #include "FtpGet.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        QStringList args = QCoreApplication::arguments();
        if( args.count() != 2 )
        {
            std::cerr << "Ex: ftpget ftp://xxxxx" << std::endl;
            return 1;
        }
    
        Ftpget getter;
        if( !getter.getFile(QUrl(args[1])) )
            return 1;
        QObject::connect(&getter, SIGNAL(done()), &a, SLOT(quit()));
    
        return a.exec();
    }
    



  • 相关阅读:
    Tomcat单机多实例部署
    通过Zabbix监控Tomcat单机多实例
    Swarm容器集群管理(超详细)
    15、基于consul+consul-template+registrator+nginx实现自动服务发现
    14、Docker监控方案(Prometheus+cAdvisor+Grafana)
    创建python虚拟环境
    Linux系统基础优化及常用命令
    Linux常用命令--用户管理,文件权限,打包命令等
    Linux--CentOS7使用firewalld打开关闭防火墙与端口
    vim
  • 原文地址:https://www.cnblogs.com/xj626852095/p/3648126.html
Copyright © 2011-2022 走看看