QT FTP上传,下载文件


#ifndef FTPCLIENT_H
#define FTPCLIENT_H
#include <QObject>
#include <QFile>
//#include <qnetworkaccessmanager.h>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkReply>
#include <QByteArray>
#include <qdebug.h>
#include <QMessageBox>
#include <QFileInfo>
#include <QDir>
#include <QTime>
#include <QMutex>
#include <QWaitCondition>

class FtpCLient:public QObject
{
    Q_OBJECT
    //enum LOADSTATUS{UPLOAD, DOWNLOAD};
signals:
    void process(int,int id);//ID为0下载,1上传
protected slots:
    void finished(QNetworkReply * reply);


    void downloadProgress(qint64,qint64);


    void uploadProgress(qint64 bytesSent, qint64 bytesTotal);


    void errorCode(QNetworkReply::NetworkError code);
public:
    FtpCLient();

    void FtpGet(QString sor, QString dev);

    void FtpPut(QString source, QString dev);

    void FtpSetUserInfor(QString user= NULL, QString pwd= NULL);

    void FtpSetHostPort(QString str, int port =21);

    QString _ToSpecialEncoding(const QString &InputStr);

private:
    QFile * m_pFile;

    QNetworkReply *m_pReply;

    QNetworkAccessManager * m_pManager;

    QUrl * m_pUrl;

    QMutex  m_mutex;

    QWaitCondition m_wait;

    bool m_bUpload;
};


#endif // FTPCLIENT_H





#include "ftpclient.h"
#include <QTextCodec>
//QByteArray data;


FtpCLient::FtpCLient()
{
    m_bUpload = false;
    m_pManager = new QNetworkAccessManager();
    m_pUrl = new QUrl();
    m_pUrl->setScheme("ftp");
    //m_pUrl->fromEncoded()

    connect(m_pManager,SIGNAL(finished(QNetworkReply*)),this,SLOT(finished(QNetworkReply *)));
}

void FtpCLient::finished(QNetworkReply * reply)
{
    if(reply->error() == QNetworkReply::NoError)
    {
        if(m_bUpload)
            return;

        reply->request();
        m_pFile->write(reply->readAll());
        //qDebug()<<time.elapsed();
        m_pFile->flush();
        m_pFile->close();
        reply->deleteLater();
    }
    else
    {
        if(m_bUpload)
        {
            QMessageBox::warning(NULL, tr("warning"), "upload fail!!!!!");
        }
        else
        {
            QMessageBox::warning(NULL, tr("warning"), tr("download fail!!!"));
        }
    }
    //m_mutex.unlock();
}

void FtpCLient::downloadProgress(qint64 bytesRev,qint64 bytesTotal)
{
    if(bytesTotal <0)
        return;

    int x = (float)bytesRev/(float)bytesTotal*100;
    emit process(x, 0);
}

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::uploadProgress(qint64 bytesSent, qint64 bytesTotal)
{
    if(bytesTotal <0)
        return;

    int x = (float)bytesSent/bytesTotal*100;
    emit process(x, 1);
}

void FtpCLient::errorCode(QNetworkReply::NetworkError code)
{
    QMessageBox::warning(NULL, tr("警告"), tr("FTP operate fail!!!!"));
}

void FtpCLient::FtpGet(QString sor, QString dev)
{
    m_bUpload = false;
    QDir dir("./image");
    if(!dir.exists())
    {
        QString str = dir.currentPath();
        str += "/image";
        dir.mkdir(str);
    }

    QFileInfo info;
    info.setFile(dir, dev);
    m_pFile = new QFile(info.filePath());
//    if(m_pFile->exists())
//        return;


    m_pFile->open(QIODevice::WriteOnly);
    m_pUrl->setPath(_ToSpecialEncoding(sor));

    m_pReply = m_pManager->get(QNetworkRequest(*m_pUrl));
    connect(m_pReply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
    connect(m_pReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(errorCode(QNetworkReply::NetworkError)));
}

QString FtpCLient::_ToSpecialEncoding(const QString &InputStr)
{
#ifdef Q_OS_WIN
    return QString::fromLatin1(InputStr.toLocal8Bit());
#else
    QTextCodec *codec= QTextCodec::codecForName("gbk");
    if (codec)
    {
        return QString::fromLatin1(codec->fromUnicode(InputStr));
    }
    else
    {
        return QString("");
    }
#endif
}

void FtpCLient::FtpPut(QString source, QString dev)
{
    m_bUpload = true;

    QFile file(source);
    file.open(QIODevice::ReadOnly);
    QByteArray data = file.readAll();

    QString str = _ToSpecialEncoding(dev);
    m_pUrl->setPath(str );

    m_pReply = m_pManager->put(QNetworkRequest(*m_pUrl), data);
    connect(m_pReply, SIGNAL(uploadProgress(qint64,qint64)), this, SLOT(uploadProgress(qint64,qint64)));
    connect(m_pReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(errorCode(QNetworkReply::NetworkError)));
}





  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值