优化Netty编码配置化

This commit is contained in:
seagull 2020-12-12 15:39:49 +08:00
parent da07b69e99
commit b84adddd29
2 changed files with 28 additions and 3 deletions

View File

@ -10,6 +10,10 @@ server.web.path=/
netty.model=true
netty.server.port=7070
netty.host=
# netty±àÂë¸ñʽ
netty.encoder=GBK
# netty½âÂë¸ñʽ
netty.decoder=UTF_8
#================================ 邮件=======================================
#smtp邮件IP 例smtp.qq.com
mail.smtp.ip=smtp.qq.com

View File

@ -21,8 +21,9 @@ import java.util.concurrent.TimeUnit;
public class NettyClient {
private static final String NETTY_SERVER_IP= SysConfig.getConfiguration().getProperty("server.web.ip");
private static final int NETTY_SERVER_PORT=Integer.parseInt(SysConfig.getConfiguration().getProperty("netty.server.port"));
private static final String NETTY_ENCODER= SysConfig.getConfiguration().getProperty("netty.encoder");
private static final String NETTY_DECODER= SysConfig.getConfiguration().getProperty("netty.decoder");
protected static Channel channel;
@ -42,8 +43,8 @@ public class NettyClient {
ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
ChannelPipeline p = ch.pipeline();
p.addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
p.addLast("decoder", new StringDecoder(StandardCharsets.UTF_8));
p.addLast("encoder", new StringEncoder(Charset.forName("GBK")));
p.addLast("decoder", new StringDecoder(charConvert(NETTY_DECODER)));
p.addLast("encoder", new StringEncoder(charConvert(NETTY_ENCODER)));
p.addLast(new IdleStateHandler(1,0,0,TimeUnit.SECONDS));
p.addLast(clientHandler);
}
@ -68,4 +69,24 @@ public class NettyClient {
}
});
}
private static Charset charConvert(String strChar){
if("utf-8".equals(strChar.toLowerCase())){
return StandardCharsets.UTF_8;
}else if("iso_8859_1".equals(strChar.toLowerCase())){
return StandardCharsets.ISO_8859_1;
}else if("us_ascii".equals(strChar.toLowerCase())){
return StandardCharsets.US_ASCII;
}else if("utf_16".equals(strChar.toLowerCase())){
return StandardCharsets.UTF_16;
}else if("utf_16be".equals(strChar.toLowerCase())){
return StandardCharsets.UTF_16BE;
}else if("utf_16le".equals(strChar.toLowerCase())){
return StandardCharsets.UTF_16LE;
}else if("gbk".equals(strChar.toLowerCase())){
return Charset.forName("GBK");
}else{
return StandardCharsets.UTF_8;
}
}
}