CXYVIP官网源码交易平台_网站源码_商城源码_小程序源码平台-丞旭猿论坛
CXYVIP官网源码交易平台_网站源码_商城源码_小程序源码平台-丞旭猿论坛
CXYVIP官网源码交易平台_网站源码_商城源码_小程序源码平台-丞旭猿论坛

java培训学费多少钱(居然可以这样)java培训机构哪些好,Java,Netty,实现HTTP/HTTPS服务,WEB静态服务器,代码案例分享,

1.java培训班学费一般多少钱

前言HTTP编码器、解码器,Netty为HTTP消息提供了ChannelHandler编码器和解码器:1、HttpRequestEncoder:编码器,用于客户端,向服务器发送请求2、HttpResponseEecoder:编码器,用于服务端,向客户端发送响应。

2.java培训班费用

3、HttpResponseDecoder:解码器,用于客户端,接收来自服务端的请求4、HttpRequestDecoder:解码器,用于服务端,接收来自客户端的请求除独立的编码器、解码器,Netty还提供了其它的编解码器:。

3.java培训费用大概多少

1、HttpClientCodec:用于客户端的编解码器,等效于HttpRequestEncoder和HttpResponseDecoder的组合2、HttpServerCodec:用于服务端的编解码器,等效于HttpRequsetDecoder和HttpResponseEncoder的组合。

4.java培训机构一般要多少钱

3、HttpServerCodec:实现了ChannelInboundHandler和ChannelOutboundHandler,所以具备服务端编码和解码能力。

5.java培训机构费用

HttpObjectAggregator聚合器:上述解码器会将每个HTTP消息中生成多个消息对象,如:HttpRequest、HttpResponse,HttpContent、LastHttpContent,使用聚合器可以将多个消息体合并为一个FullHttpRequest或者FullHttpResponse消息,便于处理请求和响应,Netty提供了HttpObjectAggregator聚合器完成该工作。

6.java培训要多少钱

SslHandlerSslHandler的ChannelHandler实现HTTPS报文加密和解密的功能,其中SslHandler在内部使用SSLEngine来完成实际的工作,SSLEngine的实现可以是JDK的SSLEngine,也可以是Netty 的OpenSslEngine,推荐使用Netty的OpenSslEngine,它性能更好,通过SslHandler进行解密和加密的过程。

7.Java培训机构多少钱

一般情况下,SslHandler在ChannelPipeline中是第一个ChannelHandler,确保只有在所有其它的ChannelHandler将它们的逻辑应用到数据之后,才会进行加密参考:SSL认证,代码案例,单向认证和双向认证的代码分享

8.java培训需要多少钱

实现HTTP服务package com.what21.netty02.demo01.http; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.*;

9.java培训学费是多少

import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel;

10.java培训要多少学费

import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import java.net.InetSocketAddress;

