while ((len = socketChannel.read(buffer)) > 0) { buffer.flip(); fileOutputStream.getChannel().write(buffer); buffer.clear(); } 每行加上注释
时间: 2023-06-10 10:08:14 浏览: 67
```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("文件已接收");
}
}
```
10T文件怎么用socketChannel从客户端传输到服务端 完整优化代码
下面是一个基于 Java NIO 的完整优化代码示例,用于将10TB文件使用 SocketChannel 从客户端传输到服务端:
客户端代码:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
public class Client {
private static final int BUFFER_SIZE = 4096;
private static final String FILE_PATH = "path/to/10TB/file";
public static void main(String[] args) {
try {
// 打开文件通道
FileChannel fileChannel = new FileInputStream(new File(FILE_PATH)).getChannel();
// 创建SocketChannel并连接服务端
SocketChannel socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("server_ip", server_port));
// 循环读取文件数据并写入SocketChannel
ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
while (fileChannel.read(buffer) != -1) {
buffer.flip();
socketChannel.write(buffer);
buffer.clear();
}
// 关闭资源
fileChannel.close();
socketChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
服务端代码:
```java
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
public class Server {
private static final int BUFFER_SIZE = 4096;
private static final String OUTPUT_FILE_PATH = "path/to/output/file";
public static void main(String[] args) {
try {
// 创建ServerSocketChannel并绑定到指定端口
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.bind(new InetSocketAddress(server_port));
// 等待客户端连接
SocketChannel socketChannel = serverSocketChannel.accept();
// 创建文件通道
FileChannel fileChannel = new FileOutputStream(OUTPUT_FILE_PATH).getChannel();
// 循环读取SocketChannel数据并写入文件通道
ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
while (socketChannel.read(buffer) != -1) {
buffer.flip();
fileChannel.write(buffer);
buffer.clear();
}
// 关闭资源
fileChannel.close();
socketChannel.close();
serverSocketChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
请注意替换代码中的 `server_ip`、`server_port`、`FILE_PATH` 和 `OUTPUT_FILE_PATH` 为实际的值。此示例仅作为基本框架,你可能需要根据实际需求进行进一步的错误处理和优化。