Qt implements FTP file transfer protocol

        FTP (File Transfer Protocol) is a set of standard protocols for file transfer on the network. It belongs to the application layer of the network transfer protocol. Its main function is to transfer files between the server and the client. This protocol uses clear text transmission. In addition to simply transferring and managing files, the function of the FTP server can also provide the following main functions based on the configuration architecture of the server software:

(1) Different users: By default, the FTP server is divided into three different identities based on the user's login status, namely: real user; guest; anonymous user.

(2) Command recording and log file recording: FTP can use the system's syslogd to record data, and the recorded data includes records of commands and user transmission data (transmission time, file size, etc.) that the user has used, so Various log information can be found in /var/log/.

(3) Directory that restricts user activities (change root, chroot for short): In order to prevent users from switching directories at will in your Linux system, limit the user's working scope to the user's home directory. FTP can restrict users to only activities in their own user home directory. When a user logs in to FTP, since the user cannot leave his/her user home directory, the displayed root directory is the content of his or her user home directory. This environment is called change root, which is chroot, which means changing the root directory.

1. Import FTP related classes into Qt

The FTP related classes in Qt were deleted after Qt5. When using them, we need to add these files to the project.

Link: https://pan.baidu.com/s/117nfMYsIdPWauYB7Ur_2Nw?pwd=u8ub 
Extraction code: u8ub 

2.FTP initialization and main signals and slots
    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 upload files
//上传文件
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 download files
//下载文件
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 gets all files in the current directory
ftp->list(dir_path);
6.FTP status change slot function
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();
    }
}

Note: FTP may disconnect and reconnect when no operations are performed for a long time.

7.FTP command execution completes the slot function
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,"操作提示","下载成功");
        }
    }
}

Note: After executing the command, getting the current command may become None, so we'd better set a flag or an enumeration variable before executing the FTP operation.

8. FTP obtains the file information slot function, which is executed when the list command is executed.
void MainWindow::slot_listInfo(QUrlInfo info)
{
    if(info.isDir())
        list_dir.append(info.name());
    else if(info.isFile())
        list_file.append(info.name());
}

Guess you like

Origin blog.csdn.net/weixin_55238862/article/details/135419803