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();
    }
    



  • 相关阅读:
    P1141零一迷宫
    P1219八皇后
    P1233木棍加工
    三 Struts2 添加返回数据
    二 Struts2 接收数据
    一 Struts2 开发流程
    12-tinyMCE文本编辑器+图片上传预览+页面倒计时自动跳转
    11-page分页原理
    10-ajax技术简介
    9-文件上传和下载
  • 原文地址:https://www.cnblogs.com/xj626852095/p/3648126.html
Copyright © 2011-2022 走看看