Qt ftp 文件上传工具开发

一、需求:
1.简单的配置ftp参数界面
2.tcp 客户端端,接收服务器下发的参数信息
3.用户上传操作界面在这里插入图片描述
在这里插入图片描述在这里插入代码片
二、源码`#-------------------------------------------------

#-------------------------------------------------
#
# Project created by QtCreator 2021-06-24T15:49:34
#
#-------------------------------------------------QT       += core gui networkgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = FtpFileShareTool
TEMPLATE = appSOURCES += main.cpp\FtpFileTool.cpp \DialogFTPSetting.cppHEADERS  += FtpFileTool.h \DialogFTPSetting.hFORMS    += FtpFileTool.ui \DialogFTPSetting.ui
// 主操作界面
#ifndef FTPFILETOOL_H
#define FTPFILETOOL_H#include <QWidget>
#include <QString>
#include <QUrl>
#include <QFile>
#include <QNetworkReply>
#include <QNetworkAccessManager>
#include "DialogFTPSetting.h"namespace Ui {
class FtpFileTool;
}class FtpFileTool : public QWidget
{Q_OBJECTpublic:explicit FtpFileTool(QWidget *parent = 0);~FtpFileTool();// 上传文件void put(const QString &fileName, const QString &path);private slots:void slot_put();void slot_applyNewConfig();void slot_uploadProgress(qint64 bytesSent, qint64 bytesTotal);   // 更新上传进度void slot_error(QNetworkReply::NetworkError code);void slot_replyFinished(QNetworkReply*);private:Ui::FtpFileTool *ui;DialogFTPSetting m_dlgFtpSetting;QUrl m_pUrl;        // 远程 ftp url 地址QFile m_file;QNetworkAccessManager *m_manager;QNetworkReply *m_pReply;QFile *m_pFile;
};#endif // FTPFILETOOL_H
#include "FtpFileTool.h"
#include "ui_FtpFileTool.h"
#include <QMessageBox>FtpFileTool::FtpFileTool(QWidget *parent) :QWidget(parent),ui(new Ui::FtpFileTool)
{ui->setupUi(this);connect(ui->pushButtonFTPSetting, &QPushButton::clicked, &m_dlgFtpSetting, &DialogFTPSetting::exec);m_pUrl.setScheme("ftp");m_manager = new QNetworkAccessManager(this);connect(&m_dlgFtpSetting, &DialogFTPSetting::sig_ftpConfigChanged, this, &FtpFileTool::slot_applyNewConfig);connect(ui->pushButtonPut, &QPushButton::clicked, this, &FtpFileTool::slot_put);connect(ui->lineEditRemotePath, &QLineEdit::textChanged, &m_dlgFtpSetting, &DialogFTPSetting::slot_setNewRemoteConfig);connect(m_manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(slot_replyFinished(QNetworkReply*)));
}FtpFileTool::~FtpFileTool()
{delete ui;
}void FtpFileTool::slot_put()
{m_pFile = new QFile(ui->lineEditLocalPath->text());qDebug() <<"src file : "<< ui->lineEditLocalPath->text().toLatin1();if(false == m_pFile->open(QIODevice::ReadOnly)){QMessageBox messageBox(this);messageBox.setWindowTitle("erro");messageBox.setText("open file error");messageBox.exec();}else{QByteArray data = m_pFile->readAll();m_pUrl.setPath(ui->lineEditRemotePath->text());m_manager->setNetworkAccessible(QNetworkAccessManager::Accessible);m_pReply = m_manager->put(QNetworkRequest(m_pUrl), data);qDebug() << "文件内容:" << data;connect(m_pReply, &QNetworkReply::uploadProgress, this, &FtpFileTool::slot_uploadProgress);connect(m_pReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slot_error(QNetworkReply::NetworkError)));if (m_pReply->error() != QNetworkReply::NoError){qDebug() << "Error: " << m_pReply->errorString();}m_pFile->close();}
}void FtpFileTool::slot_applyNewConfig()
{    m_pUrl.setHost(m_dlgFtpSetting.getRemoteIP());m_pUrl.setPort(21);m_pUrl.setUserName(m_dlgFtpSetting.getUserName());m_pUrl.setPassword(m_dlgFtpSetting.getPassword());if (m_dlgFtpSetting.getRemotePathSetting()!= "" ){m_pUrl.setPath(m_dlgFtpSetting.getRemotePathSetting().toLatin1());qDebug() <<"ftp 文件设置 " <<m_dlgFtpSetting.getRemotePathSetting().toLatin1();ui->lineEditRemotePath->setText(m_dlgFtpSetting.getRemotePathSetting());}qDebug() <<  m_pUrl.host() << m_pUrl.port() << m_pUrl.userName() << m_pUrl.password();//检测URL地址是否合法if (!m_pUrl.isValid()){QMessageBox::critical(NULL, tr("Error"), "URL地址不合法!" );}else if (m_pUrl.scheme() != "ftp"){QMessageBox::critical(NULL, tr("Error"), "URL地址必须以ftp开头!");}
}// 更新上传进度
void FtpFileTool::slot_uploadProgress(qint64 bytesSent, qint64 bytesTotal)
{ui->progressBarUpload->setMaximum(bytesTotal);ui->progressBarUpload->setValue(bytesSent);
}void FtpFileTool::slot_error(QNetworkReply::NetworkError code)
{QMessageBox messageBox(this);messageBox.setWindowTitle("erro");messageBox.setText("upload error, "+QString::number(code).toLatin1());messageBox.exec();
}// 删除指针,更新和关闭文件
void FtpFileTool::slot_replyFinished(QNetworkReply*)
{if (m_pReply->error() == QNetworkReply::NoError){m_pReply->deleteLater();m_pFile->flush();m_pFile->close();}else{QMessageBox::critical(NULL, tr("Error"), "错误!!!");}
}
// 参数配置界面和参数接收
#ifndef DIALOGFTPSETTING_H
#define DIALOGFTPSETTING_H#include <QDialog>
#include <QTcpSocket>namespace Ui {
class DialogFTPSetting;
}class DialogFTPSetting : public QDialog
{Q_OBJECTpublic:explicit DialogFTPSetting(QWidget *parent = 0);~DialogFTPSetting();QString getRemotePathSetting();QString getRemoteIP();QString getRemotePort();QString getUserName();QString getPassword();public slots:void slot_setNewRemoteConfig(const QString strPath);signals:void sig_ftpConfigChanged();    // 参数配置改变的时候发出信号private slots:void slot_getRemoteConfig();    // 接受远程参数配置数据void slot_connectOrDisconnect();   // 确认参数,并建立链接, 接受远程参数设置void slot_updateAsDisconnected();   // 被动断开界面刷新void slot_sureConfig();private:Ui::DialogFTPSetting *ui;QTcpSocket m_clientSocket;
};#endif // DIALOGFTPSETTING_H```cpp
#include "DialogFTPSetting.h"
#include "ui_DialogFTPSetting.h"
#include <QHostAddress>
DialogFTPSetting::DialogFTPSetting(QWidget *parent) :QDialog(parent),ui(new Ui::DialogFTPSetting)
{ui->setupUi(this);connect(ui->pushButtonConnect, &QPushButton::clicked, this, &DialogFTPSetting::slot_connectOrDisconnect);connect(&m_clientSocket, &QTcpSocket::disconnected, this, &DialogFTPSetting::slot_updateAsDisconnected);connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &DialogFTPSetting::slot_sureConfig);
}DialogFTPSetting::~DialogFTPSetting()
{delete ui;
}QString DialogFTPSetting::getRemotePathSetting()
{return ui->lineEdit_RecvMsg->text().toLatin1();
}QString DialogFTPSetting::getRemoteIP()
{return ui->lineEditIP->text();
}QString DialogFTPSetting::getRemotePort()
{return ui->lineEditPort->text();
}QString DialogFTPSetting::getUserName()
{return ui->lineEditUserName->text();
}QString DialogFTPSetting::getPassword()
{return ui->lineEditPassword->text();
}void DialogFTPSetting::slot_getRemoteConfig()
{QByteArray recvByts = m_clientSocket.readAll();QString strRecv = QString(recvByts);ui->lineEdit_RecvMsg->setText(strRecv.toLatin1());//    m_remoteConfig = strRecv.toLatin1();
}void DialogFTPSetting::slot_setNewRemoteConfig(const QString strPath)
{ui->lineEdit_RecvMsg->setText(strPath);
}void DialogFTPSetting::slot_connectOrDisconnect()
{if(ui->pushButtonConnect->text() == tr("断开")){m_clientSocket.disconnectFromHost();ui->lineEditIP->setEnabled(true);ui->lineEditPassword->setEnabled(true);ui->lineEditPort->setEnabled(true);ui->lineEditUserName->setEnabled(true);ui->pushButtonConnect->setText("连接");}else{quint16 port = ui->lineEditPort->text().toUInt();QHostAddress ip(ui->lineEditIP->text());m_clientSocket.connectToHost(ip, port);// 监听接受配置connect(&m_clientSocket, &QIODevice::readyRead, this, &DialogFTPSetting::slot_getRemoteConfig);if(m_clientSocket.isOpen()){ui->lineEditIP->setEnabled(false);ui->lineEditPassword->setEnabled(false);ui->lineEditPort->setEnabled(false);ui->lineEditUserName->setEnabled(false);ui->pushButtonConnect->setText("断开");}}
}void DialogFTPSetting::slot_updateAsDisconnected()
{ui->lineEditIP->setEnabled(true);ui->lineEditPassword->setEnabled(true);ui->lineEditPort->setEnabled(true);ui->lineEditUserName->setEnabled(true);ui->pushButtonConnect->setText("连接");
}void DialogFTPSetting::slot_sureConfig()
{emit sig_ftpConfigChanged();
}

三、坑
1.远程路径, 使用QUrl 的setPath() 函数来设定远程文件时候,文件路径不包含ftp://ip// , 这个部分应该是在 设置ftp ip 时候已经默认了,用户在setpath 只要告诉 下图 path 这部分,否则会有 网络错误码201
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/269243.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

微软2008系列 (Orcas + Longhorn Server+SQL2008)将于2008年2月27日发布

据John ( Windows Server Blog, com.coms news blog)&#xff0c;Windows Server 2008, Visual Studio 2008 和 Microsoft SQL Server 2008 将于2008年2月27日 "launch",希望微软不要习惯性的跳票 又据ScottGu 回复&#xff1a;One correction above - the "la…

[Err] ORA-00979: not a GROUP BY expression

not a GROUP BY expression异常产生是因为group by用法的问题。 在使用group by 时&#xff0c;有一个规则需要遵守&#xff0c;即出现在select列表中的字段&#xff0c;如果没有在组函数中&#xff0c;那么必须出现在group by 子句中。&#xff08;select中的字段不可以单独出…

python进程和线程

python 进程和线程 概念 GIL: 全局解释锁&#xff0c;解决了不同线程同时访问统一资源时&#xff0c;数据保护问题。python 虽然是多线程&#xff0c;但是因为GIL,实际上是是单线程&#xff0c;由CPU轮询&#xff0c;假线程。&#xff08;一个线程运行一段时间后会释放GIL, 另一…

arm-linux 交叉编译链接动态库使用

alientekubuntu16:~/code/256APP/App/Module256App/test$ cat build.sh arm-linux-gnueabihf-g TestModule.cpp -I ../include -L ../lib/release -L ./ libModuleSdk.so -L ./ libHalAPI.so -stdc11 -I &#xff1a;指定库的头文件目录 -L &#xff1a;指定库文件.so 所在…

2007高考:考生要根据家庭经济条件慎重填报按办学成本收费的高校及专业

2007高考&#xff1a;考生要根据家庭经济条件慎重填报按办学成本收费的高校及专业来源&#xff1a;[url]http://www.accp-teem.com.cn/ArticleView/2007-7-14/Article_View_1181.Htm[/url] 近年来&#xff0c;普通高校招生中中外合作办学的专业越来越多&#xff0c;中外合作办学…

mybatis中大于等于小于等于的写法

第一种写法&#xff08;1&#xff09;&#xff1a;原符号 < < > > & " 替换符号 < < > > &amp; &apos; &quot; 例如&#xff1a;sql如下&#xff1a; create_date…

makefile编写

多源文件 第三方库 testApp:testApp.o UdpSender.o arm-linux-gnueabihf-g -o testApp testApp.o UdpSender.o -L ./test libi2csmbus.so libads1115.so libHalAPI.so libModuleSdk.so -lpthread --stdgnu11 testApp.o: testApp.cpparm-linux-gnueabihf-g -c testApp.cpp -I…

C++习题 虚函数-计算图形面积

C习题 虚函数-计算图形面积 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 122 Solved: 86[Submit][Status][Web Board]Description 编写一个程序&#xff0c;定义抽象基类Shape&#xff0c;由它派生出5个派生类&#xff1a; Circle(圆形)、Square(正方形)、Rectangle(矩形…

〈理解〉OSI七层

第7层<Application应用层>—直接对应用程序提供服务&#xff0c;应用程序可以变化&#xff0c;但要包括电子消息传输第6层<Presentation表示层>—格式化数据&#xff0c;以便为应用程序提供通用接口。这可以包括加密服务、解压第5层<Session会话层>—在两个节…

Ubuntu 开机 Firmware Bug , Bios corrupted

因为Windows 死机&#xff0c;断电后 vmware 虚拟机开机后、进入ubuntu 出现linux启动选项 进入后界面无法打开 一直是命令输入行 解决方法 输入命令&#xff1a;fsck -y /dev/sda1 等待完成修复后再输入 exit

Vboxmanage改动uuid报错的解决的方法

我的环境&#xff1a; Virtualbox 4.3.10 r93012 操作系统&#xff1a;win7 问题&#xff1a;Virtualbox在使用拷贝的虚拟盘时会提示uuid冲突&#xff1a; Because a hard disk with uuid ‘’ already exists. 依照网上的说法&#xff0c;执行VBoxManage改动uuid报错&#xff…

SQL Server连接中的常见错误

SQL Server连接中的常见错误:一."SQL Server 不存在或访问被拒绝"这个是最复杂的,错误发生的原因比较多,需要检查的方面也比较多.一般说来,有以下几种可能性:1,SQL Server名称或IP地址拼写有误2,服务器端网络配置有误3,客户端网络配置有误要解决这个问题,我们一般要遵…

QT源码交叉编译

交叉编译QT 源码 板子&#xff1a;全志 V3S , arm32位cpu ubuntu 虚拟机搭建好交叉编译链环境&#xff0c;添加环境变量 ok3399ubuntu:~$ echo $PATH /opt/OK3399-linux-release/host/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/lo…

JQ表单序列化变成 对象

JQ表单序列化变成 对象 function serializeObject(form) { var o {}; $.each(form.serializeArray(), function(index) { if (o[this[name]]) { o[this[name]] o[this[name]] ";" this[value]; } else { o[this[name]] this[value]; } }); return o;} 、、、、、…

即将从TechReady5归来

TechReady是微软内部面向Services、DPE、TS等部门的技术会议&#xff0c;每年两次&#xff0c;这次是第5次。听了几天课&#xff0c;虽说很多内容有点旧&#xff0c;但其中还是有不少好的东东&#xff0c;呵呵。 TechReady5的第2天&#xff0c;Bill Gates给了一节General Sessi…

linux交叉编译无法识别gcc编译器

使用 arm gcc 编译时候 32位编译器无法识别 原因没有安装下面两个库导致我当时编译 qt 源码时报没有 compiler 在 编译工具存放文件夹下 查看编译器版本也无法识别编译器 64位ubunutu安装 32 位依赖库后即可 sudo apt-get install lib32ncurses5 lib32z1sudo apt-getinstall …

安装redis出现cc adlist.o /bin/sh:1:cc:not found

安装redis时 提示执行make命令时&#xff0c; 提示 CC adlist.o /bin/sh: cc: 未找到命令 问题原因&#xff1a;这是由于系统没有安装gcc环境&#xff0c;因此在进行编译时才会出现上面提示&#xff0c;当安装好gcc后再进行编译时&#xff0c;上面错误提示将消失。 解决方法&am…

理想的 ASP.NET AJAX (Part 1 - Client Centric)

怎样的AJAX才算是理想&#xff1f; 要说什么是理想的ASP.NET AJAX&#xff0c;就要先说说什么是理想的AJAX。事实上AJAX最不理想的地方在于search engine friendly以及bookmarkable&#xff0c;这两个问题有一定的相似性&#xff0c;要解决并不难&#xff0c;只是每一个系统中实…