Compare commits

...

5 Commits

Author SHA1 Message Date
JianGuo 7d2bd6175b 1.优化 2024-06-14 16:41:55 +08:00
JianGuo 9567c1defe 1.修改bug 2024-06-14 15:12:53 +08:00
JianGuo 34699ff89d 1.主题统一 2024-06-14 14:01:08 +08:00
JianGuo 52a99a220a 1.降级到jdk1.8 2024-06-14 13:50:16 +08:00
JianGuo f1eb943f08 1.pom 更新 2024-06-14 13:29:47 +08:00
11 changed files with 95 additions and 38 deletions

View File

@ -15,7 +15,7 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.java.version>21</project.java.version> <project.java.version>1.8</project.java.version>
</properties> </properties>
<dependencies> <dependencies>

View File

@ -7,6 +7,7 @@ import com.xiaoliu.handler.FileSendClientHandler;
import com.xiaoliu.handler.LoginResponseHandler; import com.xiaoliu.handler.LoginResponseHandler;
import com.xiaoliu.protocol.FilePacket; import com.xiaoliu.protocol.FilePacket;
import com.xiaoliu.protocol.request.LoginPacket; import com.xiaoliu.protocol.request.LoginPacket;
import com.xiaoliu.window.MainWindow;
import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*; import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
@ -73,9 +74,10 @@ public class Client {
log.info("连接服务器成功"); log.info("连接服务器成功");
Channel channel = future.channel(); Channel channel = future.channel();
joinCluster(channel); joinCluster(channel);
MainWindow.setStatus(0);
} else { } else {
log.info("连接服务器失败"); log.info("连接服务器失败");
MainWindow.setStatus(1);
} }
future.channel().closeFuture().sync(); future.channel().closeFuture().sync();

View File

