使用netty实现一个http挡板,轻量又实用。收藏起来吧

64 阅读5分钟

目录

一、需要的包

 二、代码

三、导出jar包

四、启动jar包

五、使用


一、需要的包

 二、代码


package com.cxf.http;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
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.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;

public class NettyHttpServer{



    private int inetPort;
 
    public NettyHttpServer(int inetPort) {
        this.inetPort = inetPort;
    }
 
    public int getInetPort() {
        return inetPort;
    }
 
    public void init() throws Exception {
 
        EventLoopGroup parentGroup = new NioEventLoopGroup();
        EventLoopGroup childGroup = new NioEventLoopGroup();
 
        try {
            ServerBootstrap server = new ServerBootstrap();
            // 1. 绑定两个线程组分别用来处理客户端通道的accept和读写时间
            server.group(parentGroup, childGroup)
                    // 2. 绑定服务端通道NioServerSocketChannel
                    .channel(NioServerSocketChannel.class)
                    // 3. 给读写事件的线程通道绑定handler去真正处理读写
                    // ChannelInitializer初始化通道SocketChannel
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            // 请求解码器
                            socketChannel.pipeline().addLast("http-decoder", new HttpRequestDecoder());
                            // 将HTTP消息的多个部分合成一条完整的HTTP消息
                            socketChannel.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65535));
                            // 响应转码器
                            socketChannel.pipeline().addLast("http-encoder", new HttpResponseEncoder());
                            // 解决大码流的问题,ChunkedWriteHandler:向客户端发送HTML5文件
                            socketChannel.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
                            // 自定义处理handler
                            socketChannel.pipeline().addLast("http-server", new NettyHttpServerHandler());
                        }
                    });
            System.out.println("Netty-http服务器已启动,端口" + inetPort);
            // 4. 监听端口(服务器host和port端口),同步返回
            // ChannelFuture future = server.bind(inetHost, this.inetPort).sync();
            ChannelFuture future = server.bind(this.inetPort).sync();
            // 当通道关闭时继续向后执行,这是一个阻塞方法
            future.channel().closeFuture().sync();
        } finally {
            childGroup.shutdownGracefully();
            parentGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) {
        NettyHttpServer nettyHttpServer = new NettyHttpServer(Integer.parseInt(args[0]));
        try {
            nettyHttpServer.init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
}
package com.cxf.http;

import static io.netty.buffer.Unpooled.copiedBuffer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.QueryStringDecoder;
import io.netty.handler.codec.http.multipart.DefaultHttpDataFactory;
import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder;
import io.netty.handler.codec.http.multipart.InterfaceHttpData;
import io.netty.handler.codec.http.multipart.MemoryAttribute;
import io.netty.util.CharsetUtil;

/*
* 自定义处理的handler
*/
public class NettyHttpServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
	

   /*
    * 处理请求
    */
   @Override
   protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpRequest fullHttpRequest) throws IOException {

       FullHttpResponse response = null;
       if (fullHttpRequest.method() == HttpMethod.GET) {
           String data = getResponse(channelHandlerContext, fullHttpRequest);
           ByteBuf buf = copiedBuffer(data, CharsetUtil.UTF_8);
           response = responseOK(HttpResponseStatus.OK, buf);
       } else if (fullHttpRequest.method() == HttpMethod.POST) {
           String data = getResponse(channelHandlerContext, fullHttpRequest);
           ByteBuf content = copiedBuffer(data, CharsetUtil.UTF_8);
           response = responseOK(HttpResponseStatus.OK, content);

       } else {
           String data = "未找到对应的url";
           ByteBuf content = copiedBuffer(data, CharsetUtil.UTF_8);
           response = responseOK(HttpResponseStatus.INTERNAL_SERVER_ERROR, content);
       }
       // 发送响应
       channelHandlerContext.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
   }
   private String getResponse(ChannelHandlerContext channelHandlerContext, FullHttpRequest fullHttpRequest) throws IOException {
	   System.out.println("===============================");
	   String uri = fullHttpRequest.getUri();
	   System.out.println("请求url:" + uri);
	   String content = fullHttpRequest.content().toString(CharsetUtil.UTF_8);
	   System.out.println("请求内容:" + content);
	   String path = new File(".").getCanonicalPath() + "/files" + uri;
	   System.out.println("读取挡板文件路径:" + path);
	   String data = getFileContent(path);
	   System.out.println("返回内容:" + data);
       System.out.println("===============================");
       return data;
}
/**
	 * @param fullHttpRequest
	 * @return 获取参数
	 */
   private String getParam(FullHttpRequest fullHttpRequest) {
		ByteBuf content = fullHttpRequest.content();
		byte[] reqContent = new byte[content.readableBytes()];
		content.readBytes(reqContent);
		String strContent = "";
		try {
			strContent = new String(reqContent, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return strContent;
   }
   
   
   /*
    * 获取GET方式传递的参数
    */
   private Map<String, Object> getGetParamsFromChannel(FullHttpRequest fullHttpRequest) {

       Map<String, Object> params = new HashMap<String, Object>();

       //if (fullHttpRequest.method() == HttpMethod.GET) {
           // 处理get请求
           QueryStringDecoder decoder = new QueryStringDecoder(fullHttpRequest.uri());
           Map<String, List<String>> paramList = decoder.parameters();
           for (Map.Entry<String, List<String>> entry : paramList.entrySet()) {
               params.put(entry.getKey(), entry.getValue().get(0));
           }
           return params;
       //} else {
       //    return null;
       //}

   }

   /*
    * 获取POST方式传递的参数
    */
   private Map<String, Object> getPostParamsFromChannel(FullHttpRequest fullHttpRequest) {

       Map<String, Object> params = new HashMap<String, Object>();

       if (fullHttpRequest.method() == HttpMethod.POST) {
           // 处理POST请求
           String strContentType = fullHttpRequest.headers().get("Content-Type").trim();
           if (strContentType.contains("x-www-form-urlencoded")) {
               params  = getFormParams(fullHttpRequest);
           } else if (strContentType.contains("application/json")) {
               try {
                   params = getJSONParams(fullHttpRequest);
               } catch (UnsupportedEncodingException e) {
                   return null;
               }
           } else {
               return null;
           }
           return params;
       } else {
           return null;
       }
   }

   /*
    * 解析from表单数据(Content-Type = x-www-form-urlencoded)
    */
   private Map<String, Object> getFormParams(FullHttpRequest fullHttpRequest) {
       Map<String, Object> params = new HashMap<String, Object>();

       HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), fullHttpRequest);
       List<InterfaceHttpData> postData = decoder.getBodyHttpDatas();

       for (InterfaceHttpData data : postData) {
           if (data.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) {
               MemoryAttribute attribute = (MemoryAttribute) data;
               params.put(attribute.getName(), attribute.getValue());
           }
       }

       return params;
   }

   /*
    * 解析json数据(Content-Type = application/json)
    */
   private Map<String, Object> getJSONParams(FullHttpRequest fullHttpRequest) throws UnsupportedEncodingException {
       Map<String, Object> params = new HashMap<String, Object>();

       ByteBuf content = fullHttpRequest.content();
       byte[] reqContent = new byte[content.readableBytes()];
       content.readBytes(reqContent);
       String strContent = new String(reqContent, "UTF-8");

     /*  JSONObject jsonParams = JSONObject.fromObject(strContent);
       for (Object key : jsonParams.keySet()) {
           params.put(key.toString(), jsonParams.get(key));
       }*/

       return params;
   }

   private FullHttpResponse responseOK(HttpResponseStatus status, ByteBuf content) {
       FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, content);
       if (content != null) {
           response.headers().set("Content-Type", "text/json;charset=UTF-8");
           response.headers().set("Content_Length", response.content().readableBytes());
       }
       return response;
   }
   
   /**
    * 读取文件的内容
    * 读取指定文件的内容
    * @param path 为要读取文件的绝对路径
    * @return 以行读取文件后的内容。
    * @since  1.0
    */
   public static final String getFileContent(String path) throws IOException
   {
     String filecontent = "";
     try {
       File f = new File(path);
       if (f.exists()) {
         FileReader fr = new FileReader(path);
         BufferedReader br = new BufferedReader(fr); //建立BufferedReader对象,并实例化为br
         String line = br.readLine(); //从文件读取一行字符串
         //判断读取到的字符串是否不为空
         while (line != null) {
           filecontent += line + "\n";
           line = br.readLine(); //从文件中继续读取一行数据
         }
         br.close(); //关闭BufferedReader对象
         fr.close(); //关闭文件
       }

     }
     catch (IOException e) {
       throw e;
     }
     return filecontent;
   }

}

