You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
2.7 KiB
C++
110 lines
2.7 KiB
C++
/*
|
|
* To change this license header, choose License Headers in Project Properties.
|
|
* To change this template file, choose Tools | Templates
|
|
* and open the template in the editor.
|
|
*/
|
|
|
|
/*
|
|
* File: FtpCLient.cpp
|
|
* Author: shjd
|
|
*
|
|
* Created on 2019年12月4日, 上午10:35
|
|
*/
|
|
|
|
#include "FtpCLient.h"
|
|
FtpCLient::FtpCLient()
|
|
{
|
|
m_pManager = new QNetworkAccessManager();
|
|
m_pUrl = new QUrl();
|
|
m_pUrl->setScheme("ftp");
|
|
fileputstate=1;
|
|
filegetstate=1;
|
|
}
|
|
|
|
|
|
|
|
|
|
void FtpCLient::finished_get(QNetworkReply * reply)
|
|
{
|
|
if(reply->error() == QNetworkReply::NoError)
|
|
{
|
|
m_gpFile->write(reply->readAll());
|
|
reply->deleteLater();
|
|
m_gpFile->flush();
|
|
m_gpFile->close();
|
|
}
|
|
else
|
|
{
|
|
qDebug()<<"Error: "<<reply->error();
|
|
filegetstate=0;
|
|
}
|
|
}
|
|
|
|
void FtpCLient::finished_put(QNetworkReply * reply)
|
|
{
|
|
if(reply->error() == QNetworkReply::NoError)
|
|
{
|
|
reply->deleteLater();
|
|
m_ppFile->close();
|
|
}
|
|
else
|
|
{
|
|
qDebug()<<"Error: "<<reply->error();
|
|
fileputstate=0;
|
|
}
|
|
}
|
|
|
|
|
|
//设置FTP服务器用户名和密码
|
|
int FtpCLient::FtpGetState()
|
|
{
|
|
return filegetstate;
|
|
}
|
|
//设置地址和端口
|
|
int FtpCLient::FtpPutState()
|
|
{
|
|
return fileputstate;
|
|
}
|
|
|
|
//设置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)
|
|
{
|
|
filegetstate=1;
|
|
m_gpFile = new QFile(dev);
|
|
m_gpFile->open(QIODevice::WriteOnly);
|
|
m_pUrl->setPath(sor);
|
|
m_pReply = m_pManager->get(QNetworkRequest(*m_pUrl));
|
|
connect(m_pManager,SIGNAL(finished(QNetworkReply*)),this,SLOT(finished_get(QNetworkReply *)));
|
|
connect(m_pReply, SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(loadError(QNetworkReply::NetworkError)));
|
|
}
|
|
//上传文件
|
|
void FtpCLient::FtpPut(QString source, QString dev)
|
|
{
|
|
fileputstate=1;
|
|
m_ppFile = new QFile(source);
|
|
m_ppFile->open(QIODevice::ReadOnly);
|
|
QByteArray data = m_ppFile->readAll();
|
|
m_pUrl->setPath(dev);
|
|
m_pReply =m_pManager->put(QNetworkRequest(*m_pUrl), data);
|
|
connect(m_pManager,SIGNAL(finished(QNetworkReply*)),this,SLOT(finished_put(QNetworkReply *)));
|
|
connect(m_pReply, SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(loadError(QNetworkReply::NetworkError)));
|
|
}
|
|
|
|
void FtpCLient::loadError(QNetworkReply::NetworkError) //传输中的错误输出
|
|
{
|
|
fileputstate=0;
|
|
filegetstate=0;
|
|
qDebug()<<"Error: "<<m_pReply->error();
|
|
} |