QT---FTP练习,连接FTP服务器,浏览其中的文件,下载到本地目录中

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 
#include 
#include 
#include 

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->progressBar->setValue(0);
    connect(ui->fileList,SIGNAL(itemActivated(QTreeWidgetItem*,int)),this,SLOT(processItem(QTreeWidgetItem*,int))
);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::ftpCommandStarted(int)
{
    int id=ftp->currentCommand();
    switch (id) {
    case QFtp::ConnectToHost:
        ui->label->setText(tr("connecting to the qftp..."));
        break;
    case QFtp::Login:
        ui->label->setText(tr("logining ..."));
        break;
    case QFtp::Get:
        ui->label->setText(tr("loading ..."));
        break;
    case QFtp::Close:
        ui->label->setText(tr("run to close ..."));
    default:
        break;
    }
}

void MainWindow::ftpCommandFinished(int,bool error)
{
    if(ftp->currentCommand()==QFtp::ConnectToHost){
        if(error)
            ui->label->setText(tr("connection has found error: %1").arg(ftp->errorString()));
        else
        ui->label->setText(tr("connecting successful"));
    }
    else if(ftp->currentCommand()==QFtp::Login){
        if(error)
            ui->label->setText(tr("login has found error: %1").arg(ftp->errorString()));
        else
            ui->label->setText(tr("login successful"));
    }
    else if(ftp->currentCommand()==QFtp::Get){
        if(error)
             ui->label->setText(tr("load has found error: %1").arg(ftp->errorString()));
        else{
             ui->label->setText(tr("load successful"));
             file->close();
        }
        ui->downloadButton->setEnabled(true);

    }
    else if(ftp->currentCommand()==QFtp::List){
        if(isDirectory.isEmpty())
        {
            ui->fileList->addTopLevelItem(new QTreeWidgetItem(QStringList()setEnabled(false);
            ui->label->setText(tr("folder is empty"));
        }
    }
    else if(ftp->currentCommand()==QFtp::Close){
       ui->label->setText(tr("closed"));
    }


}

void MainWindow::updateDataTransferProgress(qint64 readBytes, qint64 totalBytes)
{
    ui->progressBar->setMaximum(totalBytes);
    ui->progressBar->setValue(readBytes);

}

void MainWindow::addToList(const QUrlInfo &urlInfo)
{
    QTreeWidgetItem *item=new QTreeWidgetItem;
    item->setText(0,urlInfo.name());
    item->setText(1,QString::number(urlInfo.size()));
    item->setText(2,urlInfo.owner());
    item->setText(3,urlInfo.group());
    item->setText(4,urlInfo.lastModified().toString("yyyy-MM-dd"));
    QPixmap pixmap;
    item->setIcon(0,pixmap);
    isDirectory[urlInfo.name()]=urlInfo.isDir();
    ui->fileList->addTopLevelItem(item);
    if(! ui->fileList->currentItem()){
        ui->fileList->setCurrentItem(ui->fileList->topLevelItem(0));
        ui->fileList->setEnabled(true);
    }

}

void MainWindow::processItem(QTreeWidgetItem *item, int)
{
    QString name=item->text(0);
    if(isDirectory.value(name)){
        ui->fileList->clear();
        isDirectory.clear();

        currentPath+="/download";
        currentPath+=name;
        ftp->cd(name);
        ftp->list();
        ui->cdToParentButton->setEnabled(true);
    }

}

void MainWindow::on_connectButton_clicked()
{
    ui->fileList->clear();
    currentPath.clear();
    isDirectory.clear();
    ftp=new QFtp(this);
    connect(ftp,SIGNAL(commandStarted(int)),this,SLOT(ftpCommandStarted(int)));
    connect(ftp,SIGNAL(commandFinished(int,bool)),this,SLOT(ftpCommandFinished(int,bool)));
    connect(ftp,SIGNAL(listInfo(QUrlInfo)),this,SLOT(addToList(QUrlInfo)));
    connect(ftp,SIGNAL(dataTransferProgress(qint64,qint64)),this,SLOT(updateDataTransferProgress(qint64,qint64)));
    QString ftpServer = ui->ftpServerlineEdit->text();
    QString userName = ui->userNamelineEdit->text();
    QString passWord = ui->passWordlineEdit->text();
    ftp->connectToHost(ftpServer,21);
    ftp->login(userName,passWord);

}

void MainWindow::on_cdToParentButton_clicked()
{
    ui->fileList->clear();
    isDirectory.clear();
    currentPath=currentPath.left(currentPath.lastIndexOf('/'));
    if(currentPath.isEmpty()){
        ui->cdToParentButton->setEnabled(false);
        ftp->cd("/");
    }else{
        ftp->cd(currentPath);
    }
    ftp->list();
}

void MainWindow::on_downloadButton_clicked()
{
    QDir *temp = new QDir;    //获取QDir的地址,不是复制一个QDir
        //判断文件夹是否存在
        bool exist = temp->exists("./download");
        if(exist)
            QMessageBox::warning(this,tr("creat folder"),tr("The folder is exist!"));
         else {
             bool ok = temp->mkdir("./download");    //当文件夹不存在时进行创建
             if(ok)
                 QMessageBox::warning(this,tr("creat folder"),tr("Creat the folder successful"));
            }

    QString filename = ui->fileList->currentItem()->text(0);
     filename = "download\\"+filename;     //文件保存在./download文件夹中
    file = new QFile(filename);

    if(!file->open(QIODevice::WriteOnly)){
        delete file;
        return ;
    }
    ui->downloadButton->setEnabled(false);
    ftp->get(ui->fileList->currentItem()->text(0),file);
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 

class QFtp;
class QFile;
class QUrlInfo;
class QTreeWidgetItem;


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QFtp *ftp;
    //存储一个路径是否为目录信息
    QHash  isDirectory;
    //存储现在的路径
    QString currentPath;
    //表示下载的文件
    QFile *file;

signals:


private slots:
    void ftpCommandStarted(int);
    void ftpCommandFinished(int,bool);
    //更新进度条
    void updateDataTransferProgress(qint64,qint64);
    //将服务器上的文件添加到Tree Widget部件中
    void addToList(const QUrlInfo &urlInfo);
    //双击一个目录时显示其内容
    void processItem(QTreeWidgetItem* , int);

    void on_connectButton_clicked();
    void on_cdToParentButton_clicked();
    void on_downloadButton_clicked();
};

#endif // MAINWINDOW_H

QT---FTP练习,连接FTP服务器,浏览其中的文件,下载到本地目录中_第1张图片

你可能感兴趣的:(QT)