`
VIP庚
  • 浏览: 220653 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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); 
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics