QT 客户端上传文件到ftp服务器

QT 5将文件上传到ftp服务器(服务端无需写逻辑)

ftp上传文件

void FtpManager::FtpUpLoadFile(QString path)
{
    QFile*file = new QFile(path);
    file->open(QIODevice::ReadOnly);
    QByteArray byte_file = file->readAll();

    QNetworkAccessManager *accessManager = new QNetworkAccessManager(this);
    accessManager->setNetworkAccessible(QNetworkAccessManager::Accessible);
    QUrl url("ftp://***.**.***.**/picture/report.pdf");//填服务器地址
    url.setPort(21);
    url.setUserName("****");  // ftp服务器 用户名
    url.setPassword("*********");// ftp 服务器密码

    QNetworkRequest request(url);
    QNetworkReply*reply = accessManager->put(request, byte_file);
    connect(reply,&QNetworkReply::uploadProgress,[=](qint64 cur,qint64 total){
        ui->progressBar->setMaximum(total);
        ui->progressBar->setValue(cur);
    });
    connect(accessManager,&QNetworkAccessManager::finished,[=](){
        QMessageBox::information(this,"提示","上传完成!");
    });
}    

ftp下载文件

void FtpManager::FtpDownLoadFile(QString path)
{
     QFile *file = new QFile(path);
     file->open(QIODevice::WriteOnly);
     //QNetworkAccessManager *accessManager = new QNetworkAccessManager(this);
     this->setNetworkAccessible(QNetworkAccessManager::Accessible);
    //QUrl url("ftp://121.15.200.118/test.txt"); //QUrl url("ftp://159.75.127.51/picture/edx.jpg");ftp://162.168.8.244:26034/
     QUrl url("ftp://159.75.127.51/picture/edx.jpg");
     url.setPort(21);
     url.setUserName("liukai");
     url.setPassword("6GzBpXT8Ewb5PaNy");
     QNetworkRequest request(url);
     m_reply = this->get(request);
     connect(m_reply,&QNetworkReply::downloadProgress,[=](qint64 cur,qint64 total){
          emit SigPro(cur,total);
     });
     connect(m_reply,&QNetworkReply::readyRead,[=](){
         QByteArray data = m_reply->readAll();
         m_data.push_back(data);
         file->write(data);
         //emit SigShowPic(data);
     });

     connect(this,&QNetworkAccessManager::finished,[=](QNetworkReply*reply){
         emit SigDownLoadOk();
         file->close();
         if(m_reply){
             m_reply->deleteLater();
         }
     });
}

你可能感兴趣的:(QT,qt)