之前讲了怎么把数据导入到zookeeper(见zookeeper事件监听的importData方法),虽然本机win10的zookeeper展示没问题,但到了linux上就出现乱码了:
<<<2018-09-29 09:14:54,905| DEBUG | ZKUtil.getServiceAccessCompany() invoke getServiceAccessCompany !{"11111":{"id":"11111","name":"�㶫ʡ��˾","type":"11
1","spId":"AAAAA","key":"BBBBB","remark":"���ǶԵ�"},"22222":{"id":"22222","name":"����ʡ��˾","type":"22","spId":"CCCCC","key"::
"DDDDD","remark":"���ݵ���zk��"},"33333":{"id":"33333","name":"����","type":"33","spId":"EEEEE","key":"FFFFF","remark":"����"}}
| com.wlf.utils.ZKUtil>>>
想了一下,取数据时用了utf-8解码了,见getData方法:
String jsonData = new String(data, "utf-8");
那是否导入时编码有问题呢?因为数据是作为json格式导入到zk的,json说白了就是字符串,那么是否我要指定一下编码格式?试了下:
companyMap.put(sac.getId(), getJson(sac).getBytes("utf-8"));
结果乱码依旧。那么我只针对中文的字段来指定编码格式呢?再试下:
sac.setName(new String("广东省公司".getBytes("utf-8"), "utf-8"));
以上通过指定字符串的编码和解码均为utf-8,应该能保证该字段无乱码,但从zk上取到的依然乱码。
没辙,把两者结合起来,都指定,这次终于可以了:
<<<2018-09-29 11:58:02,553| DEBUG | ZKUtil.getServiceAccessCompany() invoke getCompany !{"11111":{"id":"11111","name":"广东省公司","type":"11","spId":"AAAAA","key":"BBBBB","remark":"好了"},"22222":{"id":"22222","name":"江苏省公司","type":"22","spId":"CCCCC","key":"DDDDD","remark":"数据导入zk点"},"33333":{"id":"33333","name":"北京","type":"33","spId":"EEEEE","key":"FFFFF","remark":"你是对的"}} | com.wlf.utils.ZKUtil>>>