publicclassHttpServer{ private String serverName; private String bindHost; privateint bindPort;

publicHttpServer(String serverName, String bindHost, int bindPort){ this.serverName = serverName;

this.bindHost = bindHost; this.bindPort = bindPort; } publicvoidstart()throws Exception

{ // 用于Acceptor的主”线程池” EventLoopGroup bossEventGroup = new NioEventLoopGroup();

// 初始化==>用于I/O工作的从”线程池” EventLoopGroup workerEventGroup = new NioEventLoopGroup(); try

{ ServerBootstrap serverBootstrap = new ServerBootstrap(); // group方法设置主从线程池

serverBootstrap.group(bossEventGroup, workerEventGroup); // 指定通道channel类型,服务端为:NioServerSocketChannel

serverBootstrap.channel(NioServerSocketChannel.class); serverBootstrap.option(ChannelOption.SO_BACKLOG,

1024); serverBootstrap.childOption(ChannelOption.TCP_NODELAY, true); serverBootstrap.handler(

new LoggingHandler(LogLevel.INFO)); serverBootstrap.childHandler(new HttpServerInitializer()); ChannelFuture bindFuture =

null; if (“0.0.0.0”.equalsIgnoreCase(this.bindHost)) { bindFuture = serverBootstrap.bind(

this.bindPort).sync(); } else { InetSocketAddress bindAddress = new InetSocketAddress(

this.bindHost, this.bindPort); bindFuture = serverBootstrap.bind(bindAddress).sync(); } Channel parentChannel = bindFuture.channel(); bindFuture.addListener(

new ChannelFutureListener() { @OverridepublicvoidoperationComplete(ChannelFuture channelFuture)

throws Exception { if (channelFuture.isSuccess()) { System.out.println(HttpServer.

this.serverName + “,绑定监听成功,” + channelFuture.channel().localAddress()); } else { System.err.println(HttpServer.

this.serverName + “,绑定监听失败!” + channelFuture.cause()); } } }); ChannelFuture closeFuture = bindFuture.channel().closeFuture().sync(); closeFuture.addListener(

new ChannelFutureListener() { @OverridepublicvoidoperationComplete(ChannelFuture channelFuture)

throws Exception { if (channelFuture.isSuccess()) { System.out.println(HttpServer.

this.serverName + “,停止监听成功,” + channelFuture.channel().localAddress()); } else { System.err.println(HttpServer.

this.serverName + “,停止监听失败!” + channelFuture.cause()); } } }); }

catch (Exception e) { e.printStackTrace(); } finally { // 优雅退出,释放”线程池”

if (bossEventGroup != null) { bossEventGroup.shutdownGracefully(); }

if (workerEventGroup != null) { workerEventGroup.shutdownGracefully(); } } }

publicstaticvoidmain(String[] args){ try { new HttpServer(“Web服务器”,”0.0.0.0″,8888

).start(); } catch (Exception e) { e.printStackTrace(); } } }package

com.what21.netty02.demo01.http; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelInitializer;

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

import io.netty.handler.codec.http.HttpServerCodec; import java.util.Map; publicclassHttpServerInitializer

extendsChannelInitializer { @OverrideprotectedvoidinitChannel(SocketChannel channel)

throws Exception { ChannelPipeline pipeline = channel.pipeline(); // http 编解码 pipeline.addLast(

new HttpServerCodec()); // http 消息聚合器 512*1024为接收的最大contentlength pipeline.addLast(“httpAggregator”

, new HttpObjectAggregator(512 * 1024)); // 请求处理器 pipeline.addLast(new HttpRequestHandler());

// 打印一下,每次http请求都打印 Map handlerMap = pipeline.toMap(); for (String key : handlerMap.keySet()) { System.out.println(key +

“=” + handlerMap.get(key)); } } }package com.what21.netty02.demo01.http; import io.netty.buffer.ByteBuf;

import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext;

import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.http.*; import io.netty.util.CharsetUtil;

import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.SocketAddress;

import java.util.HashMap; import java.util.Map; publicclassHttpRequestHandlerextendsSimpleChannelInboundHandler

{ @OverrideprotectedvoidchannelRead0(ChannelHandlerContext ctx, FullHttpRequest httpRequest)

throws Exception { //100 Continueif (HttpUtil.is100ContinueExpected(httpRequest)) { ctx.write(

new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE)); } // 远程地址

SocketAddress clientSocketAddress = ctx.channel().remoteAddress(); String uri = httpRequest.uri();

if (uri.equals(“/”)) { uri = “/index.html”; } String physicalPath = “D:/Users/www/dist/”

; File file = new File(physicalPath + uri); if (!file.exists()) { file =

new File(physicalPath + “/index.html”); } try { byte[] responseBytes = getContent(file.getPath()); ByteBuf content = Unpooled.copiedBuffer(responseBytes);

boolean validateHeaders = false; FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content, validateHeaders); ctx.writeAndFlush(fullHttpResponse).addListener(ChannelFutureListener.CLOSE); }

