ServerSocketChannel实现文件上传下载
时间: 2023-11-09 12:04:15 浏览: 69
ServerSocketChannel 是 Java NIO 中用来实现基于 TCP 协议的服务器的核心类之一,可以轻松地实现文件上传和下载。
1. 文件上传
以下是一个简单的文件上传服务器示例代码:
```java
try (ServerSocketChannel serverSocketChannel = ServerSocketChannel.open()) {
serverSocketChannel.bind(new InetSocketAddress(8080));
System.out.println("Server started on port 8080.");
while (true) {
try (SocketChannel socketChannel = serverSocketChannel.accept()) {
System.out.println("Client connected from " + socketChannel.getRemoteAddress());
ByteBuffer buffer = ByteBuffer.allocate(1024);
// 读取文件名
socketChannel.read(buffer);
buffer.flip();
String fileName = StandardCharsets.UTF_8.decode(buffer).toString();
// 读取文件内容
buffer.clear();
try (FileChannel fileChannel = FileChannel.open(Paths.get(fileName), StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
while (socketChannel.read(buffer) > 0) {
buffer.flip();
fileChannel.write(buffer);
buffer.clear();
}
}
System.out.println("File uploaded: " + fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
```
客户端代码:
```java
try (SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("localhost", 8080))) {
Path path = Paths.get("test.txt");
ByteBuffer buffer = ByteBuffer.allocate(1024);
// 发送文件名
buffer.put(path.getFileName().toString().getBytes());
buffer.flip();
socketChannel.write(buffer);
buffer.clear();
// 发送文件内容
try (FileChannel fileChannel = FileChannel.open(path)) {
while (fileChannel.read(buffer) > 0) {
buffer.flip();
socketChannel.write(buffer);
buffer.clear();
}
}
System.out.println("File uploaded.");
} catch (IOException e) {
e.printStackTrace();
}
```
2. 文件下载
以下是一个简单的文件下载服务器示例代码:
```java
try (ServerSocketChannel serverSocketChannel = ServerSocketChannel.open()) {
serverSocketChannel.bind(new InetSocketAddress(8080));
System.out.println("Server started on port 8080.");
while (true) {
try (SocketChannel socketChannel = serverSocketChannel.accept()) {
System.out.println("Client connected from " + socketChannel.getRemoteAddress());
ByteBuffer buffer = ByteBuffer.allocate(1024);
// 读取文件名
socketChannel.read(buffer);
buffer.flip();
String fileName = StandardCharsets.UTF_8.decode(buffer).toString();
// 发送文件内容
buffer.clear();
try (FileChannel fileChannel = FileChannel.open(Paths.get(fileName))) {
while (fileChannel.read(buffer) > 0) {
buffer.flip();
socketChannel.write(buffer);
buffer.clear();
}
}
System.out.println("File downloaded: " + fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
```
客户端代码:
```java
try (SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("localhost", 8080))) {
Path path = Paths.get("test.txt");
ByteBuffer buffer = ByteBuffer.allocate(1024);
// 发送文件名
buffer.put(path.getFileName().toString().getBytes());
buffer.flip();
socketChannel.write(buffer);
buffer.clear();
// 接收文件内容
try (FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
while (socketChannel.read(buffer) > 0) {
buffer.flip();
fileChannel.write(buffer);
buffer.clear();
}
}
System.out.println("File downloaded.");
} catch (IOException e) {
e.printStackTrace();
}
```
以上就是使用 ServerSocketChannel 实现文件上传和下载的简单示例。注意在实际生产环境中需要对代码进行优化和处理可能的异常情况。
阅读全文