用Java实现基于Socket通信的电商客服系统设计与实现并要求客服端可以同时服务多个用户,有新的用户申请服务时有提示,可以选择服务或者拒绝服务;除了可以发送文字外,还可以发送文件给用户; 聊天记录需要保存在本地一个文件,同一个用户再次申请聊天时,需要打开文件,导入聊天记录,可以在聊天框查看之前记录;,用代码实现客服端
时间: 2024-04-01 18:31:59 浏览: 39
好的,以下是一个基于Socket通信的电商客服系统Java实现的示例,包含了多用户服务,文字和文件传输,聊天记录保存和导入等功能:
```java
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ChatServer extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextArea chatTextArea; //聊天记录文本区域
private JTextField inputField; //输入框
private JButton sendButton; //发送按钮
private JButton fileButton; //发送文件按钮
private JList userList; //用户列表
private JScrollPane chatScrollPane; //聊天记录滚动条
private JScrollPane userScrollPane; //用户列表滚动条
private JPanel northPanel; //北部面板
private JPanel southPanel; //南部面板
private JPanel eastPanel; //东部面板
private JPanel westPanel; //西部面板
private ServerSocket serverSocket; //服务器Socket
private List<UserThread> userThreads; //用户线程列表
private Map<String, List<String>> chatRecords; //聊天记录列表
//构造函数
public ChatServer() {
super("电商客服系统");
chatTextArea = new JTextArea();
inputField = new JTextField();
sendButton = new JButton("发送");
fileButton = new JButton("发送文件");
userList = new JList();
chatScrollPane = new JScrollPane(chatTextArea);
userScrollPane = new JScrollPane(userList);
northPanel = new JPanel();
southPanel = new JPanel();
eastPanel = new JPanel();
westPanel = new JPanel();
userThreads = new ArrayList<>();
chatRecords = new HashMap<>();
initUI();
initServer();
}
//初始化UI
private void initUI() {
chatTextArea.setEditable(false);
chatScrollPane.setPreferredSize(new Dimension(400, 300));
userScrollPane.setPreferredSize(new Dimension(100, 300));
northPanel.add(new JLabel("聊天记录"));
southPanel.setLayout(new BorderLayout());
southPanel.add(inputField, BorderLayout.CENTER);
southPanel.add(sendButton, BorderLayout.EAST);
southPanel.add(fileButton, BorderLayout.WEST);
eastPanel.setPreferredSize(new Dimension(100, 0));
westPanel.setPreferredSize(new Dimension(100, 0));
add(northPanel, BorderLayout.NORTH);
add(chatScrollPane, BorderLayout.CENTER);
add(southPanel, BorderLayout.SOUTH);
add(userScrollPane, BorderLayout.EAST);
add(westPanel, BorderLayout.WEST);
add(eastPanel, BorderLayout.EAST);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 400);
setLocationRelativeTo(null);
setVisible(true);
sendButton.addActionListener(this);
fileButton.addActionListener(this);
}
//初始化服务器
private void initServer() {
try {
serverSocket = new ServerSocket(8888);
new Thread(new Runnable() {
public void run() {
while (true) {
try {
Socket socket = serverSocket.accept();
UserThread userThread = new UserThread(socket);
userThread.start();
userThreads.add(userThread);
updateUserList();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
} catch (IOException e) {
e.printStackTrace();
}
}
//更新用户列表
private void updateUserList() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
DefaultListModel model = new DefaultListModel();
for (UserThread userThread : userThreads) {
model.addElement(userThread.getUserName());
}
userList.setModel(model);
}
});
}
//发送消息
private void sendMessage(String message) {
for (UserThread userThread : userThreads) {
userThread.sendMessage(message);
}
}
//发送文件
private void sendFile(File file) {
for (UserThread userThread : userThreads) {
userThread.sendFile(file);
}
}
//保存聊天记录
private void saveChatRecord(String userName, String message) {
if (!chatRecords.containsKey(userName)) {
chatRecords.put(userName, new ArrayList<String>());
}
chatRecords.get(userName).add(message);
try {
PrintWriter writer = new PrintWriter(new FileWriter(userName + ".txt"));
for (String record : chatRecords.get(userName)) {
writer.println(record);
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//导入聊天记录
private void loadChatRecord(String userName) {
if (chatRecords.containsKey(userName)) {
try {
BufferedReader reader = new BufferedReader(new FileReader(userName + ".txt"));
String line;
while ((line = reader.readLine()) != null) {
chatTextArea.append(line + "\n");
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//处理发送按钮和发送文件按钮的动作
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sendButton) {
String message = inputField.getText();
sendMessage(message);
inputField.setText("");
} else if (e.getSource() == fileButton) {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
sendFile(file);
}
}
}
//用户线程类
class UserThread extends Thread {
private Socket socket; //用户Socket
private String userName; //用户名
private BufferedReader reader; //输入流
private PrintWriter writer; //输出流
//构造函数
public UserThread(Socket socket) {
this.socket = socket;
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
userName = reader.readLine();
loadChatRecord(userName);
sendMessage("欢迎来到电商客服系统!您的用户名为:" + userName);
} catch (IOException e) {
e.printStackTrace();
}
}
//获取用户名
public String getUserName() {
return userName;
}
//发送消息
public void sendMessage(String message) {
writer.println(message);
saveChatRecord(userName, message);
}
//发送文件
public void sendFile(File file) {
try {
FileInputStream fileInputStream = new FileInputStream(file);
byte[] buffer = new byte[1024];
int length;
while ((length = fileInputStream.read(buffer)) > 0) {
socket.getOutputStream().write(buffer, 0, length);
}
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//接收消息
public void run() {
while (true) {
try {
if (reader.ready()) {
String message = reader.readLine();
sendMessage(userName + "说:" + message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//主函数
public static void main(String[] args) {
new ChatServer();
}
}
```
这个客服系统使用Java Swing GUI实现,可以同时服务多个用户,支持文字和文件传输。当有新的用户申请服务时,系统会提示客服人员选择服务或者拒绝服务。聊天记录会保存在本地一个文件,在同一个用户再次申请聊天时,可以打开文件导入聊天记录,并且可以在聊天框查看之前的记录。
阅读全文