概述
Netty服务端启动代码:
public class NettyWebSocketServer extends Thread {
protected ServerBootstrap BOOTSTRAP;
protected EventLoopGroup BOSS_GROUP;
protected EventLoopGroup WORKER_GROUP;
protected int PORT;
public NettyWebSocketServer(int port, ChannelInitializer<?> initializer){
PORT = port;
BOSS_GROUP = new NioEventLoopGroup(1);
WORKER_GROUP = new NioEventLoopGroup();
BOOTSTRAP = new ServerBootstrap();
BOOTSTRAP.group(BOSS_GROUP, WORKER_GROUP)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler(initializer);
}
public void run(){
try {
ChannelFuture future = BOOTSTRAP.bind(PORT).sync();
LogUtil.println("Netty Websocket Server startup at PORT : " + PORT);
future.channel().closeFuture().sync();
}catch(Exception e){
e.printStackTrace();
}finally {
shutdown();
}
}
public void shutdown(){
BOSS_GROUP.shutdownGracefully();
WORKER_GROUP.shutdownGracefully();
LogUtil.println("Netty Websocket Server shutdown!");
}
public void startService(){
this.start();
}
public int getPORT() {
return PORT;
}
}
Channel通信链配置
public abstract class BaseChannelInitilalizer extends
ChannelInitializer<SocketChannel> {
protected ChannelHandler HANDLER;
protected MessageProcessMgr msgMgr;
public BaseChannelInitilalizer(ChannelHandler handler, MessageProcessMgr msgMgr) {
HANDLER = handler;
this.msgMgr = msgMgr;
}
}
public class TestServerInitializer extends BaseChannelInitilalizer {
public TestServerInitializer(ChannelHandler handler,
MessageProcessMgr msgMgr) {
super(handler, msgMgr);
}
@Override
protected void initChannel(SocketChannel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpObjectAggregator(65536));
pipeline.addLast(new ChunkedWriteHandler());
pipeline.addLast(HANDLER);
}
}
消息分发器
@Sharable
public class TestDispacheHandler extends SimpleChannelInboundHandler<Object> {
private static final Pattern INSECURE_URI = Pattern.compile(".*[<>&"].*");
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg)
throws Exception {
if(msg instanceof FullHttpRequest){
FullHttpRequest request = (FullHttpRequest)msg;
final String uri = request.getUri();
final String path = sanitizeUri(uri);
File file = new File(path);
RandomAccessFile raf = new RandomAccessFile(file, "r");
long fileLength = raf.length();
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
HttpHeaders.setContentLength(response, fileLength);
setContentTypeHeader(response, file);
if (HttpHeaders.isKeepAlive(request)) {
response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
}
ctx.write(response);
ChannelFuture sendFileFuture;
ChannelFuture lastContentFuture;
sendFileFuture = ctx.writeAndFlush(new HttpChunkedInput(new ChunkedFile(raf, 0, fileLength, 8192)),ctx.newProgressivePromise());
lastContentFuture = sendFileFuture;
sendFileFuture.addListener(new ChannelProgressiveFutureListener() {
@Override
public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) {
if (total < 0) { // total unknown
System.err.println(future.channel() + " Transfer progress: " + progress);
} else {
System.err.println(future.channel() + " Transfer progress: " + progress + " / " + total);
}
}
public void operationComplete(ChannelProgressiveFuture future) {
System.err.println(future.channel() + " Transfer complete.");
}
});
lastContentFuture.addListener(ChannelFutureListener.CLOSE);
}
}
private static String sanitizeUri(String uri) {
try {
uri = URLDecoder.decode(uri, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new Error(e);
}
if (uri.isEmpty() || uri.charAt(0) != '/') {
return null;
}
uri = uri.replace('/', File.separatorChar);
if (uri.contains(File.separator + '.') ||
uri.contains('.' + File.separator) ||
uri.charAt(0) == '.' || uri.charAt(uri.length() - 1) == '.' ||
INSECURE_URI.matcher(uri).matches()) {
return null;
}
return SystemPropertyUtil.get("user.dir") + File.separator + uri;
}
private static void setContentTypeHeader(HttpResponse response, File file) {
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
response.headers().set(CONTENT_TYPE, mimeTypesMap.getContentType(file.getPath()));
}
}
注解
@Sharable:如果不加这个注解,则分发器只会收到一次消息,以后再也收不到了
sendFileFuture = ctx.writeAndFlush(new HttpChunkedInput(new ChunkedFile(raf, 0, fileLength, 8192)),ctx.newProgressivePromise());
Chunk方式传输文件
public class TestMain {
public static void main(String[] args) throws Exception{
new NettyWebSocketServer(8080,new TestServerInitializer(new TestDispacheHandler(), null)).startService();
Thread.sleep(1000000);
}
}
浏览器中输入
http://127.0.0.1:8080/netty_demo_upload.jar,开始下载文件
最后
以上就是单身老师为你收集整理的Netty示例:文件下载的全部内容,希望文章能够帮你解决Netty示例:文件下载所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复