Qt5.X FTP上傳與下載

Devere發表於2018-06-25

Cliet.h


#ifndef CLIET_H
#define CLIET_H
#include <QFile>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkReply>
#include <QByteArray>
#include <QMessageBox>
#include <QFileInfo>
#include <QDir>
class FtpCLient:public QObject
{
    Q_OBJECT
protected slots:
    void finished(QNetworkReply * reply);
public:
    FtpCLient();
    void FtpGet(QString sor, QString dev);
    void FtpPut(QString source, QString dev);
    void FtpSetUserInfor(QString user, QString pwd);
    void FtpSetHostPort(QString str, int port =21);
private:
    QFile * m_pFile;
    QNetworkReply *m_pReply;
    QNetworkAccessManager * m_pManager;
    QUrl * m_pUrl;
};
#endif // CLIET_H

Cliet.cpp

#include "cliet.h"
FtpCLient::FtpCLient()
{
    m_pManager = new QNetworkAccessManager();
    m_pUrl = new QUrl();
    m_pUrl->setScheme("ftp");
    m_pUrl->setHost("192.168.0.217");
    connect(m_pManager,SIGNAL(finished(QNetworkReply*)),this,SLOT(finished(QNetworkReply *)));
}
void FtpCLient::finished(QNetworkReply * reply)
{
    m_pFile->write(reply->readAll());
    m_pFile->flush();
    m_pFile->close();
    reply->deleteLater();
}
//設定FTP伺服器使用者名稱和密碼
void FtpCLient::FtpSetUserInfor(QString user, QString pwd)
{
    m_pUrl->setUserName(user);
    m_pUrl->setPassword(pwd);
}
//設定地址和埠
void FtpCLient::FtpSetHostPort(QString str, int port )
{
    m_pUrl->setHost(str);
    m_pUrl->setPort(port);
}
//下載檔案
void FtpCLient::FtpGet(QString sor, QString dev)
{
    QFileInfo info;
    info.setFile(dev);
    m_pFile = new QFile(info.filePath());
    m_pFile->open(QIODevice::Append|QIODevice::WriteOnly);
    m_pUrl->setPath(sor);
    m_pReply = m_pManager->get(QNetworkRequest(*m_pUrl));
}
//上傳檔案
void FtpCLient::FtpPut(QString source, QString dev)
{
    qDebug()<<"start";
    QFile file(source);
    file.open(QIODevice::ReadOnly);
    QByteArray data = file.readAll();
    qDebug()<<"start1";
    m_pFile = new QFile(dev);
    m_pFile->open(QIODevice::ReadOnly|QIODevice::WriteOnly);
    m_pUrl->setPath(dev);
    m_pManager->put(QNetworkRequest(*m_pUrl), data);
    qDebug()<<"start2";
}

呼叫

void Debugwidget::on_input_clicked()
{
    QString file_path = QFileDialog::getOpenFileName(this,"請選擇待匯入ENI檔案","./","*.xml");
    if(file_path!="")
    {
        QString dev;
        for(int i=file_path.size()-1;i>=0;i--)
        {
            if(file_path[i]=='/')
            {
                dev = file_path.right(file_path.size()-i-1);
                break;
            }
        }
        client.FtpPut(file_path,"ENI/"+dev);
    }
}
void Debugwidget::on_inputOtherFile_clicked()
{
    QString file_path = QFileDialog::getOpenFileName(this,"請選擇待匯入其他檔案","./","*.xml");
    if(file_path!="")
    {
        QString dev;
        for(int i=file_path.size()-1;i>=0;i--)
        {
            if(file_path[i]=='/')
            {
                dev = file_path.right(file_path.size()-i-1);
                break;
            }
        }
        client.FtpPut(file_path,dev);
    }
}


相關文章