netty httpserver
2016-07-05 23:06
260 查看
netty也可以作为一个小巧的http服务器使用。
package com.ming.netty.http.httpserver; import java.net.InetSocketAddress; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.http.HttpContentCompressor; import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpRequestDecoder; import io.netty.handler.codec.http.HttpResponseDecoder; import io.netty.handler.stream.ChunkedWriteHandler; public class HttpServer { public static void main(String[] args) { new HttpServer().run("127.0.0.1", 8500); } public void run(String addr,int port){ NioEventLoopGroup boosGroup=new NioEventLoopGroup(); NioEventLoopGroup workGroup=new NioEventLoopGroup(); try { ServerBootstrap bootstrap=new ServerBootstrap(); bootstrap.group(boosGroup, workGroup); bootstrap.channel(NioServerSocketChannel.class); bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-decoder",new HttpRequestDecoder()); ch.pipeline().addLast("http-aggregator",new HttpObjectAggregator(65536));//定义缓冲数据量 ch.pipeline().addLast("encoder", new HttpResponseDecoder()); ch.pipeline().addLast("chunkedWriter", new ChunkedWriteHandler()); ch.pipeline().addLast("deflater", new HttpContentCompressor());//压缩作用 ch.pipeline().addLast("handler", new HttpServerHandler()); } }); ChannelFuture f=bootstrap.bind(new InetSocketAddress(addr, port)).sync(); System.out.println("启动服务器:"+f.channel().localAddress()); //等等服务器端监听端口关闭 f.channel().closeFuture().sync(); } catch (Exception e) { e.printStackTrace(); }finally{ workGroup.shutdownGracefully(); workGroup.shutdownGracefully(); } } }
package com.ming.netty.http.httpserver; import java.nio.ByteBuffer; import java.util.Map; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.buffer.UnpooledByteBufAllocator; import io.netty.channel.Channel; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.http.DefaultFullHttpRequest; import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.HttpContent; import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpMessage; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpVersion; import io.netty.handler.codec.http.LastHttpContent; import io.netty.util.CharsetUtil; public class HttpServerHandler extends SimpleChannelInboundHandler<Object> { private HttpRequest request; private ByteBuf buffer_body = UnpooledByteBufAllocator.DEFAULT.buffer(); private StringBuffer sb_debug = new StringBuffer(); @Override protected void messageReceived(ChannelHandlerContext ctx, Object msg) throws Exception { DefaultFullHttpRequest request=(DefaultFullHttpRequest)msg; System.out.println("启动服务器:"+request.getMethod()+request.getUri()); try { if ((msg instanceof HttpMessage) && HttpHeaders.is100ContinueExpected((HttpMessage)msg)) { ctx.write(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE)); } if (msg instanceof HttpRequest) { this.request = (HttpRequest)msg; sb_debug.append("\n>> HTTP REQUEST -----------\n"); sb_debug.append(this.request.getProtocolVersion().toString()) .append(" ").append(this.request.getMethod().name()) .append(" ").append(this.request.getUri()); sb_debug.append("\n"); HttpHeaders headers = this.request.headers(); if (!headers.isEmpty()) { for (Map.Entry<String, String> header : headers) { sb_debug.append(header.getKey()).append(": ").append(header.getValue()).append("\n"); } } sb_debug.append("\n"); } else if (msg instanceof HttpContent) { HttpContent content = (HttpContent) msg; ByteBuf thisContent = content.content(); if (thisContent.isReadable()) { buffer_body.writeBytes(thisContent); } if (msg instanceof LastHttpContent) { sb_debug.append(buffer_body.toString(CharsetUtil.UTF_8)); LastHttpContent trailer = (LastHttpContent) msg; if (!trailer.trailingHeaders().isEmpty()) { for (String name : trailer.trailingHeaders().names()) { sb_debug.append(name).append("="); for (String value : trailer.trailingHeaders().getAll(name)) { sb_debug.append(value).append(","); } sb_debug.append("\n\n"); } } sb_debug.append("\n<< HTTP REQUEST -----------"); } } } catch (Exception e) { e.printStackTrace(); } finally { System.out.println(sb_debug+""); FullHttpResponse response=new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.ACCEPTED); String str="hello,my netty httpServer!"; StringBuilder buf=new StringBuilder(); buf.append("<!DOCTYPE html><head></head><body>").append(str).append("</body></html>"); ByteBuf buffer=Unpooled.copiedBuffer(buf,CharsetUtil.UTF_8); response.content().writeBytes(buffer); response.headers().set("Content-Type", "text/html; charset=UTF-8"); response.headers().set("Content-Length",1000); buffer.release(); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } } }
相关文章推荐
- 看完后,搞懂ARP的工作原理,其实并不难!
- APP开发流程实例讲解-儒释道网络电台八天开发全程-在Android Studio中完成界面设计
- 查看工作组计算机 无法访问 没有权限使用网络资源
- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path 解决方法
- HTTP协议(下午茶)
- Google摒弃HttpClient之后 OKHttp第三框架
- Google摒弃HttpClient之后 OKHttp第三框架
- Google摒弃HttpClient之后 OKHttp第三框架
- Google摒弃HttpClient之后 OKHttp第三框架
- 安卓第三方框架 android-async-http
- 安卓第三方框架 android-async-http
- 安卓第三方框架 android-async-http
- 安卓第三方框架 android-async-http
- netty tcp option SO_RCVBUF
- 计算机网络(5)-----ICMP协议和PING程序
- mac 下virtual box 安装多台centOs并配置网络集群
- UNIX网络编程——并发服务器(多进程)
- 计算机网络(4)-----子网掩码
- 网络编程概述及Http协议
- 计算机网络(3)-----IP数据报格式