用 java.nio 包的类写一个服务端和一个客户端,客户端发送D盘文件 1.jpg给服务端,服务端接收文件,返回字符串“文件已接收”,并把文件保存为D盘的2.jpg 每行代码都加注释
时间: 2023-06-13 12:02:05 浏览: 48
先写客户端:
```java
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FileClient {
public static void main(String[] args) throws IOException {
// 1. 创建 SocketChannel,并连接到服务端地址
SocketChannel socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("localhost", 8888));
System.out.println("连接服务器成功");
// 2. 读取文件并发送给服务端
Path filePath = Paths.get("D:/1.jpg");
byte[] fileBytes = Files.readAllBytes(filePath);
ByteBuffer buffer = ByteBuffer.wrap(fileBytes);
while (buffer.hasRemaining()) {
socketChannel.write(buffer);
}
System.out.println("文件发送成功");
// 3. 关闭 SocketChannel
socketChannel.close();
}
}
```
然后是服务端:
```java
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FileServer {
public static void main(String[] args) throws IOException {
// 1. 创建 ServerSocketChannel,并绑定端口
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.bind(new InetSocketAddress(8888));
System.out.println("服务器启动,等待客户端连接");
// 2. 接受客户端连接,并接收文件
SocketChannel socketChannel = serverSocketChannel.accept();
System.out.println("客户端已连接");
ByteBuffer buffer = ByteBuffer.allocate(1024);
Path filePath = Paths.get("D:/2.jpg");
FileOutputStream fileOutputStream = new FileOutputStream(filePath.toFile());
int len;
while ((len = socketChannel.read(buffer)) > 0) {
buffer.flip();
fileOutputStream.getChannel().write(buffer);
buffer.clear();
}
System.out.println("文件接收成功");
// 3. 关闭 SocketChannel 和 ServerSocketChannel
socketChannel.close();
serverSocketChannel.close();
// 4. 返回字符串“文件已接收”
System.out.println("文件已接收");
}
}
```
注释如下:
1. 客户端:
```java
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FileClient {
public static void main(String[] args) throws IOException {
// 1. 创建 SocketChannel,并连接到服务端地址
SocketChannel socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("localhost", 8888));
System.out.println("连接服务器成功");
// 2. 读取文件并发送给服务端
Path filePath = Paths.get("D:/1.jpg"); // 文件路径
byte[] fileBytes = Files.readAllBytes(filePath); // 读取文件
ByteBuffer buffer = ByteBuffer.wrap(fileBytes); // 包装成 ByteBuffer
while (buffer.hasRemaining()) { // 发送文件
socketChannel.write(buffer);
}
System.out.println("文件发送成功");
// 3. 关闭 SocketChannel
socketChannel.close();
}
}
```
2. 服务端:
```java
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FileServer {
public static void main(String[] args) throws IOException {
// 1. 创建 ServerSocketChannel,并绑定端口
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.bind(new InetSocketAddress(8888));
System.out.println("服务器启动,等待客户端连接");
// 2. 接受客户端连接,并接收文件
SocketChannel socketChannel = serverSocketChannel.accept();
System.out.println("客户端已连接");
ByteBuffer buffer = ByteBuffer.allocate(1024); // 读取数据的缓冲区
Path filePath = Paths.get("D:/2.jpg"); // 文件路径
FileOutputStream fileOutputStream = new FileOutputStream(filePath.toFile()); // 创建文件输出流
int len;
while ((len = socketChannel.read(buffer)) > 0) { // 读取数据并写入文件
buffer.flip();
fileOutputStream.getChannel().write(buffer);
buffer.clear();
}
System.out.println("文件接收成功");
// 3. 关闭 SocketChannel 和 ServerSocketChannel
socketChannel.close();
serverSocketChannel.close();
// 4. 返回字符串“文件已接收”
System.out.println("文件已接收");
}
}
```