catch (IOException e) { e.printStackTrace(); HttpResponseStatus responseStatus = HttpResponseStatus.INTERNAL_SERVER_ERROR; FullHttpResponse fullHttpResponse =

new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, responseStatus); ctx.writeAndFlush(fullHttpResponse).addListener(ChannelFutureListener.CLOSE); } }

/** * @param filePath * @return * @throws IOException */privatebyte[] getContent(String filePath)

throws IOException { File file = new File(filePath); long fileSize = file.length();

if (fileSize > Integer.MAX_VALUE) { returnnull; } FileInputStream fi = new

FileInputStream(file); byte[] buffer = newbyte[(int) fileSize]; int offset = 0;

int numRead = 0; while (offset =

0) { offset += numRead; } // 确保所有数据均被读取if (offset != buffer.length) {

thrownew IOException(“Could not completely read file ” + file.getName()); } fi.close();

return buffer; } @OverridepublicvoidchannelReadComplete(ChannelHandlerContext ctx){ ctx.flush(); } }

实现HTTP/HTTPS服务,共用端口package com.what21.netty02.demo01.https; import io.netty.bootstrap.ServerBootstrap;

import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel;

import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import java.net.InetSocketAddress;

publicclassHttpServer{ private String serverName; private String bindHost; privateint bindPort;

publicHttpServer(String serverName, String bindHost, int bindPort){ this.serverName = serverName;

this.bindHost = bindHost; this.bindPort = bindPort; } publicvoidstart()throws Exception

{ // 用于Acceptor的主”线程池” EventLoopGroup bossEventGroup = new NioEventLoopGroup();

// 初始化==>用于I/O工作的从”线程池” EventLoopGroup workerEventGroup = new NioEventLoopGroup(); try

{ ServerBootstrap serverBootstrap = new ServerBootstrap(); // group方法设置主从线程池

serverBootstrap.group(bossEventGroup, workerEventGroup); // 指定通道channel类型,服务端为:NioServerSocketChannel

serverBootstrap.channel(NioServerSocketChannel.class); serverBootstrap.option(ChannelOption.SO_BACKLOG,

1024); serverBootstrap.childOption(ChannelOption.TCP_NODELAY, true); serverBootstrap.handler(

new LoggingHandler(LogLevel.INFO)); serverBootstrap.childHandler(new HttpServerInitializer()); ChannelFuture bindFuture =

null; if (“0.0.0.0”.equalsIgnoreCase(this.bindHost)) { bindFuture = serverBootstrap.bind(

this.bindPort).sync(); } else { InetSocketAddress bindAddress = new InetSocketAddress(

this.bindHost, this.bindPort); bindFuture = serverBootstrap.bind(bindAddress).sync(); } Channel parentChannel = bindFuture.channel(); bindFuture.addListener(

new ChannelFutureListener() { @OverridepublicvoidoperationComplete(ChannelFuture channelFuture)

throws Exception { if (channelFuture.isSuccess()) { System.out.println(HttpServer.

this.serverName + “,绑定监听成功,” + channelFuture.channel().localAddress()); } else { System.err.println(HttpServer.

this.serverName + “,绑定监听失败!” + channelFuture.cause()); } } }); ChannelFuture closeFuture = bindFuture.channel().closeFuture().sync(); closeFuture.addListener(

new ChannelFutureListener() { @OverridepublicvoidoperationComplete(ChannelFuture channelFuture)

throws Exception { if (channelFuture.isSuccess()) { System.out.println(HttpServer.

this.serverName + “,停止监听成功,” + channelFuture.channel().localAddress()); } else { System.err.println(HttpServer.

this.serverName + “,停止监听失败!” + channelFuture.cause()); } } }); }

catch (Exception e) { e.printStackTrace(); } finally { // 优雅退出,释放”线程池”

if (bossEventGroup != null) { bossEventGroup.shutdownGracefully(); }

if (workerEventGroup != null) { workerEventGroup.shutdownGracefully(); } } }

publicstaticvoidmain(String[] args){ try { new HttpServer(“Web服务器(https)”,”0.0.0.0″

,8443).start(); } catch (Exception e) { e.printStackTrace(); } } }package com.what21.netty02.demo01.https;

