From 7d2bd6175bac27261e6c5eca1c2e815f7009050c Mon Sep 17 00:00:00 2001 From: JianGuo Date: Fri, 14 Jun 2024 16:41:55 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/xiaoliu/Client.java | 4 +- .../handler/FileSendClientHandler.java | 2 + .../java/com/xiaoliu/window/MainWindow.java | 56 ++++++++++++++----- .../handler/FilePacketServerHandler.java | 2 +- .../handler/FileReceiveServerHandler.java | 16 +++--- 5 files changed, 57 insertions(+), 23 deletions(-) diff --git a/FileTransferClient/src/main/java/com/xiaoliu/Client.java b/FileTransferClient/src/main/java/com/xiaoliu/Client.java index a72dd0e..d254c25 100644 --- a/FileTransferClient/src/main/java/com/xiaoliu/Client.java +++ b/FileTransferClient/src/main/java/com/xiaoliu/Client.java @@ -7,6 +7,7 @@ import com.xiaoliu.handler.FileSendClientHandler; import com.xiaoliu.handler.LoginResponseHandler; import com.xiaoliu.protocol.FilePacket; import com.xiaoliu.protocol.request.LoginPacket; +import com.xiaoliu.window.MainWindow; import io.netty.bootstrap.Bootstrap; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; @@ -73,9 +74,10 @@ public class Client { log.info("连接服务器成功"); Channel channel = future.channel(); joinCluster(channel); - + MainWindow.setStatus(0); } else { log.info("连接服务器失败"); + MainWindow.setStatus(1); } future.channel().closeFuture().sync(); diff --git a/FileTransferClient/src/main/java/com/xiaoliu/handler/FileSendClientHandler.java b/FileTransferClient/src/main/java/com/xiaoliu/handler/FileSendClientHandler.java index 10329bb..4bcfec6 100644 --- a/FileTransferClient/src/main/java/com/xiaoliu/handler/FileSendClientHandler.java +++ b/FileTransferClient/src/main/java/com/xiaoliu/handler/FileSendClientHandler.java @@ -3,6 +3,7 @@ package com.xiaoliu.handler; import com.xiaoliu.codec.Codec; import com.xiaoliu.protocol.FilePacket; import com.xiaoliu.protocol.Packet; +import com.xiaoliu.window.MainWindow; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; @@ -43,6 +44,7 @@ public class FileSendClientHandler extends ChannelInboundHandlerAdapter { ctx.writeAndFlush(fileRegion).addListener(future -> { if (future.isSuccess()) { log.info("{} 发送完成...", file.getName()); + MainWindow.notify("文件发送完成:" + file.getName()); } }); } diff --git a/FileTransferClient/src/main/java/com/xiaoliu/window/MainWindow.java b/FileTransferClient/src/main/java/com/xiaoliu/window/MainWindow.java index 2df1646..6b35aaf 100644 --- a/FileTransferClient/src/main/java/com/xiaoliu/window/MainWindow.java +++ b/FileTransferClient/src/main/java/com/xiaoliu/window/MainWindow.java @@ -35,6 +35,9 @@ public class MainWindow extends JFrame { // 初始化UI组件 initializeComponents(); + + //tuichu + hock(); } private void initStatusLabel() { @@ -43,14 +46,29 @@ public class MainWindow extends JFrame { statusLabel.setText("未连接"); } - public void setStatusLabel(String text, Color color ){ - if(statusLabel == null){ + public void setStatusLabel(String text, Color color) { + if (statusLabel == null) { initStatusLabel(); } statusLabel.setText(text); statusLabel.setForeground(color); } + public static void setStatus(int status) { + if (statusLabel == null) { + return; + } + if (status == 0) { + statusLabel.setText("已连接"); + statusLabel.setForeground(Color.GREEN); + notify("已连接服务器 ..."); + } else { + statusLabel.setText("未连接"); + statusLabel.setForeground(Color.RED); + notify("连接失败 ..."); + } + } + private void initializeComponents() { // 在这里添加你的组件初始化代码 // 创建一个JTextArea用于显示日志 @@ -63,28 +81,26 @@ public class MainWindow extends JFrame { // 将JScrollPane添加到窗口中 getContentPane().add(scrollPane, BorderLayout.CENTER); - JLabel address = new JLabel(); + JLabel address = new JLabel(); address.setText("地址:"); JTextField textField = new JTextField(20); // 参数20指定了文本框的列数 JButton sendButton = new JButton("连接"); JButton stopButton = new JButton("断开"); stopButton.setEnabled(false); - sendButton.addActionListener(e ->{ + sendButton.addActionListener(e -> { String text = textField.getText(); String[] split = text.split(":"); - if(split.length < 2){ + if (split.length < 2) { JOptionPane.showMessageDialog(this, "请输入正确地址", "输入错误", JOptionPane.ERROR_MESSAGE); return; } String host = split[0]; String port = split[1]; - setLogText("连接到: "+ host + ":" + port); + setLogText("连接到: " + host + ":" + port); new Thread(() -> { //新起一个县城去初始化netty try { sendButton.setEnabled(false); - setLogText("连接成功..."); - setStatusLabel("已连接", Color.GREEN); stopButton.setEnabled(true); Client.init(host, Integer.parseInt(port)); } catch (Exception ex) { @@ -110,7 +126,6 @@ public class MainWindow extends JFrame { // 示例:添加一个按钮,点击时向文本区域添加日志信息 JButton startButton = new JButton("发送"); - startButton.addActionListener(e -> { JFileChooser fileChooser = new JFileChooser(); fileChooser.setCurrentDirectory(new java.io.File(System.getProperty("user.home") + "/Desktop")); @@ -127,7 +142,7 @@ public class MainWindow extends JFrame { log.info("文件大小:{}", file.length()); new Thread(() -> { //开一个新县城去泡发送程序 - setLogText("文件发送开始: "+selectedFilePath); + setLogText("文件发送开始: " + selectedFilePath); Client.send(file); }).start(); @@ -139,6 +154,7 @@ public class MainWindow extends JFrame { Client.shutdown(); setStatusLabel("未连接", Color.RED); sendButton.setEnabled(true); + stopButton.setEnabled(false); }); @@ -158,18 +174,25 @@ public class MainWindow extends JFrame { SwingUtilities.invokeLater(() -> new MainWindow().setVisible(true)); } - public JTextArea getLogTextArea(){ - if(logTextArea == null){ + public JTextArea getLogTextArea() { + if (logTextArea == null) { initLogTextArea(); } return logTextArea; } - public void setLogText(String text){ + public static void notify(String text) { + if (logTextArea == null) { + return; + } logTextArea.append(text + "\n"); } - public void initLogTextArea(){ + public void setLogText(String text) { + logTextArea.append(text + "\n"); + } + + public void initLogTextArea() { // 在这里添加你的组件初始化代码 // 创建一个JTextArea用于显示日志 logTextArea = new JTextArea(); @@ -183,4 +206,9 @@ public class MainWindow extends JFrame { getContentPane().add(scrollPane, BorderLayout.CENTER); } + public void hock() { + log.info("退出执行..."); + //优雅退出 + Runtime.getRuntime().addShutdownHook(new Thread(Client::shutdown)); + } } diff --git a/FileTransferService/src/main/java/com/xiaoliu/handler/FilePacketServerHandler.java b/FileTransferService/src/main/java/com/xiaoliu/handler/FilePacketServerHandler.java index 9b0a31a..b511e1a 100644 --- a/FileTransferService/src/main/java/com/xiaoliu/handler/FilePacketServerHandler.java +++ b/FileTransferService/src/main/java/com/xiaoliu/handler/FilePacketServerHandler.java @@ -26,7 +26,7 @@ public class FilePacketServerHandler extends SimpleChannelInboundHandler= fileLength) { - log.info("文件接收完成....."); - outputStream.close(); - } - } +// private void sendComplete(long readLength) throws IOException { +// if (readLength >= fileLength) { +// log.info("文件接收完成....."); +// outputStream.close(); +// } +// } private void sendComplete(LoginPacket loginPacket) throws IOException { + log.debug("ReadLength {}", loginPacket.getReadLength()); + log.debug("FileLength {}", loginPacket.getFileLength()); if (loginPacket.getReadLength() >= loginPacket.getFileLength()) { log.info("文件接收完成..."); if(loginPacket.getFileOutputStream() != null){