另一种方式:

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class TestServer {
    public static void main(String[] args) throws Exception {

        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();

            serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new TestServerInitializer());

            ChannelFuture channelFuture = serverBootstrap.bind(6668).sync();
            
            channelFuture.channel().closeFuture().sync();

        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}


import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;


public class TestServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {

        //向管道加入处理器

        //得到管道
        ChannelPipeline pipeline = ch.pipeline();

        //加入一个netty 提供的httpServerCodec codec =>[coder - decoder]
        //HttpServerCodec 说明
        //1. HttpServerCodec 是netty 提供的处理http的 编-解码器
        pipeline.addLast("MyHttpServerCodec",new HttpServerCodec());
        //2. 增加一个自定义的handler
        pipeline.addLast("MyTestHttpServerHandler", new TestHttpServerHandler());

        System.out.println("ok~~~~");

    }
}



import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;

import java.net.URI;

/*
说明
1. SimpleChannelInboundHandler 是 ChannelInboundHandlerAdapter
2. HttpObject 客户端和服务器端相互通讯的数据被封装成 HttpObject
 */
public class TestHttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {


    //channelRead0 读取客户端数据
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {


        System.out.println("对应的channel=" + ctx.channel() + " pipeline=" + ctx
        .pipeline() + " 通过pipeline获取channel" + ctx.pipeline().channel());

        System.out.println("当前ctx的handler=" + ctx.handler());

        //判断 msg 是不是 httprequest请求
        if(msg instanceof HttpRequest) {

            System.out.println("ctx 类型="+ctx.getClass());

            System.out.println("pipeline hashcode" + ctx.pipeline().hashCode() + " TestHttpServerHandler hash=" + this.hashCode());

            System.out.println("msg 类型=" + msg.getClass());
            System.out.println("客户端地址" + ctx.channel().remoteAddress());

            //获取到
            HttpRequest httpRequest = (HttpRequest) msg;
            //获取uri, 过滤指定的资源
            URI uri = new URI(httpRequest.uri());
            if("/favicon.ico".equals(uri.getPath())) {
                System.out.println("请求了 favicon.ico, 不做响应");
                return;
            }
            //回复信息给浏览器 [http协议]

            ByteBuf content = Unpooled.copiedBuffer("hello, 我是服务器", CharsetUtil.UTF_8);

            //构造一个http的相应,即 httpresponse
            FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);

            response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
            response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());

            //将构建好 response返回
            ctx.writeAndFlush(response);

        }
    }



}

三、导出jar包

详细百度

四、启动jar包

五、使用

1、在jar包同级目录下创建files目录。

2、在files目录下新建test文件。

3、test文件中内容:

{"name":"zhangsan"}

4、使用postman发送测试一下吧。

 5、url与文件对应规则

localhost:8083/test 后面的test就是files目录下对应的文件,文件内容会作为返回内容进行返回。