import com.what21.netty01.demo01.cert2.KeyStoreUtils; import com.what21.netty02.demo01.http.HttpRequestHandler;

import io.netty.buffer.ByteBuf; import io.netty.channel.*; import io.netty.channel.socket.SocketChannel;

import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpServerCodec;

import io.netty.handler.ssl.SslContext; import io.netty.handler.ssl.SslContextBuilder; import io.netty.handler.ssl.SslHandler;

import java.security.PrivateKey; import java.security.cert.Certificate; import java.security.cert.X509Certificate;

import java.util.Map; publicclassHttpServerInitializerextendsChannelInitializer { @

Override protected void initChannel(SocketChannel channel) throwsException { ChannelPipeline

pipeline = channel.pipeline(); //RuleBasedIpFilter ruleBasedIpFilter = new RuleBasedIpFilter(rule1, rejectAll);

//channel.pipeline().addLast(“ipFilter”, ruleBasedIpFilter);// http 编解码 channel.pipeline().addLast(

“httpServerCodec”, new HttpServerCodec()); // http 消息聚合器 512*1024为接收的最大contentlength channel.pipeline().addLast(

“httpObjectAggregator”, new HttpObjectAggregator(512 * 1024)); // 请求处理器 channel.pipeline().addLast(

“serverHandler”, new HttpRequestHandler()); // HTTPS支持 channel.pipeline().addFirst(new

ChannelInboundHandlerAdapter() { @Overridepublic void channelRead(ChannelHandlerContext ctx,

Object msg) throwsException { if (((ByteBuf) msg).getByte(0) == 22) {

KeyStoreUtils.KeyStoreEntry keyStoreEntry = KeyStoreUtils.readToKeyStoreEntry();

PrivateKey privateKey = keyStoreEntry.getPrivateKey(); Certificate certificate = keyStoreEntry.getCertificate();

SslContext sslContext = SslContextBuilder.forServer(privateKey, (X509Certificate) certificate).build();

SslHandler sslHandler = sslContext.newHandler(channel.alloc()); // 将处理 https 的处理程序添加至 HttpServerCodec 之前

ctx.pipeline().addBefore(“httpServerCodec”, “sslHandler”, sslHandler); } ctx.pipeline().remove(this);

super.channelRead(ctx, msg); } }); // 打印一下,每次http请求都打印Map

> handlerMap = pipeline.toMap(); for (String key : handlerMap.keySet()) { System.out.

println(key + “=” + handlerMap.get(key)); } } }package com.what21.netty02.demo01.https;

import lombok.Data; import sun.misc.BASE64Encoder; import java.io.FileInputStream; import java.security.KeyStore;

import java.security.PrivateKey; import java.security.PublicKey; import java.security.cert.Certificate;

publicclass KeyStoreUtils { /** * @param storePath 秘钥库文件路径 * @param storePasswd 秘钥库密码 * @param alias 证书别名 * @param trustPasswd 证书密码 * @return * @throws Exception */

publicstatic KeyStoreEntry readToKeyStoreEntry(String storePath, String storePasswd, String alias, String

trustPasswd) throws Exception { KeyStore keyStore = KeyStore.getInstance(“JKS”); keyStore.load(

new FileInputStream(storePath), storePasswd.toCharArray()); PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, trustPasswd.toCharArray()); PublicKey publicKey = keyStore.getCertificate(alias).getPublicKey(); Certificate certificate = keyStore.getCertificate(alias); BASE64Encoder encoder =

new BASE64Encoder(); String privateKeyEncoded = encoder.encode(privateKey.getEncoded());

String publicKeyEncoded = encoder.encode(publicKey.getEncoded()); KeyStoreEntry keyStoreEntry =

new KeyStoreEntry(); keyStoreEntry.setKeyStore(keyStore); keyStoreEntry.setPrivateKey(privateKey); keyStoreEntry.setPublicKey(publicKey); keyStoreEntry.setCertificate(certificate); keyStoreEntry.setPrivateKeyEncoded(privateKeyEncoded); keyStoreEntry.setPublicKeyEncoded(publicKeyEncoded);

return keyStoreEntry; } @Datapublicstaticclass KeyStoreEntry { private KeyStore keyStore;

private PrivateKey privateKey; private PublicKey publicKey; private Certificate certificate;

privateString privateKeyEncoded; privateString publicKeyEncoded; } @Datapublicstatic

