用Java简单的网络文件共享系统的设计与实现·
时间: 2023-12-08 13:03:42 浏览: 46
设计思路:
1. 服务器端
服务器端主要负责文件的管理和共享,需要实现以下功能:
- 监听来自客户端的连接请求
- 接收客户端上传的文件,并保存在指定目录下
- 将指定目录下的文件列表发送给客户端
- 将客户端请求的文件发送给客户端
2. 客户端
客户端主要负责与服务器端进行通信,需要实现以下功能:
- 连接服务器并发送请求
- 上传本地文件到服务器上
- 下载服务器上的文件到本地
- 显示服务器上的文件列表
实现步骤:
1. 服务器端
首先需要创建一个服务器端程序,用于监听客户端的连接请求。这可以通过Java中的ServerSocket类来实现。
```java
public class Server {
private static final int PORT = 8888; // 服务器监听的端口号
private static final String DIR = "D:\\shared"; // 共享目录的路径
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(PORT);
System.out.println("服务器已启动,等待客户端连接...");
while (true) {
Socket socket = server.accept(); // 等待客户端连接
System.out.println("客户端已连接:" + socket.getInetAddress());
new ServerThread(socket).start(); // 创建新线程处理客户端请求
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
其中,ServerThread是一个继承自Thread的类,用于处理客户端请求。
接下来,我们需要在ServerThread中实现以下功能:
- 接收客户端上传的文件,并保存在指定目录下
- 将指定目录下的文件列表发送给客户端
- 将客户端请求的文件发送给客户端
```java
public class ServerThread extends Thread {
private Socket socket;
public ServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String line;
while ((line = in.readLine()) != null) {
if (line.startsWith("upload")) { // 上传文件
String[] parts = line.split(" ");
String filename = parts[1];
long fileSize = Long.parseLong(parts[2]);
FileOutputStream fos = new FileOutputStream(DIR + "\\" + filename);
byte[] buffer = new byte[1024];
int len;
long totalSize = 0;
while ((len = socket.getInputStream().read(buffer)) != -1) {
fos.write(buffer, 0, len);
totalSize += len;
if (totalSize == fileSize) {
break;
}
}
fos.close();
System.out.println("文件上传成功:" + filename);
} else if (line.equals("list")) { // 列出文件列表
File dir = new File(DIR);
String[] files = dir.list();
out.println(files.length);
for (String filename : files) {
out.println(filename);
}
} else if (line.startsWith("download")) { // 下载文件
String[] parts = line.split(" ");
String filename = parts[1];
File file = new File(DIR + "\\" + filename);
FileInputStream fis = new FileInputStream(file);
long fileSize = file.length();
out.println(fileSize);
byte[] buffer = new byte[1024];
int len;
long totalSize = 0;
while ((len = fis.read(buffer)) != -1) {
socket.getOutputStream().write(buffer, 0, len);
totalSize += len;
if (totalSize == fileSize) {
break;
}
}
fis.close();
System.out.println("文件下载成功:" + filename);
}
}
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
2. 客户端
客户端需要实现以下功能:
- 连接服务器并发送请求
- 上传本地文件到服务器上
- 下载服务器上的文件到本地
- 显示服务器上的文件列表
```java
public class Client {
private static final String SERVER_IP = "127.0.0.1"; // 服务器IP地址
private static final int SERVER_PORT = 8888; // 服务器端口号
public static void main(String[] args) {
try {
Socket socket = new Socket(SERVER_IP, SERVER_PORT);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
Scanner scanner = new Scanner(System.in);
String line;
while (true) {
System.out.println("请输入命令(list-列出文件列表,upload <filename>-上传文件,download <filename>-下载文件):");
line = scanner.nextLine();
if (line.equals("list")) { // 列出文件列表
out.println(line);
int count = Integer.parseInt(in.readLine());
for (int i = 0; i < count; i++) {
System.out.println(in.readLine());
}
} else if (line.startsWith("upload")) { // 上传文件
String[] parts = line.split(" ");
String filename = parts[1];
File file = new File(filename);
FileInputStream fis = new FileInputStream(file);
long fileSize = file.length();
out.println(line + " " + fileSize);
byte[] buffer = new byte[1024];
int len;
long totalSize = 0;
while ((len = fis.read(buffer)) != -1) {
socket.getOutputStream().write(buffer, 0, len);
totalSize += len;
if (totalSize == fileSize) {
break;
}
}
fis.close();
System.out.println("文件上传成功:" + filename);
} else if (line.startsWith("download")) { // 下载文件
String[] parts = line.split(" ");
String filename = parts[1];
out.println(line);
long fileSize = Long.parseLong(in.readLine());
FileOutputStream fos = new FileOutputStream(filename);
byte[] buffer = new byte[1024];
int len;
long totalSize = 0;
while ((len = socket.getInputStream().read(buffer)) != -1) {
fos.write(buffer, 0, len);
totalSize += len;
if (totalSize == fileSize) {
break;
}
}
fos.close();
System.out.println("文件下载成功:" + filename);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
以上就是一个简单的Java网络文件共享系统的设计与实现。需要注意的是,此实现仅适用于小型文件的共享,对于大型文件的共享可能需要进行分块传输等优化处理。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)