用java设计并实现一个基于字符界面的C/S结构的文件传输程序
时间: 2023-12-18 11:03:01 浏览: 84
好的,我可以为您提供一份基本的Java代码实现:
1. 客户端界面设计
客户端界面采用字符界面,使用Java中的Console类来实现。客户端界面需要包括以下功能:
- 连接服务器
- 上传文件
- 下载文件
- 查看服务器上的文件列表
- 断开连接
以下是客户端界面的代码实现:
```
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class Client {
private Socket socket;
private BufferedReader reader;
private Console console;
public Client() {
console = System.console();
}
public void start() {
while (true) {
String command = console.readLine("请输入命令:");
if (command.equals("connect")) {
connect();
} else if (command.equals("upload")) {
upload();
} else if (command.equals("download")) {
download();
} else if (command.equals("list")) {
list();
} else if (command.equals("disconnect")) {
disconnect();
break;
} else {
System.out.println("无效命令,请重新输入!");
}
}
}
private void connect() {
try {
String host = console.readLine("请输入服务器地址:");
int port = Integer.parseInt(console.readLine("请输入服务器端口号:"));
socket = new Socket(host, port);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("连接成功!");
} catch (IOException e) {
System.out.println("连接失败!");
e.printStackTrace();
}
}
private void upload() {
// TODO: 实现文件上传
}
private void download() {
// TODO: 实现文件下载
}
private void list() {
// TODO: 实现查看服务器上的文件列表
}
private void disconnect() {
try {
socket.close();
reader.close();
System.out.println("已断开连接!");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Client client = new Client();
client.start();
}
}
```
2. 服务器端实现
服务器端需要实现以下功能:
- 监听客户端连接请求
- 接收客户端上传的文件并保存到指定目录
- 发送指定目录下的文件列表给客户端
- 接收客户端下载的文件并发送给客户端
以下是服务器端的代码实现:
```
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Server {
private ServerSocket serverSocket;
private List<String> fileList;
public Server(int port) {
try {
serverSocket = new ServerSocket(port);
fileList = new ArrayList<>();
System.out.println("服务器已启动,等待客户端连接...");
} catch (IOException e) {
e.printStackTrace();
}
}
public void start() {
while (true) {
try {
Socket socket = serverSocket.accept();
System.out.println("客户端已连接:" + socket.getInetAddress());
new Thread(new ClientHandler(socket)).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private synchronized void addFile(String filename) {
fileList.add(filename);
}
private synchronized void removeFile(String filename) {
fileList.remove(filename);
}
private synchronized List<String> getFileList() {
return new ArrayList<>(fileList);
}
private class ClientHandler implements Runnable {
private Socket socket;
private BufferedReader reader;
private PrintWriter writer;
public ClientHandler(Socket socket) {
try {
this.socket = socket;
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while (true) {
String command = reader.readLine();
if (command == null) {
break;
}
if (command.equals("upload")) {
receiveFile();
} else if (command.equals("download")) {
sendFile();
} else if (command.equals("list")) {
sendFileList();
} else {
writer.println("无效命令!");
writer.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
reader.close();
writer.close();
System.out.println("客户端已断开连接:" + socket.getInetAddress());
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void receiveFile() throws IOException {
String filename = reader.readLine();
addFile(filename);
FileOutputStream fos = new FileOutputStream(filename);
byte[] buffer = new byte[1024];
int len;
while ((len = socket.getInputStream().read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
removeFile(filename);
System.out.println("已接收文件:" + filename);
}
private void sendFile() throws IOException {
String filename = reader.readLine();
File file = new File(filename);
if (!file.exists() || !file.isFile()) {
writer.println("文件不存在!");
writer.flush();
return;
}
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
socket.getOutputStream().write(buffer, 0, len);
}
fis.close();
System.out.println("已发送文件:" + filename);
}
private void sendFileList() throws IOException {
List<String> fileList = getFileList();
for (String filename : fileList) {
writer.println(filename);
}
writer.flush();
System.out.println("已发送文件列表!");
}
}
public static void main(String[] args) {
Server server = new Server(8888);
server.start();
}
}
```
以上是一个基本的Java代码实现,可以根据实际需求进行调整和优化。
阅读全文