while ((len = socketChannel.read(buffer)) > 0) { buffer.flip(); fileOutputStream.getChannel().write(buffer); buffer.clear(); } 这段代码加上注释
时间: 2023-06-10 07:08:15 浏览: 69
这段代码是一个基于 Java NIO 的网络传输示例。它的作用是从一个 SocketChannel 中读取数据,并将读取到的数据写入本地文件中,直到读取完所有数据。
具体来说,代码中的 `socketChannel` 是一个 SocketChannel 对象,表示与远程计算机建立的网络连接。`buffer` 是一个 ByteBuffer 对象,用于存储从 SocketChannel 中读取到的数据。`fileOutputStream` 是一个 FileOutputStream 对象,用于将数据写入本地文件。
代码中的 `while` 循环会不断尝试从 SocketChannel 中读取数据,直到读取完所有数据。每次读取数据后,需要将 ByteBuffer 的读取位置重置为缓冲区的起始位置(通过 `flip()` 方法实现),然后将数据写入本地文件(通过 `write()` 方法实现)。最后,需要清空 ByteBuffer 中的数据,以便下一轮读取数据(通过 `clear()` 方法实现)。
需要注意的是,这段代码并没有处理 SocketChannel 的异常情况,如连接异常、读取异常等。在实际应用中,需要根据具体情况对异常进行处理。
相关问题
while ((len = socketChannel.read(buffer)) > 0) { buffer.flip(); fileOutputStream.getChannel().write(buffer); buffer.clear(); } 每行加上注释
```java
while ((len = socketChannel.read(buffer)) > 0) { // 从socketChannel读取数据,如果返回值大于0,则说明读取成功
buffer.flip(); // 切换buffer为读模式
fileOutputStream.getChannel().write(buffer); // 将buffer中的数据写入文件输出流
buffer.clear(); // 切换buffer为写模式,准备下一次读取
}
```
用 java.nio 包的类写一个服务端和一个客户端,客户端发送D盘文件 1.jpg给服务端,服务端接收文件,返回字符串“文件已接收”,并把文件保存为D盘的2.jpg 每行代码都加注释
先写客户端:
```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("文件已接收");
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)