class KeyStoreParam { publicString storePath = “d:/server.jks”; publicString storePasswd =

“123456”; publicString alias = “server”; publicString trustPasswd = “123456”; }

/** * @param keyStoreParam * @return */publicstatic KeyStoreEntry readToKeyStoreEntry(KeyStoreParam keyStoreParam) {

try { return readToKeyStoreEntry(keyStoreParam.getStorePath(), keyStoreParam.getStorePasswd(), keyStoreParam.getAlias(), keyStoreParam.getTrustPasswd()); }

catch (Exception e) { e.printStackTrace(); } returnnull; } /** * @return */

publicstatic KeyStoreEntry readToKeyStoreEntry() { String storePath = “d:/server.jks”;

String storePasswd = “123456”; String alias = “server”; String trustPasswd = “123456”

; try { return readToKeyStoreEntry(storePath, storePasswd, alias, trustPasswd); }

catch (Exception e) { e.printStackTrace(); } returnnull; } }package com.what21.netty02.demo01.https;

import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFutureListener;

import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import

io.netty.handler.codec.http.*; import java.io.File; import java.io.FileInputStream; import java.io.IOException;

import java.net.SocketAddress; publicclassHttpRequestHandlerextendsSimpleChannelInboundHandler

> { @OverrideprotectedvoidchannelRead0(ChannelHandlerContext ctx, FullHttpRequest httpRequest)throws

Exception { //100 Continueif (HttpUtil.is100ContinueExpected(httpRequest)) { ctx.write(

new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE)); } // 远程地址

SocketAddress clientSocketAddress = ctx.channel().remoteAddress(); String uri = httpRequest.uri();

if (uri.equals(“/”)) { uri = “/index.html”; } String physicalPath = “D:/Users/www/dist/”

; File file = new File(physicalPath + uri); if (!file.exists()) { file =

new File(physicalPath + “/index.html”); } try { byte[] responseBytes = getContent(file.getPath()); ByteBuf content = Unpooled.copiedBuffer(responseBytes);

boolean validateHeaders = false; FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content, validateHeaders); ctx.writeAndFlush(fullHttpResponse).addListener(ChannelFutureListener.CLOSE); }

catch (IOException e) { e.printStackTrace(); HttpResponseStatus responseStatus = HttpResponseStatus.INTERNAL_SERVER_ERROR; FullHttpResponse fullHttpResponse =

new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, responseStatus); ctx.writeAndFlush(fullHttpResponse).addListener(ChannelFutureListener.CLOSE); } }

/** * @param filePath * @return * @throws IOException */privatebyte[] getContent(String filePath)

throws IOException { File file = new File(filePath); long fileSize = file.length();

if (fileSize > Integer.MAX_VALUE) { returnnull; } FileInputStream fi = new

FileInputStream(file); byte[] buffer = newbyte[(int) fileSize]; int offset = 0;

int numRead = 0; while (offset =

0) { offset += numRead; } // 确保所有数据均被读取if (offset != buffer.length) {

thrownew IOException(“Could not completely read file ” + file.getName()); } fi.close();

return buffer; } @OverridepublicvoidchannelReadComplete(ChannelHandlerContext ctx){ ctx.flush(); } }

© 版权声明
THE END
喜欢就支持一下吧
点赞0赞赏 分享
相关推荐
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容