package SanWa.UI;
import SanWa.Net.MsgTypeConfig;
import SanWa.Net.User;
import SanWa.Util.FileUtils;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Objects;
public class WenJianconpoent extends Box {
public static int i;
public static JTextArea infortextArea;
public static JTextArea infortextArea1;
public static JTextArea infortextArea21;
public static JTextArea infortextArea22;
public static JButton checkButton;
public static JButton checkButtonxx;
private JPanel c2;
private JPanel c21;
private JPanel c22;
public WenJianconpoent() {
//垂直布局
super(BoxLayout.Y_AXIS);
Font fon = new Font("黑体", 1, 12);
this.setBackground(new Color(208,208,208));
JLabel d1Label = new JLabel("文件发送界面界面");//组装视图
d1Label.setBackground(new Color(50,50,50));
d1Label.setForeground(Color.white);
JPanel btnPanel = new JPanel();
Color color = new Color(208,208,208);
d1Label.setFont(new java.awt.Font("黑体", 1, 25));
d1Label.setBounds(500, 500, 1500, 600);
btnPanel.setBackground(new Color(50,50,50));
btnPanel.setMaximumSize(new Dimension(2000, 40));
btnPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
btnPanel.add(d1Label);
this.add(btnPanel);
Box u3Box = Box.createHorizontalBox();
Box u4Box = Box.createHorizontalBox();
JScrollPane scrollPane_8 = new JScrollPane();
JScrollPane scrollPane_9 = new JScrollPane();
JScrollPane scrollPane_7 = new JScrollPane();
JScrollPane scrollPane_6 = new JScrollPane();
JLabel pLabel1 = new JLabel("请在此输入发送文件路径:");
pLabel1.setFont(new Font("黑体", 1, 16));
pLabel1.setPreferredSize(new Dimension(200, 30));
c21 = new JPanel();
c21.setLayout(new FlowLayout(FlowLayout.LEFT));
c21.setBackground(new Color(208,208,208));
c21.setPreferredSize(new Dimension(600, 30));
JPanel btn1Panel1 = new JPanel();
Color color1 = new Color(208,208,208);
btn1Panel1.setBackground(color1);
btn1Panel1.add(pLabel1);
JPanel btn11Panel11 = new JPanel();
Color color11 = new Color(208,208,208);
infortextArea21 = new JTextArea();
infortextArea21.setFont(new Font("黑体", 1, 18));
infortextArea21.setPreferredSize(new Dimension(400, 29));
infortextArea21.setBackground(new Color(208,208,208));
infortextArea21.append("");
btn11Panel11.setLayout(new FlowLayout(FlowLayout.RIGHT));
btn11Panel11.setBackground(color11);
btn11Panel11.add(infortextArea21);
scrollPane_8.setViewportView(infortextArea21);
c21.add(btn1Panel1);
c21.add(scrollPane_8);
u3Box.add(c21);
scrollPane_7.setViewportView(u3Box);
this.add(scrollPane_7);
JLabel pLabel2 = new JLabel("请在此输入文件的接收者:");
pLabel2.setFont(new Font("黑体", 1, 16));
pLabel2.setPreferredSize(new Dimension(200, 30));
c22 = new JPanel();
c22.setLayout(new FlowLayout(FlowLayout.LEFT));
c22.setBackground(new Color(208,208,208));
c22.setPreferredSize(new Dimension(600, 30));
JPanel btn1Panel2 = new JPanel();
btn1Panel2.setBackground(color1);
btn1Panel2.add(pLabel2);
JPanel btn11Panel22 = new JPanel();
infortextArea22 = new JTextArea();
infortextArea22.setFont(new Font("黑体", 1, 18));
infortextArea22.setPreferredSize(new Dimension(400, 29));
infortextArea22.setBackground(new Color(208,208,208));
infortextArea22.append("");
JButton checkButton2 = new JButton("发送文件");
checkButton2.setPreferredSize(new Dimension(85, 39));
checkButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String receiver = infortextArea22.getText();
String path = infortextArea21.getText();
if (!Objects.equals(path, "")) {
if (!Objects.equals(receiver, "")) {
File file = new File(path);
if (file.exists()) {
byte[] bytes = FileUtils.readFile(path);
User user = new User(receiver, MainView.ss, MsgTypeConfig.WENJIAN, System.currentTimeMillis(), file.getName(), file.length(), bytes);
try {
MainView.out.writeObject(user);
infortextArea1.append("您正在给"+receiver+"用户发送文件!等待对方接受或拒绝!"+"\n");
} catch (IOException ioException) {
ioException.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "文件找不到!请重新输入路径名!");
}
} else {
JOptionPane.showMessageDialog(null, "接收者为空!请输入接收者!");
}
} else {
JOptionPane.showMessageDialog(null, "路径为空!请输入路径名!");
}
}
});
btn11Panel22.setLayout(new FlowLayout(FlowLayout.RIGHT));
btn11Panel22.setBackground(color11);
btn11Panel22.add(infortextArea22);
scrollPane_9.setViewportView(infortextArea22);
c22.add(btn1Panel2);
c22.add(scrollPane_9);
c22.add(checkButton2);
u4Box.add(c22);
scrollPane_6.setViewportView(u4Box);
this.add(scrollPane_6);
Box u1Box = Box.createHorizontalBox();
u1Box.setPreferredSize(new Dimension(850, 190));
JLabel uLabel = new JLabel(" ");
infortextArea1 = new JTextArea();
infortextArea1.setEditable(false);
infortextArea1.setFont(new Font("黑体", 1, 18));
infortextArea1.setBounds(0, 0, 850, 200);
infortextArea1.setBackground(new Color(208,208,208));
JScrollPane scrollPane21 = new JScrollPane();
scrollPane21.setBounds(0, 0, 850, 300);
scrollPane21.setViewportView(infortextArea1);
u1Box.add(scrollPane21);
this.add(u1Box);
Box u5Box = Box.createHorizontalBox();
JScrollPane scrollPane_5 = new JScrollPane();
JLabel pLabel = new JLabel("请在此选择是否接受文件!");
pLabel.setFont(new Font("黑体", 1, 16));
pLabel.setPreferredSize(new Dimension(210, 30));
c2 = new JPanel();
c2.setLayout(new FlowLayout(FlowLayout.LEFT));
c2.setBackground(new Color(208,208,208));
c2.setPreferredSize(new Dimension(600, 30));
JPanel btn1Panel = new JPanel();
btn1Panel.setBackground(color1);
btn1Panel.add(pLabel);
checkButton = new JButton("接受文件");
checkButtonxx = new JButton("拒绝文件");
checkButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ClientReadMsgThread.isSave = false;
}
});
checkButtonxx.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ClientReadMsgThread.isClose = true;
}
});
c2.add(btn1Panel);
c2.add(checkButton);
c2.add(checkButtonxx);
u5Box.add(c2);
scrollPane_5.setViewportView(u5Box);
this.add(scrollPane_5);
Box uBox = Box.createHorizontalBox();
uBox.setPreferredSize(new Dimension(850, 190));
infortextArea = new JTextArea();
infortextArea.setFont(new Font("黑体", 1, 18));
infortextArea.setEditable(false);
infortextArea.setBackground(new Color(208,208,208));
JScrollPane scrollPane_1 = new JScrollPane();
uBox.add(scrollPane_1);
scrollPane_1.setViewportView(infortextArea);
this.add(uBox);
JPanel btnPanel1 = new JPanel();
Color color12 = new Color(208,208,208);
btnPanel1.setBackground(color12);
btnPanel1.setMaximumSize(new Dimension(1500, 40));
btnPanel1.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton getButton1 = new JButton("打开文件夹");
getButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
File file = new File("E:\\WenJian");
if (!file.exists()) {
file.mkdirs();
}
try {
Desktop.getDesktop().open(file);
} catch (IOException e12) {
e12.printStackTrace();
}
}
});
btnPanel1.add(getButton1);
this.add(btnPanel1);
}
}
package SanWa.UI;
import SanWa.Net.MsgTypeConfig;
import SanWa.Net.User;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class YinShenCompoent extends Box {
public static JTextArea infortextArea1;
public static ObjectOutputStream send= MainView.out;
public YinShenCompoent() {
//垂直布局
super(BoxLayout.Y_AXIS);
//组装视图
Box u1Box = Box.createHorizontalBox();
u1Box.setPreferredSize(new Dimension(850, 505));
infortextArea1 = new JTextArea();
infortextArea1.setEditable(false);
infortextArea1.setBounds(0, 0, 850, 200);
infortextArea1.setBackground(new Color(208,208,208));
JScrollPane scrollPane21 = new JScrollPane();
scrollPane21.setBounds(0, 0, 850, 300);
scrollPane21.setViewportView(infortextArea1);
u1Box.add(scrollPane21);
JPanel btnPanel = new JPanel();
Color color = new Color(208,208,208);
btnPanel.setBackground(new Color(50,50,50));
btnPanel.setMaximumSize(new Dimension(1500, 80));
btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
JLabel d11Label = new JLabel(" 当前在线成员列表 ");
d11Label.setBackground(new Color(50,50,50));
d11Label.setForeground(Color.white);
d11Label.setFont(new java.awt.Font("楷体", 1, 35));
JButton getButton = new JButton("获取");
getButton.setFont(new java.awt.Font("楷体", 1, 20));
getButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
User us = new User(MainView.ss,null,null, MsgTypeConfig.ONLINE_LIST1,System.currentTimeMillis());
try {
send.writeObject(us);
} catch (IOException ioException) {
ioException.printStackTrace();
}
infortextArea1.setText("");
}
});
JButton getButton1 = new JButton("刷新");
getButton1.setFont(new java.awt.Font("楷体", 1, 20));
getButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
User us = new User(MainView.ss,null,null, MsgTypeConfig.ONLINE_LIST1,System.currentTimeMillis());
try {
send.writeObject(us);
} catch (IOException ioException) {
ioException.printStackTrace();
}
infortextArea1.setText("");
}
});
btnPanel.add(d11Label);
btnPanel.add(getButton);
btnPanel.add(getButton1);
this.add(btnPanel);
Box u5Box = Box.createHorizontalBox();
u5Box.setPreferredSize(new Dimension(650,15));
JScrollPane scrollPane_5 = new JScrollPane();
scrollPane_5.setPreferredSize(new Dimension(650,10));
JPanel btn1Panel = new JPanel();
// btn1Panel.setPreferredSize(new Dimension(650, 30));
btn1Panel.setBackground(new Color(208,208,208));
JLabel d1Label = new JLabel("【编号】 ");
JLabel d2Label = new JLabel("【姓名】 ");
JLabel d3Label = new JLabel("【IP 地 址】 ");
JLabel d4Label = new JLabel("【端口号】 ");//组装视图
JLabel d5Label = new JLabel("【本地端口号】 ");//组装视图
JLabel d6Label = new JLabel("【在线状态】");//组装视图
Color color1 = new Color(208,208,208);
d1Label.setFont(new java.awt.Font("楷体", 1, 17));
d2Label.setFont(new java.awt.Font("楷体", 1, 17));
d3Label.setFont(new java.awt.Font("楷体", 1, 17));
d4Label.setFont(new java.awt.Font("楷体", 1, 17));
d5Label.setFont(new java.awt.Font("楷体", 1, 17));
d6Label.setFont(new java.awt.Font("楷体", 1, 17));
btn1Panel.setBackground(color1);
btn1Panel.setMaximumSize(new Dimension(650, 35));
btn1Panel.add(d1Label);
btn1Panel.add(d2Label);
btn1Panel.add(d3Label);
btn1Panel.add(d4Label);
btn1Panel.add(d5Label);
btn1Panel.add(d6Label);
scrollPane_5.setViewportView(btn1Panel);u5Box.add(scrollPane_5);
this.add(u5Box);
this.add(u1Box);
JPanel huan = new JPanel();
Color ys = new Color(208,208,208);
huan.setBackground(ys);
huan.setMaximumSize(new Dimension(1500, 80));
huan.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton yinshen = new JButton("隐身");
yinshen.setFont(new java.awt.Font("楷体", 1, 20));
yinshen.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
User us = new User(MainView.ss,null,null, MsgTypeConfig.ONLINE_MSG,System.currentTimeMillis());
try {
send.writeObject(us);
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}); JButton zaixian = new JButton("上线");
zaixian.setFont(new java.awt.Font("楷体", 1, 20));
zaixian.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
User us = new User(MainView.ss,null,null, MsgTypeConfig.ONLINE_MSG,System.currentTimeMillis());
try {
send.writeObject(us);
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
});
huan.add(yinshen);
huan.add(zaixian);
this.add(huan);
}
}
package SanWa.Util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FileUtils {
public static byte[] readFile(String path) {
File file = new File(path);
byte datas[] = null;
if (!file.exists()) {
datas = null;
} else {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(file);
byte data[] = new byte[1024 * 1024];
int len = 0;
while ((len = fis.read(data)) > 0) {
baos.write(data, 0, len);
}
datas = baos.toByteArray();
baos.flush();
baos.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return datas;
}
public static boolean writeFile(String path, byte datas[]) {
try {
File file = new File(path);
FileOutputStream fos = new FileOutputStream(file);
fos.write(datas);
fos.flush();
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
package SanWa.Util;
public class PathUtils {
//获取路径
private static final String IMG_PATH="ChatRoom\\Image\\";
public static String getRealPath(String relativePath) {
return IMG_PATH + relativePath;
}
}
package SanWa.Util;
import java.awt.*;
public class ScreenUtils {
//获取电脑屏幕的宽度
public static int getScreenWidth() {
return Toolkit.getDefaultToolkit().getScreenSize().width;
}
//获取电脑屏幕的高度
public static int getScreenHeight() {
return Toolkit.getDefaultToolkit().getScreenSize().height;
}
}
package SanWa.Net;
import java.io.Serializable;
import java.net.Socket;
import java.util.HashMap;
public class User implements Serializable {
static final long serialVersionUID = 456789L;
//String zfMsg = username + ":" + content + ":" + msgType + ":" + System.currentTimeMillis();
private String username;
private String userSend;
private String content;
private MsgTypeConfig msgType;
private long time;
private HashMap<String, Socket> hm;
private String fileName;
private long fileLength;
private byte fileData[];
public User(String username, String userSend, MsgTypeConfig msgType, long time, String fileName, long fileLength, byte[] fileData) {
this.username = username;
this.userSend = userSend;
this.msgType = msgType;
this.time = time;
this.fileName = fileName;
this.fileLength = fileLength;
this.fileData = fileData;
}
public User(String username, String content, MsgTypeConfig msgType, long time, HashMap<String, Socket> hm) {
this.username = username;
this.content = content;
this.msgType = msgType;
this.time = time;
this.hm = hm;
}
public User(String username, String content, MsgTypeConfig msgType, long time) {
this.username = username;
this.content = content;
this.msgType = msgType;
this.time = time;
}
public User(String userSend, String username, String content, MsgTypeConfig msgType, long time) {
this.userSend = userSend;
this.username = username;
this.content = content;
this.msgType = msgType;
this.time = time;
}
public User() {
}
public String getFileName() {
return fileName;
}
public long getFileLength() {
return fileLength;
}
public byte[] getFileData() {
return fileData;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", userSend='" + userSend + '\'' +
", content='" + content + '\'' +
", msgType=" + msgType +
", time=" + time +
", hm=" + hm +
'}';
}
public String getUserSend() {
return userSend;
}
public HashMap<String, Socket> getHm() {
return hm;
}
public MsgTypeConfig getMsgType() {
return msgType;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
}
package SanWa.UI;
import javax.swing.*;
import java.awt.*;
public class BackGroundPanel extends JPanel {
//声明图片
private Image backIcon;
public BackGroundPanel(Image backIcon) {
this.backIcon = backIcon;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
//绘制背景
g.drawImage(backIcon,0,0,this.getWidth(),this.getHeight(),null);
}
}
package SanWa.UI;
import SanWa.Net.User;
import java.io.*;
import java.net.Socket;
import java.util.HashMap;
public class RegisterUserThread extends Thread {
public static ObjectInputStream in;
public static ObjectOutputStream out;
public static String username;
private Socket sk;
private HashMap<String, Socket> hm;
public RegisterUserThread(Socket sk, HashMap<String, Socket> hm) {
this.sk = sk;
this.hm = hm;
}
@Override
public void run() {
try {
in = new ObjectInputStream(sk.getInputStream());
out = new ObjectOutputStream(sk.getOutputStream());
while (true) {
User user = null;
try {
user = (User) in.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
username = user.getUsername();
if (!hm.containsKey(user.getUsername())) {
hm.put(user.getUsername(), sk);
String s = ": " + user.getUsername() + " " + sk.getInetAddress() + " " + sk.getPort() + " " +
sk.getLocalPort() + " 在线】";
Server.info.add(s);
out.writeObject(new User("通过验证", null, null, 0));
break;
} else {
//给客户端反馈
out.writeObject(new User("拒绝", null, null, 0));
}
}
new ServerReadMsgThread(sk, hm).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package SanWa.UI;
import SanWa.Util.PathUtils;
import SanWa.Util.ScreenUtils;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
public class Server {
public static JTextArea infortextArea1;
public static Socket sk;
public static ArrayList<String> info = new ArrayList<>();
final int WIDTH = 600;
final int HEIGHT = 600;
JFrame jf = new JFrame("弎屲服务端");
JTextField d2Field;
//程序入口
public static void main(String[] args) {
new Server().init();
}
//组装视图
public void init() {
//设置窗口的相关属性
jf.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2, (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);
jf.setVisible(false);
try {
jf.setIconImage(ImageIO.read(new File(PathUtils.getRealPath("04.JPG"))));
} catch (IOException e) {
e.printStackTrace();
}
//设置窗口内容
BackGroundPanel bgPanel = null;
try {
bgPanel = new BackGroundPanel(ImageIO.read(new File(PathUtils.getRealPath("x002.jpg"))));
} catch (IOException e) {
e.printStackTrace();
}
//组装登录相关的元素
Box vBox = Box.createVerticalBox();
Box dkBox = Box.createHorizontalBox();
JLabel d1Label = new JLabel("请 输 入 您 的 端 口 号:");
//d2Field = new JTextField("请输入您的端口号!", 23);
d2Field = new JTextField("8888", 23);
d2Field.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
if (d2Field.getText().equals("请输入您的端口号!")) {
d2Field.setText("");
}
}
@Override
public void focusLost(FocusEvent e) {
if (d2Field.getText().equals("")) {
d2Field.setText("请输入您的端口号!");
}
}
});
dkBox.add(d1Label);
dkBox.add(Box.createHorizontalStrut(25));
dkBox.add(d2Field);
Box u1Box = Box.createHorizontalBox();
u1Box.setPreferredSize(new Dimension(250, 350));
infortextArea1 = new JTextArea();
infortextArea1.setBackground(new Color(217, 217, 217));
JScrollPane scrollPane21 = new JScrollPane();
scrollPane21.setViewportView(infortextArea1);
u1Box.add(scrollPane21);
//组装按钮
Box btnBox = Box.createHorizontalBox();
JButton loginBtn = new JButton("启动服务器");
loginBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
HashMap<String, Socket> hm = new HashMap<>();
int dk = Integer.parseInt(d2Field.getText().trim());
ServerSocket ss = null;
try {
ss = new ServerSocket(dk);
} catch (IOException ioException) {
ioException.printStackTrace();
}
infortextArea1.append("服务器已经开启,等待连接。。。"+"\n");
JOptionPane.showMessageDialog(null, "服务器已开启!");
//jf.dispose();
int i = 1;
while (true) {
try {
sk = ss.accept();
} catch (IOException ioException) {
ioException.printStackTrace();
}
infortextArea1.append((i++) + " 个客户端已经连接"+"\n");
new RegisterUserThread(sk, hm).start();
}
}
});
JButton clcBtn = new JButton("关闭服务器");
clcBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
//退出按钮
Box closeBox = Box.createHorizontalBox();
JButton coBtn = new JButton("退出");
coBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
closeBox.add(coBtn);
JButton coBtn1 = new JButton("重置");
coBtn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
d2Field.setText("请输入您的端口号!");
}
});
btnBox.add(loginBtn);
btnBox.add(Box.createHorizontalStrut(100));
btnBox.add(coBtn1);
btnBox.add(Box.createHorizontalStrut(100));
btnBox.add(clcBtn);
vBox.add(Box.createVerticalStrut(35));
vBox.add(dkBox);
vBox.add(Box.createVerticalStrut(20));
vBox.add(btnBox);
vBox.add(Box.createVerticalStrut(20));
vBox.add(u1Box);
vBox.add(Box.createVerticalStrut(30));
vBox.add(closeBox);
bgPanel.add(vBox);
jf.add(bgPanel);
jf.setVisible(true);
}
}
/*
package SanWa.UI;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
public class Server {
public static HashMap<String, Socket> hm=new HashMap<>();
public static Socket sk;
public static ArrayList<String> info=new ArrayList<>();
public static void main(String[] args) throws IOException {
HashMap<String, Socket> hm = new HashMap<>();
ServerSocket ss = new ServerSocket(8888);
System.out.println("服务器已经开启,等待连接。。。");
int i = 1;
while (true) {
sk = ss.accept();
System.out.println((i++) + " 个客户端已经连接");
new RegisterUserThread(sk,hm).start();
}
}
}
*/
package SanWa.UI;
import SanWa.Net.MsgTypeConfig;
import SanWa.Net.User;
import java.io.*;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Set;
public class ServerReadMsgThread extends Thread {
public static User receiver = null;
private Socket sk;
private HashMap<String, Socket> hm;
public ServerReadMsgThread(Socket sk, HashMap<String, Socket> hm) {
this.sk = sk;
this.hm = hm;
}
@Override
public void run() {
try {
ObjectInputStream oin = RegisterUserThread.in;
while (true) {
try {
receiver = (User) oin.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (receiver.getMsgType() == (MsgTypeConfig.PRIVATE_MSG)) {
User sendUser = new User(receiver.getUsername(), receiver.getUserSend(), receiver.getContent(), receiver.getMsgType(), receiver.getTime());
ObjectOutputStream sendStream = new ObjectOutputStream(hm.get(receiver.getUsername()).getOutputStream());
sendStream.writeObject(sendUser);
} else if (receiver.getMsgType() == MsgTypeConfig.PUBLIC_MSG) {
Set<String> keySet = hm.keySet();
for (String key : keySet) {
if (key.equals(receiver.getUsername())) {
continue;
}
User sendUser = new User(receiver.getUsername(), receiver.getContent(), receiver.getMsgType(), receiver.getTime());
ObjectOutputStream sendStream = new ObjectOutputStream(hm.get(key).getOutputStream());
sendStream.writeObject(sendUser);
}
} else if (receiver.getMsgType() == MsgTypeConfig.SHANGXIAN) {
Set<String> keySet1 = hm.keySet();
ObjectOutputStream sendStream;
String timeStr = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(receiver.getTime());
for (String key : keySet1) {
if (key.equals(RegisterUserThread.username)) {
String Msg = "【上线信息】:【用户:" + RegisterUserThread.username + ":" + "上线了】【" + timeStr + "】【我】";
User sendUser = new User(RegisterUserThread.username, null, Msg, MsgTypeConfig.SHANGXIAN, System.currentTimeMillis());
sendStream = new ObjectOutputStream(hm.get(key).getOutputStream());
sendStream.writeObject(sendUser);
} else {
String Msg = "【上线信息】:【用户:" + RegisterUserThread.username + ":" + "上线了】【" + timeStr + "】";
User sendUser = new User(RegisterUserThread.username, null, Msg, MsgTypeConfig.SHANGXIAN, System.currentTimeMillis());
sendStream = new ObjectOutputStream(hm.get(key).getOutputStream());
sendStream.writeObject(sendUser);
}
}
} else if (receiver.getMsgType() == MsgTypeConfig.ONLINE_LIST) {
String ss = receiver.getUserSend();
String xi = "";
int i = 1;
for (String s : Server.info) {
if (s.equals(": " + ss + " " + hm.get(ss).getInetAddress() + " " + hm.get(ss).getPort() + " " +
hm.get(ss).getLocalPort() + " 在线】")) {
xi = xi + +i + s + "【我】" + "\n";
}
if (!(s.equals(": " + ss + " " + hm.get(ss).getInetAddress() + " " + hm.get(ss).getPort() + " " +
hm.get(ss).getLocalPort() + " 在线】"))) {
xi = xi + i + s + "\n";
}
i++;
}
User sendUser = new User(receiver.getUsername(), xi, receiver.getMsgType(), receiver.getTime());
ObjectOutputStream sendStream = new ObjectOutputStream(hm.get(receiver.getUserSend()).getOutputStream());
sendStream.writeObject(sendUser);
} else if (receiver.getMsgType() == MsgTypeConfig.ONLINE_LIST1) {
String ss = receiver.getUserSend();
String xi = "";
int i = 1;
for (String s : Server.info) {
if (s.equals(": " + ss + " " + hm.get(ss).getInetAddress() + " " + hm.get(ss).getPort() + " " +
hm.get(ss).getLocalPort() + " 在线】")) {
xi = xi + +i + s + "【我】" + "\n";
}
if (!(s.equals(": " + ss + " " + hm.get(ss).getInetAddress() + " " + hm.get(ss).getPort() + " " +
hm.get(ss).getLocalPort() + " 在线】"))) {
xi = xi + i + s + "\n";
}
i++;
}
User sendUser = new User(receiver.getUsername(), xi, receiver.getMsgType(), receiver.getTime());
ObjectOutputStream sendStream = new ObjectOutputStream(hm.get(receiver.getUserSend()).getOutputStream());
sendStream.writeObject(sendUser);
} else if (receiver.getMsgType() == MsgTypeConfig.ONLINE_MSG) {
String ss = receiver.getUserSend();
String mu = ": " + ss + " " + hm.get(ss).getInetAddress() + " " + hm.get(ss).getPort() + " " +
hm.get(ss).getLocalPort() + " 在线】";
if (Server.info.contains(mu)) {
Server.info.remove(mu);
User sendUser = new User(receiver.getUserSend(), "您已成功隐身!", receiver.getMsgType(), receiver.getTime());
ObjectOutputStream sendStream = new ObjectOutputStream(hm.get(receiver.getUserSend()).getOutputStream());
sendStream.writeObject(sendUser);
} else {
Server.info.add(mu);
User sendUser = new User(receiver.getUserSend(), "您已成功上线!", receiver.getMsgType(), receiver.getTime());
ObjectOutputStream sendStream = new ObjectOutputStream(hm.get(receiver.getUserSend()).getOutputStream());
sendStream.writeObject(sendUser);
}
} else if (receiver.getMsgType() == MsgTypeConfig.WENJIAN) {
ObjectOutputStream sendStream = new ObjectOutputStream(hm.get(receiver.getUsername()).getOutputStream());
sendStream.writeObject(receiver);
} else if (receiver.getMsgType() == MsgTypeConfig.FANKUI) {
if (receiver.getContent().equals("yes")) {
ObjectOutputStream sendStream = new ObjectOutputStream(hm.get(receiver.getUserSend()).getOutputStream());
sendStream.writeObject(receiver);
} else {
ObjectOutputStream sendStream = new ObjectOutputStream(hm.get(receiver.getUserSend()).getOutputStream());
sendStream.writeObject(receiver);
}
} else if (receiver.getMsgType() == MsgTypeConfig.EXIT_MSG) {
//下线提醒
Set<String> keySet = hm.keySet();
for (String key : keySet) {
if (key.equals(receiver.getUsername())) {
continue;
}
String timeStr = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(receiver.getTime());
String Msg = "【下线信息】:【用户:" + RegisterUserThread.username + ":" + "下线了】【" + timeStr + "】";
//转发格式:发送者:内容:消息类型:时间
User sendUser = new User(null, Msg, receiver.getMsgType(), receiver.getTime());
ObjectOutputStream sendStream = new ObjectOutputStream(hm.get(key).getOutputStream());
sendStream.writeObject(sendUser);
String ss = receiver.getUsername();
Server.info.remove(": " + ss + " " + hm.get(ss).getInetAddress() + " " + hm.get(ss).getPort() + " " +
hm.get(ss).getLocalPort() + " 在线】");
}
break;
}
}
//关闭这个人的管道
hm.get(receiver.getUsername()).close();
//移除这个用户
hm.remove(receiver.getUsername());
} catch (IOException e) {
e.printStackTrace();
}
}
}
package SanWa.Util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FileUtils {
public static byte[] readFile(String path) {
File file = new File(path);
byte datas[] = null;
if (!file.exists()) {
datas = null;
} else {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(file);
byte data[] = new byte[1024 * 1024];
int len = 0;
while ((len = fis.read(data)) > 0) {
baos.write(data, 0, len);
}
datas = baos.toByteArray();
baos.flush();
baos.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return datas;
}
public static boolean writeFile(String path, byte datas[]) {
try {
File file = new File(path);
FileOutputStream fos = new FileOutputStream(file);
fos.write(datas);
fos.flush();
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
package SanWa.Util;
public class PathUtils {
//获取路径
private static final String IMG_PATH="ChatRooms\\Image\\";
public static String getRealPath(String relativePath) {
return IMG_PATH + relativePath;
}
}
package SanWa.Util;
import java.awt.*;
public class ScreenUtils {
//获取电脑屏幕的宽度
public static int getScreenWidth() {
return Toolkit.getDefaultToolkit().getScreenSize().width;
}
//获取电脑屏幕的高度
public static int getScreenHeight() {
return Toolkit.getDefaultToolkit().getScreenSize().height;
}
}