QT FTP上传,下载文件

来源:互联网 发布:阿里云对网宿科技影响 编辑:程序博客网 时间:2024/05/16 08:37

#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)));}


2 0
原创粉丝点击