修复乱码问题

This commit is contained in:
fengj 2020-03-06 11:12:06 +08:00
parent f82667d909
commit 8ecc621711
2 changed files with 4 additions and 3 deletions

View File

@ -63,7 +63,7 @@ public class ClientHandler extends ChannelHandlerAdapter {
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws UnsupportedEncodingException, InterruptedException { public void channelRead(ChannelHandlerContext ctx, Object msg) throws UnsupportedEncodingException, InterruptedException {
//服务端消息处理,如果接收到测试任务方法则直接产生一个http请求并发送请求到本地 //服务端消息处理,如果接收到测试任务方法则直接产生一个http请求并发送请求到本地
String jsonStr = new String(msg.toString().getBytes(), "UTF-8"); String jsonStr = msg.toString();
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
try { try {
json = JSON.parseObject(jsonStr); json = JSON.parseObject(jsonStr);

View File

@ -1,6 +1,7 @@
package luckyclient.netty; package luckyclient.netty;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -52,8 +53,8 @@ public class NettyClient {
ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes()); ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
ChannelPipeline p = ch.pipeline(); ChannelPipeline p = ch.pipeline();
p.addLast(new DelimiterBasedFrameDecoder(1024, delimiter)); p.addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
p.addLast("decoder", new StringDecoder()); p.addLast("decoder", new StringDecoder(Charset.forName("GBK")));
p.addLast("encoder", new StringEncoder()); p.addLast("encoder", new StringEncoder(Charset.forName("UTF-8")));
p.addLast(new IdleStateHandler(1,0,0,TimeUnit.SECONDS)); p.addLast(new IdleStateHandler(1,0,0,TimeUnit.SECONDS));
p.addLast(clientHandler); p.addLast(clientHandler);
} }