netty中 解决服务器到客户端传输数据 中文乱码问题

  最近在开发一个 基于netty框架的项目  服务端是用eclipse编程开发的,客户端使用android studio 开发的
由于2个编码格式不一样 所以造成了消息传递之间的 中文乱码
解决办法
服务端在initChannel中声明:
//编码格式
					arg0.pipeline().addLast(new StringEncoder(Charset.forName("GBK")));
					//解码格式
					arg0.pipeline().addLast(new StringDecoder(Charset.forName("UTF-8")));


客户端在initChannel中声明:
 ch.pipeline().addLast(new StringEncoder(Charset.forName("UTF-8")));
                            ch.pipeline().addLast(new StringDecoder(Charset.forName("GBK")));


搞定!


这有一个编码转换的方法
  String strGBK = URLEncoder.encode(str, "GBK");  
            System.out.println(strGBK);  
            String strUTF8 = URLDecoder.decode(str, "UTF-8");  
            System.out.println(strUTF8); 
 

猜你喜欢

转载自592713711.iteye.com/blog/2260149