commit 6649fd406e116ed43dbde79acc085d906e28414f Author: sonichy Date: Fri Feb 11 13:25:11 2022 +0800 first diff --git a/FileTrans b/FileTrans new file mode 100644 index 0000000..c107988 Binary files /dev/null and b/FileTrans differ diff --git a/FileTrans.png b/FileTrans.png new file mode 100644 index 0000000..a127da1 Binary files /dev/null and b/FileTrans.png differ diff --git a/FileTrans.pro b/FileTrans.pro new file mode 100644 index 0000000..c611b60 --- /dev/null +++ b/FileTrans.pro @@ -0,0 +1,22 @@ +QT += core gui network + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = FileTrans +TEMPLATE = app + +CONFIG += c++11 + +SOURCES += \ + main.cpp \ + mainwindow.cpp + +HEADERS += \ + mainwindow.h + + +FORMS += \ + mainwindow.ui + +RESOURCES += \ + res.qrc \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..46bef7f --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# File Trans +基于 Qt Socket 的文件传输软件。 + +## 界面 +![alt](preview.jpg) + +## 参考 +[Qt文服务器](https://github.com/sonichy/HTYServer) +[Android手机间使用socket进行文件互传实例](https://www.cnblogs.com/zhujiabin/p/7139644.html) \ No newline at end of file diff --git a/icon.htm b/icon.htm new file mode 100644 index 0000000..6bd46cd --- /dev/null +++ b/icon.htm @@ -0,0 +1,40 @@ + + + +LOGO + + + + + + + diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..54b1046 --- /dev/null +++ b/install.sh @@ -0,0 +1,4 @@ +filename=FileTrans +s="[Desktop Entry]\nName=文件传输\nComment=A file transfer base on Qt.\nExec=`pwd`/$filename\nIcon=`pwd`/$filename.png\nPath=`pwd`\nTerminal=false\nType=Application\nCategories=Net;" +echo -e $s > $filename.desktop +cp `pwd`/$filename.desktop ~/.local/share/applications/$filename.desktop diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..9d3d447 --- /dev/null +++ b/main.cpp @@ -0,0 +1,13 @@ +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + qSetMessagePattern("[ %{file}: %{line} ] %{message}"); + QApplication a(argc, argv); + a.setOrganizationName("HTY"); + a.setApplicationName("FileTrans"); + MainWindow w; + w.show(); + return a.exec(); +} \ No newline at end of file diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..915a946 --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,193 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include +#include +#include +#include +#include +#include +#include +#include + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow), + settings(QApplication::organizationName(), QApplication::applicationName()) +{ + ui->setupUi(this); + setFixedSize(400, 500); + ui->lineEdit_port->setValidator(new QIntValidator(1024, 65535, this)); + ui->lineEdit_root_dir->setText(settings.value("Directory", QApplication::applicationDirPath()).toString()); + QAction *action_browser = new QAction; + action_browser->setIcon(QIcon::fromTheme("folder")); + ui->lineEdit_root_dir->addAction(action_browser, QLineEdit::TrailingPosition); + connect(action_browser, &QAction::triggered, [=](){ + QString dir = QFileDialog::getExistingDirectory(this, tr("Root Directory"), + settings.value("Directory", QApplication::applicationDirPath()).toString(), + QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); + if (dir != "") { + ui->lineEdit_root_dir->setText(dir); + settings.setValue("Directory", ui->lineEdit_root_dir->text()); + } + }); + + QList list = QNetworkInterface::allInterfaces(); + foreach (QNetworkInterface interface, list) { + QList entryList = interface.addressEntries(); + foreach (QNetworkAddressEntry entry, entryList) { + if (!entry.ip().toString().contains("::") && entry.ip().toString() != "127.0.0.1") { + IP = entry.ip().toString(); + ui->lineEdit_ip->setText(IP); + } + } + } + + connect(ui->pushButton_start, &QPushButton::toggled, [=](bool b){ + if (b) { + state = 0; + qint64 port = 9999; + while (!tcpServer->listen(QHostAddress::Any, port)) { + port--; + } + ui->label_local->setText(IP + ":" + QString::number(port)); + ui->lineEdit_port->setText(QString::number(port)); + ui->textBrowser->clear(); + ui->textBrowser->append("[" + QDateTime::currentDateTime().toString("HH:mm:ss") + "] 监听:" + IP + ":" + QString::number(port)); + ui->pushButton_start->setText("Stop"); + } else { + tcpServer->close(); + ui->pushButton_start->setText("Start"); + } + }); + + connect(ui->pushButton_add, &QPushButton::clicked, [=]{ + if (path == "") + path = "."; + QStringList SL_path = QFileDialog::getOpenFileNames(this, "上传文件", path); + //qDebug() << SL_path; + for (int i=0; ipushButton_clear, &QPushButton::clicked, [=]{ + ui->textBrowser->clear(); + }); + + tcpServer = new QTcpServer(this); + connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnect())); + ui->pushButton_start->toggle(); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::newConnect() +{ + tcpSocket = tcpServer->nextPendingConnection(); + ui->textBrowser->append("[" + QDateTime::currentDateTime().toString("HH:mm:ss") + "] " + tcpSocket->peerAddress().toString().replace("::ffff:","") + ":" + QString::number(tcpSocket->peerPort())); + connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readyRead())); + + connect(tcpSocket, &QTcpSocket::disconnected, [=](){ + qDebug() << state; + if (state == 0) { + state = 1; + } else { + file.close(); + state = 0; + length = 0; + } + }); + + connect(tcpSocket, QOverload::of(&QAbstractSocket::error), [=](QAbstractSocket::SocketError socketError){ + qDebug() << socketError; + QMetaEnum metaEnum = QMetaEnum::fromType(); + QString errorString = metaEnum.valueToKey(socketError); + ui->textBrowser->append("[" + QDateTime::currentDateTime().toString("HH:mm:ss") + "] " + errorString); + }); +} + +void MainWindow::readyRead() +{ + QByteArray BA = tcpSocket->readAll(); + //qDebug() << BA ; + if (state == 0) { + QString filename = BA; + ui->textBrowser->append("[" + QDateTime::currentDateTime().toString("HH:mm:ss") + "] " + "接收:" + filename); + QString filepath = ui->lineEdit_root_dir->text() + "/" + filename; + file.setFileName(filepath); + file.open(QIODevice::WriteOnly); + } else { + file.write(BA); + length += BA.length(); + ui->statusBar->showMessage("接收:" + BS(length)); + } +} + +QString MainWindow::BS(long b) +{ + QString s = ""; + if (b > 999999999) { + s = QString::number(b/(1024*1024*1024.0), 'f', 2) + " GB"; + } else { + if (b > 999999) { + s = QString::number(b/(1024*1024.0), 'f', 2) + " MB"; + } else { + if (b > 999) { + s = QString::number(b/(1024.0), 'f', 2) + " KB"; + } else { + s = QString::number(b) + " B"; + } + } + } + return s; +} + +void MainWindow::upload(QString filepath) +{ + qint64 port = ui->lineEdit_port->text().toInt(); + QTcpSocket *tcpSocket1 = new QTcpSocket(this); + connect(tcpSocket1, QOverload::of(&QAbstractSocket::error), [=](QAbstractSocket::SocketError socketError){ + qDebug() << socketError; + QMetaEnum metaEnum = QMetaEnum::fromType(); + QString errorString = metaEnum.valueToKey(socketError); + ui->textBrowser->append("[" + QDateTime::currentDateTime().toString("HH:mm:ss") + "] " + errorString); + }); + + tcpSocket1->connectToHost(QHostAddress(ui->lineEdit_ip->text()), port); + connect(tcpSocket1, &QTcpSocket::connected, [=](){ + QString filename = QFileInfo(filepath).fileName(); + tcpSocket1->write(filename.toUtf8()); + tcpSocket1->close(); + ui->textBrowser->append("[" + QDateTime::currentDateTime().toString("HH:mm:ss") + "] 上传:" + filename); + }); + + QTcpSocket *tcpSocket2 = new QTcpSocket(this); + connect(tcpSocket2, QOverload::of(&QAbstractSocket::error), [=](QAbstractSocket::SocketError socketError){ + qDebug() << socketError; + QMetaEnum metaEnum = QMetaEnum::fromType(); + QString errorString = metaEnum.valueToKey(socketError); + ui->textBrowser->append("[" + QDateTime::currentDateTime().toString("HH:mm:ss") + "] " + errorString); + }); + tcpSocket2->connectToHost(QHostAddress(ui->lineEdit_ip->text()), port); + connect(tcpSocket2, &QTcpSocket::connected, [=](){ + qint64 length1 = 0; + QFile file(filepath); + if (file.open(QIODevice::ReadOnly)) { + while (!file.atEnd()) { + QByteArray BA = file.read(10240); //每次读取10k数据 + tcpSocket2->write(BA); + //qDebug() << "\b" << file.pos(); + length1 += 10240; + ui->statusBar->showMessage("上传:" + BS(length1) + "/" + BS(file.size())); + } + ui->textBrowser->append("[" + QDateTime::currentDateTime().toString("HH:mm:ss") + "] " + "上传完成"); + tcpSocket2->close(); + } + }); +} \ No newline at end of file diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..fd1dc14 --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,40 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include +#include +#include + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private: + Ui::MainWindow *ui; + QSettings settings; + QString IP, path; + QTcpServer *tcpServer; + QTcpSocket *tcpSocket; + int state; + QFile file; + qint64 length; + QString BS(long b); + void upload(QString filepath); + +private slots: + void newConnect(); + void readyRead(); + +}; + +#endif // MAINWINDOW_H \ No newline at end of file diff --git a/mainwindow.ui b/mainwindow.ui new file mode 100644 index 0000000..54f80cc --- /dev/null +++ b/mainwindow.ui @@ -0,0 +1,177 @@ + + + MainWindow + + + + 0 + 0 + 400 + 500 + + + + 文件传输 + + + + :/FileTrans.png:/FileTrans.png + + + + + 0 + + + 0 + + + + + + + IP + + + Qt::AlignCenter + + + + + + + Port + + + Qt::AlignCenter + + + + + + + 根目录 + + + Qt::AlignCenter + + + + + + + 本机 + + + Qt::AlignCenter + + + + + + + ip:port + + + + + + + + + + + + + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Start + + + true + + + + + + + + + + + + + + + Clear + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + + + 0 + 0 + 400 + 27 + + + + + + + + lineEdit_ip + lineEdit_port + lineEdit_root_dir + pushButton_start + pushButton_add + pushButton_clear + textBrowser + + + + + + diff --git a/preview.jpg b/preview.jpg new file mode 100644 index 0000000..3a5a972 Binary files /dev/null and b/preview.jpg differ diff --git a/res.qrc b/res.qrc new file mode 100644 index 0000000..7655202 --- /dev/null +++ b/res.qrc @@ -0,0 +1,5 @@ + + + FileTrans.png + +