From b84adddd29314b25a8e8de1509bc1767dc4784bb Mon Sep 17 00:00:00 2001 From: seagull Date: Sat, 12 Dec 2020 15:39:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96Netty=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/Resources/sys_config.properties | 4 +++ .../java/luckyclient/netty/NettyClient.java | 27 ++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/Resources/sys_config.properties b/src/main/Resources/sys_config.properties index b3fb49b..58c3a21 100644 --- a/src/main/Resources/sys_config.properties +++ b/src/main/Resources/sys_config.properties @@ -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 diff --git a/src/main/java/luckyclient/netty/NettyClient.java b/src/main/java/luckyclient/netty/NettyClient.java index d6cac27..19b57e8 100644 --- a/src/main/java/luckyclient/netty/NettyClient.java +++ b/src/main/java/luckyclient/netty/NettyClient.java @@ -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; + } + } }