@ -1,6 +1,6 @@
package com.xiaoliu; package com.xiaoliu;
import com.formdev.flatlaf.FlatDarculaLaf; import com.formdev.flatlaf.themes.FlatMacLightLaf;
import com.xiaoliu.window.MainWindow; import com.xiaoliu.window.MainWindow;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -12,8 +12,9 @@ public class Main {
public static void main(String[] args ){ public static void main(String[] args ){
log.info("服务启动中..."); log.info("服务启动中...");
try { try {
UIManager.setLookAndFeel(new FlatDarculaLaf()); UIManager.setLookAndFeel(new FlatMacLightLaf());
} catch (UnsupportedLookAndFeelException e) { //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
log.error("UnsupportedLookAndFeelException: {}", e.getMessage()); log.error("UnsupportedLookAndFeelException: {}", e.getMessage());
} }
SwingUtilities.invokeLater(() -> new MainWindow().setVisible(true)); SwingUtilities.invokeLater(() -> new MainWindow().setVisible(true));

View File

@ -3,6 +3,7 @@ package com.xiaoliu.handler;
import com.xiaoliu.codec.Codec; import com.xiaoliu.codec.Codec;
import com.xiaoliu.protocol.FilePacket; import com.xiaoliu.protocol.FilePacket;
import com.xiaoliu.protocol.Packet; import com.xiaoliu.protocol.Packet;
import com.xiaoliu.window.MainWindow;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
@ -43,6 +44,7 @@ public class FileSendClientHandler extends ChannelInboundHandlerAdapter {
ctx.writeAndFlush(fileRegion).addListener(future -> { ctx.writeAndFlush(fileRegion).addListener(future -> {
if (future.isSuccess()) { if (future.isSuccess()) {
log.info("{} 发送完成...", file.getName()); log.info("{} 发送完成...", file.getName());
MainWindow.notify("文件发送完成:" + file.getName());
} }
}); });
} }

View File

@ -35,6 +35,9 @@ public class MainWindow extends JFrame {
// 初始化UI组件 // 初始化UI组件
initializeComponents(); initializeComponents();
//tuichu
hock();
} }
private void initStatusLabel() { private void initStatusLabel() {
@ -43,14 +46,29 @@ public class MainWindow extends JFrame {
statusLabel.setText("未连接"); statusLabel.setText("未连接");
} }
public void setStatusLabel(String text, Color color ){ public void setStatusLabel(String text, Color color) {
if(statusLabel == null){ if (statusLabel == null) {
initStatusLabel(); initStatusLabel();
} }
statusLabel.setText(text); statusLabel.setText(text);
statusLabel.setForeground(color); 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() { private void initializeComponents() {
// 在这里添加你的组件初始化代码 // 在这里添加你的组件初始化代码
// 创建一个JTextArea用于显示日志 // 创建一个JTextArea用于显示日志
@ -63,41 +81,40 @@ public class MainWindow extends JFrame {
// 将JScrollPane添加到窗口中 // 将JScrollPane添加到窗口中
getContentPane().add(scrollPane, BorderLayout.CENTER); getContentPane().add(scrollPane, BorderLayout.CENTER);
JLabel adress = new JLabel(); JLabel address = new JLabel();
adress.setText("地址:"); address.setText("地址:");
JTextField textField = new JTextField(20); // 参数20指定了文本框的列数 JTextField textField = new JTextField(20); // 参数20指定了文本框的列数
JButton sendButton = new JButton("连接"); JButton sendButton = new JButton("连接");
JButton stopButton = new JButton("断开"); JButton stopButton = new JButton("断开");
stopButton.setEnabled(false); stopButton.setEnabled(false);
sendButton.addActionListener(e ->{ sendButton.addActionListener(e -> {
String text = textField.getText(); String text = textField.getText();
String[] split = text.split(":"); String[] split = text.split(":");
if(split.length < 2){ if (split.length < 2) {
JOptionPane.showMessageDialog(this, "请输入正确地址", "输入错误", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(this, "请输入正确地址", "输入错误", JOptionPane.ERROR_MESSAGE);
return; return;
} }
String host = split[0]; String host = split[0];
String port = split[1]; String port = split[1];
setLogText("连接到: "+ host + ":" + port); setLogText("连接到: " + host + ":" + port);
new Thread(() -> { new Thread(() -> {
//新起一个县城去初始化netty //新起一个县城去初始化netty
try { try {
sendButton.setEnabled(false); sendButton.setEnabled(false);
setLogText("连接成功...");
setStatusLabel("已连接", Color.GREEN);
stopButton.setEnabled(true); stopButton.setEnabled(true);
Client.init(host, Integer.parseInt(port)); Client.init(host, Integer.parseInt(port));
} catch (Exception ex) { } catch (Exception ex) {
setLogText("连接失败..."); sendButton.setEnabled(true);
stopButton.setEnabled(false); stopButton.setEnabled(false);
setStatusLabel("已连接", Color.RED); setStatusLabel("未连接", Color.RED);
JOptionPane.showMessageDialog(this, "连接失败,请检查...", "连接", JOptionPane.ERROR_MESSAGE);
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
}).start(); }).start();
}); });
JPanel northPanel = new JPanel(); JPanel northPanel = new JPanel();
northPanel.add(adress); northPanel.add(address);
northPanel.add(textField); northPanel.add(textField);
northPanel.add(sendButton); northPanel.add(sendButton);
northPanel.add(statusLabel, BorderLayout.WEST); northPanel.add(statusLabel, BorderLayout.WEST);
@ -109,7 +126,6 @@ public class MainWindow extends JFrame {
// 示例添加一个按钮点击时向文本区域添加日志信息 // 示例添加一个按钮点击时向文本区域添加日志信息
JButton startButton = new JButton("发送"); JButton startButton = new JButton("发送");
startButton.addActionListener(e -> { startButton.addActionListener(e -> {
JFileChooser fileChooser = new JFileChooser(); JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new java.io.File(System.getProperty("user.home") + "/Desktop")); fileChooser.setCurrentDirectory(new java.io.File(System.getProperty("user.home") + "/Desktop"));
@ -126,7 +142,7 @@ public class MainWindow extends JFrame {
log.info("文件大小:{}", file.length()); log.info("文件大小:{}", file.length());
new Thread(() -> { new Thread(() -> {
//开一个新县城去泡发送程序 //开一个新县城去泡发送程序
setLogText("文件发送开始: "+selectedFilePath); setLogText("文件发送开始: " + selectedFilePath);
Client.send(file); Client.send(file);
}).start(); }).start();
@ -138,6 +154,7 @@ public class MainWindow extends JFrame {
Client.shutdown(); Client.shutdown();
setStatusLabel("未连接", Color.RED); setStatusLabel("未连接", Color.RED);
sendButton.setEnabled(true); sendButton.setEnabled(true);
stopButton.setEnabled(false);
}); });
@ -157,18 +174,25 @@ public class MainWindow extends JFrame {
SwingUtilities.invokeLater(() -> new MainWindow().setVisible(true)); SwingUtilities.invokeLater(() -> new MainWindow().setVisible(true));
} }
public JTextArea getLogTextArea(){ public JTextArea getLogTextArea() {
if(logTextArea == null){ if (logTextArea == null) {
initLogTextArea(); initLogTextArea();
} }
return logTextArea; return logTextArea;
} }
public void setLogText(String text){ public static void notify(String text) {
if (logTextArea == null) {
return;
}
logTextArea.append(text + "\n"); logTextArea.append(text + "\n");
} }
public void initLogTextArea(){ public void setLogText(String text) {
logTextArea.append(text + "\n");
}
public void initLogTextArea() {
// 在这里添加你的组件初始化代码 // 在这里添加你的组件初始化代码
// 创建一个JTextArea用于显示日志 // 创建一个JTextArea用于显示日志
logTextArea = new JTextArea(); logTextArea = new JTextArea();
@ -182,4 +206,9 @@ public class MainWindow extends JFrame {
getContentPane().add(scrollPane, BorderLayout.CENTER); getContentPane().add(scrollPane, BorderLayout.CENTER);
} }
public void hock() {
log.info("退出执行...");
//优雅退出
Runtime.getRuntime().addShutdownHook(new Thread(Client::shutdown));
}
} }

View File

@ -15,7 +15,7 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.java.version>21</project.java.version> <project.java.version>1.8</project.java.version>
</properties> </properties>
<dependencies> <dependencies>

View File

@ -10,6 +10,8 @@ public class FilePacket extends Packet {
File file; File file;
long fileLength;
int ACK; int ACK;
@Override @Override
@ -22,10 +24,12 @@ public class FilePacket extends Packet {
public FilePacket(File file) { public FilePacket(File file) {
this.file = file; this.file = file;
this.fileLength = file.length();
} }
public FilePacket(File file, int ACK) { public FilePacket(File file, int ACK) {
this.file = file; this.file = file;
this.fileLength = file.length();
this.ACK = ACK; this.ACK = ACK;
} }
@ -44,4 +48,12 @@ public class FilePacket extends Packet {
public void setACK(int ACK) { public void setACK(int ACK) {
this.ACK = ACK; this.ACK = ACK;
} }
public long getFileLength() {
return fileLength;
}
public void setFileLength(long fileLength) {
this.fileLength = fileLength;
}
} }

View File

@ -27,6 +27,14 @@ public class LoginPacket extends Packet {
boolean exec = false; boolean exec = false;
public void flush() {
this.exec = false;
this.fileName = null;
this.fileLength = 0;
this.readLength = 0;
this.fileOutputStream = null;
}
@Override @Override
public Byte getCommand() { public Byte getCommand() {
return LOGIN_PACKET_REQUEST; return LOGIN_PACKET_REQUEST;

View File

@ -15,7 +15,7 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.java.version>21</project.java.version> <project.java.version>1.8</project.java.version>
</properties> </properties>
<dependencies> <dependencies>
@ -27,7 +27,7 @@
</dependencies> </dependencies>
<build> <build>
<finalName>sane-service-v${project.version}</finalName> <finalName>NettyService-v${project.version}</finalName>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>

View File

@ -25,8 +25,8 @@ public class FilePacketServerHandler extends SimpleChannelInboundHandler<FilePac
} }
loginPacket.setExec(true); loginPacket.setExec(true);
loginPacket.setFileName(file.getName()); loginPacket.setFileName(file.getName());
loginPacket.setFileLength(file.length()); loginPacket.setFileLength(packet.getFileLength());
loginPacket.setFileOutputStream(new FileOutputStream(new File("./server-receive-" + file.getName()))); loginPacket.setFileOutputStream(new FileOutputStream(new File("./service-receive-" + file.getName())));
// FileReceiveServerHandler.fileLength = file.length(); // FileReceiveServerHandler.fileLength = file.length();
// FileReceiveServerHandler.outputStream = new FileOutputStream( // FileReceiveServerHandler.outputStream = new FileOutputStream(
// new File("./server-receive-" + file.getName()) // new File("./server-receive-" + file.getName())

View File

@ -27,7 +27,7 @@ public class FileReceiveServerHandler extends ChannelInboundHandlerAdapter {
ByteBuf byteBuf = (ByteBuf) msg; ByteBuf byteBuf = (ByteBuf) msg;
int type = byteBuf.getInt(0); int type = byteBuf.getInt(0);
if (type != Codec.TYPE) { if (type != Codec.TYPE) {
loginPacket.setReadLength(loginPacket.getReadLength()+byteBuf.readableBytes()); loginPacket.setReadLength(loginPacket.getReadLength() + byteBuf.readableBytes());
//readLength += byteBuf.readableBytes(); //readLength += byteBuf.readableBytes();
writeToFile(byteBuf, loginPacket.getFileOutputStream()); writeToFile(byteBuf, loginPacket.getFileOutputStream());
sendComplete(loginPacket); sendComplete(loginPacket);
@ -43,19 +43,22 @@ public class FileReceiveServerHandler extends ChannelInboundHandlerAdapter {
byteBuf.release(); byteBuf.release();
} }
private void sendComplete(long readLength) throws IOException { // private void sendComplete(long readLength) throws IOException {
if (readLength >= fileLength) { // if (readLength >= fileLength) {
log.info("文件接收完成....."); // log.info("文件接收完成.....");
outputStream.close(); // outputStream.close();
} // }
} // }
private void sendComplete(LoginPacket loginPacket) throws IOException { private void sendComplete(LoginPacket loginPacket) throws IOException {
log.debug("ReadLength {}", loginPacket.getReadLength());
log.debug("FileLength {}", loginPacket.getFileLength());
if (loginPacket.getReadLength() >= loginPacket.getFileLength()) { if (loginPacket.getReadLength() >= loginPacket.getFileLength()) {
log.info("文件接收完成..."); log.info("文件接收完成...");
loginPacket.setExec(false); if(loginPacket.getFileOutputStream() != null){
loginPacket.setReadLength(0); loginPacket.getFileOutputStream().close();
loginPacket.getFileOutputStream().close(); }
loginPacket.flush();
} }
} }