#include "widget.h"
#include "ui_widget.h"
#include <QtNetwork>
#include <QFileDialog>
#pragma execution_character_set("utf-8")
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("文件传输客户端");
ui->sendButton->setEnabled(false);
m_payloadSize=64*1024;
m_totalBytes=0;
m_bytesWritten=0;
m_bytesToWrite=0;
ui->hostLineEdit->setText("127.0.0.1");
ui->portLineEdit->setText("9999");
m_tcpClient = new QTcpSocket(this);
// m_tcpClient->connectToHost()连接成功后会调用此函数
connect(m_tcpClient,SIGNAL(connected()),this,SLOT(startTransfer()));
// 执行m_tcpClient->write(m_outBlock)会触发此函数进行文件传输
connect(m_tcpClient,SIGNAL(bytesWritten(qint64)),this,SLOT(updateClientProgress(qint64)));
// m_tcpClient连接和传输过程中出错,均会调用此函数进行提示
connect(m_tcpClient,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
// connect(ui->sendButton,SIGNAL(clicked(bool)),this,SLOT(sendBtnClicked()));
// connect(ui->openButton,SIGNAL(clicked(bool)),this,SLOT(openBtnClicked()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::openFile()
{
m_fileName=QFileDialog::getOpenFileName(this);
if(!m_fileName.isEmpty())
{
ui->sendButton->setEnabled(true);
ui->clientStatusLabel->setText(QString("文件打开成功,请点击发送!"));
ui->lineEdit->setText(m_fileName);
}
}
void Widget::sendFile()
{
ui->sendButton->setEnabled(false);
m_bytesWritten=0;
ui->clientStatusLabel->setText("正在连接中...");
m_tcpClient->connectToHost(ui->hostLineEdit->text(),ui->portLineEdit->text().toInt());
}
void Widget::startTransfer()
{
// 打开需要传输的文件
m_localFile = new QFile(m_fileName);
if(!m_localFile->open(QFile::ReadOnly))
{
ui->clientStatusLabel->setText("文件打开失败!");
return;
}
// 初始化总大小字节数
m_totalBytes = m_localFile->size();
// 定义数据流
QDataStream sendOut(&m_outBlock,QIODevice::WriteOnly);
sendOut.setVersion(QDataStream::Qt_5_9);
QString strFileName = m_fileName.right(m_fileName.size()- m_fileName.lastIndexOf('/')-1);
//数据流中写入文件总大小、文件名大小、文件名,qint64(0)占位用
sendOut << qint64(0) << qint64(0) << strFileName;
// 修改总大小字节数
m_totalBytes += m_outBlock.size();
//修改数据流中写入的文件总大小、文件名大小
sendOut.device()->seek(0);
sendOut << m_totalBytes << qint64(m_outBlock.size()-sizeof(qint64)*2);
// 先调用m_tcpClient->write(m_outBlock)把m_outBlock中的文件总大小qint64、文件名大小qint64、文件名QString发出去
m_bytesToWrite = m_totalBytes - m_tcpClient->write(m_outBlock);
ui->clientStatusLabel->setText("服务端连接成功");
m_outBlock.resize(0);
}
void Widget::updateClientProgress(qint64 numBytes)
{
m_bytesWritten += (int)numBytes;
if(m_bytesToWrite > 0)
{
// 一次发送64*1024byte内容
m_outBlock = m_localFile->read(qMin(m_bytesToWrite,m_payloadSize));
m_bytesToWrite -= (int)m_tcpClient->write(m_outBlock);
m_outBlock.resize(0);
}
else
{
if(m_localFile->isOpen())
{
m_localFile->close();
}
}
int nPercent = (double)m_bytesWritten*100 / m_totalBytes ;
ui->clientProgressBar->setMaximum(100);
if(m_bytesWritten == m_totalBytes)
{
ui->clientProgressBar->setValue(100);
ui->clientStatusLabel->setText(QString("传送文件 %1 成功").arg(m_fileName));
if(m_localFile->isOpen())
{
m_localFile->close();
}
m_tcpClient->close();
}
else
{
ui->clientProgressBar->setValue(nPercent);
}
}
void Widget::displayError(QAbstractSocket::SocketError)
{
qDebug()<<m_tcpClient->errorString();
m_tcpClient->close();
ui->clientProgressBar->reset();
QString sError = m_tcpClient->errorString();
sError = QString("客户端出错:%1,请检查服务端是否连接正常").arg(sError);
ui->clientStatusLabel->setText(sError);
ui->sendButton->setEnabled(true);
}
void Widget::on_openButton_clicked()
{
ui->clientProgressBar->reset();
ui->clientStatusLabel->setText("等待打开文件!");
openFile();
}
void Widget::on_closeButton_clicked()
{
close();
}
void Widget::on_sendButton_clicked()
{
sendFile();
}
基于Qt实现文件上传功能,包括客户端和服务端源码。
版权申诉
5星 · 超过95%的资源 32 浏览量
2022-06-30
16:52:04
上传
评论 5
收藏 16KB ZIP 举报
欧特克_Glodon
- 粉丝: 4w+
- 资源: 64
最新资源
- 论文(最终)_20240430235101.pdf
- 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页