文件服務(wù)器
實(shí)現(xiàn)一個(gè)可以展示指定用戶輸入的文件路徑,返回對(duì)應(yīng)文件內(nèi)容的服務(wù)器。

實(shí)例代碼
服務(wù)端
public class FileServer {
public static void main(String[] args) {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
ChannelFuture channelFuture = serverBootstrap.group(bossGroup, workerGroup)
.channel(NIOServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// 編碼 String
ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8))
// 按照行進(jìn)行解碼
.addLast(new LineBasedFrameDecoder(1024))
// String 解碼
.addLast(new StringDecoder(CharsetUtil.UTF_8))
// 大數(shù)據(jù)流的處理
.addLast(new ChunkedWriteHandler())
.addLast(new FileServerHandler());
}
})
.bind(8889)
.syncUninterruptibly();
channelFuture.channel().closeFuture().syncUninterruptibly();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
}
FileServerHandler.JAVA
針對(duì)文件服務(wù)器的處理,實(shí)現(xiàn)如下:
import java.io.RandomaccessFile;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.stream.ChunkedFile;
public class FileServerHandler extends SimpleChannelInboundHandler<String> {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
// 提醒客戶端輸入文件路徑
ctx.writeAndFlush("HELLO: Type the path of the file to retrieve.n");
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
// 只讀方式打開(kāi)文件
try(RandomAccessFile file = new RandomAccessFile(msg, "r")) {
long length = file.length();
ctx.write("OK: " + length + 'n');
ctx.write(new ChunkedFile(file));
ctx.writeAndFlush("n");
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
整體比較簡(jiǎn)單,exceptionCaught 為異常時(shí)的處理。
channelActive() 為客戶端連接時(shí),服務(wù)端返回客戶端的提示。
channelRead0() 為服務(wù)端對(duì)于客戶端的反饋,就是通過(guò)客戶端輸入的文件路徑,返回文件內(nèi)容。
測(cè)試驗(yàn)證
我們直接使用本地的 telnet
- 打開(kāi)命令行
輸入 telnet localhost 8889
192:~ houbinbin$ telnet localhost 8889
Trying ::1...
Connected to localhost.
Escape character is '^]'.
HELLO: Type the path of the file to retrieve.
- 輸入文件路徑
/Users/houbinbin/code/_github/netty-learn/netty-learn-four/src/main/java/com/github/houbb/netty/learn/four/file/FileServer.java
反饋如下:
就是把 FileServer.java 這個(gè)文件內(nèi)容全部返回回來(lái)了。
OK: 2387
/*
* Copyright (c) 2019. houbinbin Inc.
* netty-learn All rights reserved.
*/
package com.github.houbb.netty.learn.four.file;
....... 內(nèi)容省略
/**
* <p> </p>
*
* <pre> Created: 2019/9/21 11:49 PM </pre>
* <pre> Project: netty-learn </pre>
*
* @author houbinbin
*/
public class FileServer {
public static void main(String[] args) {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
.... 內(nèi)容省略
}
}
Connection closed by foreign host.