封装QNetworkAccessManager实现Ftp的上传
效果:
头文件:
#ifndef FTPMANAGER_H
#define FTPMANAGER_H
/**
* @brief Ftp文件上传,支持大文件
* @author HAN
* @date 2017-11-8
*/
#include <QString>
#include <QDebug>
#include <QUrl>
#include <QNetworkAccessManager>
#include <QFile>
#include <QByteArray>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
class FtpManager
{
public:
FtpManager(QString rftpUsesrname,QString rftpUserPwd,QString rftpIP,int rftpPort = 21); //账号登录
QString uploadFile(QString path_fileName,QString ftpFileName);
QNetworkReply *getRelay();
private:
QFile *sourceFile;
QByteArray by_txt;
QNetworkAccessManager *manager;
QUrl *ftpUrl;
QString ftpIP;
QString ftpUsesrname;
int ftpPort;
QString ftpUserPwd;
QNetworkReply *replyOrg;
};
#endif // FTPMANAGER_H
cpp:
#include "ftpmanager.h"
#include <QException>
/**
* @brief 账号登录,初始化
* @author HAN
* @date 2017-11-8
*/
FtpManager::FtpManager(QString rftpUsesrname,QString rftpUserPwd,QString rftpIP,int rftpPort)
{
ftpIP = rftpIP;
ftpUsesrname = rftpUsesrname;
ftpPort = rftpPort;
ftpUserPwd = rftpUserPwd;
}
/**
* @brief 利用QNetworkAccessManager上传文件到ftp
* @author HAN
* @date 2017-11-8
* @param fileName 上传的文件,包含路径
* @param ftpFileName 服务器上的路径
* @return
*/
QString FtpManager::uploadFile(QString path_fileName,QString ftpFileName)
{
if(ftpIP == "")
{
return "请设置ftp服务器信息";
}
try{
sourceFile = new QFile(path_fileName); //设置文件路径
sourceFile->open(QIODevice::ReadOnly); //读取模式
//by_txt=sourceFile->readAll(); //全部读取,遇到大文件时会内存溢出
//sourceFile->close(); //关闭文件
manager = new QNetworkAccessManager;
ftpUrl = new QUrl(); //设置QUrl数据
ftpUrl->setScheme("ftp"); //设置URL的类型(或协议)
ftpUrl->setHost(ftpIP); //设置主机地址
ftpUrl->setPort(ftpPort); //端口
ftpUrl->setUserName(ftpUsesrname); //ftp用户名
ftpUrl->setPassword(ftpUserPwd); //ftp密码
ftpUrl->setPath(ftpFileName);//设置路径+文件名,这里为ftp根目录
replyOrg = manager->put(QNetworkRequest(*ftpUrl), sourceFile); //上传
}catch(QException e)
{
return "错误:"+QString(e.what());
}
return "success";
}
/**
* @brief 获取上传得到的QNetworkReply,用于进度条显示、获取上传结果等
* @date 2017/12/15
* @param han
* @return void
*/
QNetworkReply *FtpManager::getRelay()
{
return replyOrg;
}
用法:
FtpManager *ftp = new FtpManager(FtpNumber,FtpPwd,FtpServerIP);
ftp->uploadFile(path,fileName); //上传测试文件
reply = ftp->getRelay(); //获取
connect(reply, SIGNAL(finished()), this, SLOT(OnFinishRelay()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(upLoadError(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(uploadProgress(qint64 ,qint64)),this,SLOT(OnUploadProgress(qint64,qint64)));
槽函数
/**
* @brief 显示上传进度条
* @date 2017/12/15
* @param han
* @return void
*/
void OnUploadProgress(qint64 bytesSent, qint64 bytesTotal)
{
ui->uploadProgressBar->setValue(bytesSent*1.0/bytesTotal*100);
}
/**
* @brief 上传错误
* @date 2017/12/15
* @param han
* @return void
*/
void upLoadError(QNetworkReply::NetworkError errorCode)
{
}
/**
* @brief 发送结果回调函数
* @date 2017/12/15
* @param han
* @return void
*/
void OnFinishRelay()
{
if(reply->error() == QNetworkReply::NoError)
{
//成功
}
else
{
//错误
}
reply->deleteLater();
}