0
点赞
收藏
分享

微信扫一扫

何为算法之什么是算法

自由情感小屋 2024-01-08 阅读 5
qt

        FTP(File Transfer Protocol,文件传输协议)是用于在网络上进行文件传输的一套标准协议,它属于网络传输协议的应用层。它最主要的功能是在服务器与客户端之间进行文件的传输。这个协议使用的是明文传输。FTP服务器的功能除了单纯的进行文件的传输与管理外,依据服务器软件的配置架构,它还可以提供以下几个主要功能:

1.Qt当中导入FTP相关类

Qt当中FTP相关类,在Qt5之后便删除了,在使用时我们需要将这几个文件加入到项目当中

2.FTP初始化及主要信号与槽
    ftp=new QFtp(this);
    ftp->setTransferMode(QFtp::Passive);
    connect(ftp,SIGNAL(stateChanged(int)),this,SLOT(slot_stateChanged(int)));
    connect(ftp,SIGNAL(commandFinished(int,bool)),this,SLOT(slot_command_finish(int,bool)));
    connect(ftp,SIGNAL(listInfo(QUrlInfo)),this,SLOT(slot_listInfo(QUrlInfo)));
3.FTP上传文件
//上传文件
void MainWindow::upload_file(QString src, QString dst)
{
    if(!(ftp->state()==QFtp::LoggedIn && ftp!=nullptr)){
        return;
    }
    if(up_file!=nullptr)
        delete up_file;
    up_file = new QFile(src);
    if (up_file != nullptr && up_file->open(QIODevice::ReadOnly)) {
        ftp->put(up_file, dst);
    }
}
4.FTP下载文件
//下载文件
void MainWindow::download_file(QString src, QString dst)
{
    if(!(ftp->state()==QFtp::LoggedIn && ftp!=nullptr)){
        return;
    }
    if(down_file!=nullptr)
        delete down_file;
    down_file = new QFile(dst);
    if (down_file != nullptr && down_file->open(QIODevice::WriteOnly)) {
        ftp->get(src, down_file);
    }
}
5.FTP获取当前目录下所有文件
ftp->list(dir_path);
6.FTP状态改变槽函数
void MainWindow::slot_stateChanged(int state)
{
    if(state==QFtp::Unconnected){
        ui->pushButton->setEnabled(false);
        ui->pushButton_2->setEnabled(false);
        ui->listWidget->setEnabled(false);
        ftp->connectToHost(ftp_ip,ftp_port);
        QMessageBox::warning(this,"操作提示","FTP连接断开");
    }
    else if(state == QFtp::Connected){
        ftp->login(ftp_user,ftp_password);
    }
    else if(state == QFtp::LoggedIn){
        ui->pushButton->setEnabled(true);
        ui->pushButton_2->setEnabled(true);
        ui->listWidget->setEnabled(true);
        ftp->list(dir_path);

        list_dir.clear();
        list_file.clear();
    }
}

注意:FTP在长时间没有进行任何操作时,可能会进行断开重连操作

7.FTP命令执行完成槽函数
void MainWindow::slot_command_finish(int id, bool fail)
{
    Q_UNUSED(id)

    if(fail){
        QMessageBox::warning(this,"操作提示",QString("%1 命令执行失败,%2").arg(ftp->currentCommand()).arg(ftp->errorString()));
    }
    else{
        if(ftp->currentCommand() == QFtp::List){
            update_list();
        }
        else if(ftp->currentCommand() == QFtp::Put){
            QMessageBox::information(this,"操作提示","上传成功");
            ftp->list(dir_path);
            list_dir.clear();
            list_file.clear();
            ui->label->setText(dir_path);
        }
        else if(ftp->currentCommand() == QFtp::Get){
            QMessageBox::information(this,"操作提示","下载成功");
        }
    }
}

注意:在执行完成命令之后,获取当前命令可能会变为None,所以我们在执行FTP操作之前最好设置一个标志位或一个枚举变量

8.FTP获取文件信息槽函数,在执行list命令时执行
void MainWindow::slot_listInfo(QUrlInfo info)
{
    if(info.isDir())
        list_dir.append(info.name());
    else if(info.isFile())
        list_file.append(info.name());
}
举报

相关推荐

0 条评论