Java设计并实现一个基于字符界面的C/S结构的文件传输程序,给出代码和配置文件
时间: 2024-03-17 08:46:31 浏览: 19
好的,下面是一个Java实现的基于字符界面的C/S结构的文件传输程序的代码和配置文件。
客户端代码:
```java
import java.io.*;
import java.net.*;
import java.util.*;
public class FileTransferClient {
private static final int BUFFER_SIZE = 1024;
private static final String SERVER_HOST = "localhost";
private static final int SERVER_PORT = 1234;
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
// 连接服务器
Socket socket = new Socket(SERVER_HOST, SERVER_PORT);
System.out.println("Connected to server");
// 获取输入输出流
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
while (true) {
// 显示菜单
System.out.println("1. Upload file");
System.out.println("2. Download file");
System.out.println("3. List files");
System.out.println("4. Exit");
System.out.print("Enter choice: ");
// 读取用户输入
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
// 上传文件
System.out.print("Enter file path: ");
String uploadFilePath = scanner.nextLine();
File uploadFile = new File(uploadFilePath);
if (!uploadFile.exists()) {
System.out.println("File not found");
break;
}
// 发送文件名和文件长度
out.writeUTF(uploadFile.getName());
out.writeLong(uploadFile.length());
// 读取文件内容并发送
try (FileInputStream fis = new FileInputStream(uploadFile)) {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
System.out.println("File uploaded successfully");
break;
case 2:
// 下载文件
System.out.print("Enter file name: ");
String downloadFileName = scanner.nextLine();
// 发送文件名
out.writeUTF(downloadFileName);
// 接收文件长度
long downloadFileSize = in.readLong();
if (downloadFileSize == -1) {
System.out.println("File not found");
break;
}
// 接收文件内容并保存
try (FileOutputStream fos = new FileOutputStream(downloadFileName)) {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
while (downloadFileSize > 0 && (bytesRead = in.read(buffer, 0, (int) Math.min(buffer.length, downloadFileSize))) != -1) {
fos.write(buffer, 0, bytesRead);
downloadFileSize -= bytesRead;
}
}
System.out.println("File downloaded successfully");
break;
case 3:
// 列出文件列表
out.writeUTF("LIST");
int fileCount = in.readInt();
System.out.println("File count: " + fileCount);
for (int i = 0; i < fileCount; i++) {
System.out.println(in.readUTF());
}
break;
case 4:
// 退出程序
out.writeUTF("EXIT");
socket.close();
System.exit(0);
default:
System.out.println("Invalid choice");
break;
}
}
}
}
```
服务器端代码:
```java
import java.io.*;
import java.net.*;
import java.util.*;
public class FileTransferServer {
private static final int BUFFER_SIZE = 1024;
private static final String ROOT_DIR = "/path/to/root/dir";
private static final int SERVER_PORT = 1234;
public static void main(String[] args) throws IOException {
// 启动服务器
ServerSocket serverSocket = new ServerSocket(SERVER_PORT);
System.out.println("Server started");
while (true) {
// 等待客户端连接
Socket socket = serverSocket.accept();
System.out.println("Client connected");
// 获取输入输出流
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
while (true) {
// 接收客户端请求
String request = in.readUTF();
if (request.equals("UPLOAD")) {
// 接收上传文件
String fileName = in.readUTF();
long fileSize = in.readLong();
File file = new File(ROOT_DIR, fileName);
try (FileOutputStream fos = new FileOutputStream(file)) {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
while (fileSize > 0 && (bytesRead = in.read(buffer, 0, (int) Math.min(buffer.length, fileSize))) != -1) {
fos.write(buffer, 0, bytesRead);
fileSize -= bytesRead;
}
}
System.out.println("File uploaded: " + fileName);
} else if (request.equals("DOWNLOAD")) {
// 发送下载文件
String fileName = in.readUTF();
File file = new File(ROOT_DIR, fileName);
if (!file.exists()) {
// 文件不存在
out.writeLong(-1);
} else {
// 发送文件长度
out.writeLong(file.length());
// 发送文件内容
try (FileInputStream fis = new FileInputStream(file)) {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
}
System.out.println("File downloaded: " + fileName);
} else if (request.equals("LIST")) {
// 列出文件列表
File rootDir = new File(ROOT_DIR);
File[] files = rootDir.listFiles();
out.writeInt(files.length);
for (File file : files) {
out.writeUTF(file.getName());
}
} else if (request.equals("EXIT")) {
// 断开连接
socket.close();
System.out.println("Client disconnected");
break;
} else {
System.out.println("Invalid request: " + request);
}
}
}
}
}
```
客户端配置文件:
```
server.host=localhost
server.port=1234
```
服务器端配置文件:
```
root.dir=/path/to/root/dir
server.port=1234
```
注意:以上代码和配置文件仅供参考,具体实现还需要根据实际情况进行调整和完善。同时,由于篇幅和时间限制,代码中可能存在错误或者不完善的地方,请读者自行检查和完善。