Question

前段时间,工作的时候。测试数据,有位同事测试老是失败。经过调查定位,发现是Redis内存储的数据格式不对。比较奇怪的是,远程SSH登陆到同一台机器操作,由于客户机的编码不一致,启动的时候会导致乱码的情况。特别是英文操作系统存储中文字符的时候。


Solution

编码的问题常常会遇到。在Java内主要通过如下的操作进行转码

### 方法1
Byte[] bytes = "hello".getBytes("ISO8859-1");//""里面的参数为需要转化的编码,一般是ISO8859-1
String str = new String(bytes,"utf-8");//转化为utf-8编码 ### 方法2
String str = new String("hello","utf-8");//转化为utf-8编码 

PS: 或者可以使用之前说过的,序列化和反序列化的方法进行解决。

public class Stuent implement Serialize{private String name;public String getName(){}public String setName(String name){}
}public class Demo{public static void main(String []agrgs){// Jedis attributeprivate static String address = "192.168.100.76";private static int port = 6379;private static int timeout = 10000;private static String auth = "admin";// JedisConfig attributeprivate static int max_active = 1024;private static int max_idle = 200;private static int max_wait = 10000;private static boolean test_on_borrow = true;private static JedisPool jedisPool = null;JedisPoolConfig config = new JedisPoolConfig();config.setMaxIdle(max_idle);config.setMaxWaitMillis(max_wait);config.setTestOnBorrow(test_on_borrow);config.setMaxTotal(max_active);jedisPool = new JedisPool(config,address,port,timeout,auth);Jedis jedis = config.getJedis();Stuent student = new Student();student.setName("Bruce");// 转换为Json格式存储jedis.set(key,JSONObject.toJSONString(person));// 获取对象 转换为需要的类型Stuent cacheObj = JSONObject.parseObject(jedis.get(key), Person.class);}
}

Others

//对象序列化为字符串public static String objectSerialiable(Object obj){String serStr = null;try {ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();  ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);  objectOutputStream.writeObject(obj);    serStr = byteArrayOutputStream.toString("ISO-8859-1");  serStr = java.net.URLEncoder.encode(serStr, "UTF-8");  objectOutputStream.close();  byteArrayOutputStream.close();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return serStr;}//字符串反序列化为对象public static Object objectDeserialization(String serStr){Object newObj = null;try {String redStr = java.net.URLDecoder.decode(serStr, "UTF-8");  ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(redStr.getBytes("ISO-8859-1"));  ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);   newObj = objectInputStream.readObject();objectInputStream.close();  byteArrayInputStream.close();} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return newObj;}### 方法二String[] input = {  "### \u8bbe\u7f6eLogger\u8f93\u51fa\u7ea7\u522b\u548c\u8f93\u51fa\u76ee\u7684\u5730 ###",  "### \u628a\u65e5\u5fd7\u4fe1\u606f\u8f93\u51fa\u5230\u63a7\u5236\u53f0 ###",  "### \u628a\u65e5\u5fd7\u4fe1\u606f\u8f93\u51fa\u5230\u6587\u4ef6\uff1aheros.log ###",  "###\u663e\u793aSQL\u8bed\u53e5\u90e8\u5206"  };  String output = new String(input.getBytes("iso-8859-1"),"utf-8");  System.out.println("转换后得结果:"+output);  

Reference

[1]. (博客园)解决Jedis数据读取乱码问题
[2]. (ITEYE)redis 中文乱码
[3]. 乱码转中文输出(ISO-8859-1 to UTF-8)
[4]. (博客园)Jedis和JAVA对象的序列化和反序列化的使用
[5]. (CSDN)Redis中文乱码问题的解决
[6]. 添加到redis里的key乱码
[7]. (原理)Redis数据编码方式详解
[8]. (原理)Redis Java客户端Jredis

Redis 乱码解决与思考相关推荐

  1. 基于 Redis 实现分布式锁思考

    以下文章来源方志朋的博客,回复"666"获面试宝典 来源:blog.csdn.net/xuan_lu/article/details/111600302 分布式锁 基于redis实 ...

  2. log中文乱码 springboot_springboot + shiro 权限注解、统一异常处理、请求乱码解决

    springboot + shiro 权限注解.统一异常处理.请求乱码解决 前篇 后台权限管理系统 20200808新版本更新 版本升级及内容优化版本,改动内容: 版本更新,springboot从1. ...

  3. 基于redis实现分布式锁思考

    分布式锁 基于redis实现分布式锁思考几个问题??? synchronized锁为什么不能应用于分布式锁? synchronized虽然能够解决同步问题,但是每次只有一个线程访问,并且synchro ...

  4. java中文乱码解决之道(二)—–字符编码详解:基础知识 + ASCII + GB**

    原文出处:http://cmsblogs.com/?p=1412 在上篇博文(java中文乱码解决之道(一)-–认识字符集)中,LZ简单介绍了主流的字符编码,对各种编码都是点到为止,以下LZ将详细阐述 ...

  5. pycharm 运行控制台中文乱码解决办法

    pycharm 运行控制台中文乱码解决办法

  6. vs code中文乱码解决方法

    修改 1.(安装方法) 2.显示终端输入数据输出结果(完美解决) 3.修改部分:中文乱码解决方法 第一步: 第二步: 1.(安装方法) 转载于: https:blog.csdn.net/qq_4304 ...

  7. Zabbix 中文乱码解决

    Zabbix 中文乱码解决 1.在本地的C:\Windows\Fonts下选择自己喜欢的字体,此处选择黑体(我只测试过宋体,楷体,黑体,幼圆其他中文字体应该都支持) 在C:\Windows\Fonts ...

  8. Ubuntu10.04下gFtp2.0.19 远程客户端中文乱码解决方法

    Ubuntu10.04下gFtp2.0.19 远程客户端中文乱码解决方法如下: 1.打开"gFTP-FTP-属性-常规-远程字符集": 2.将远程字符集设置为:gb2312,gbk ...

  9. 【数据库】MySQL乱码解决(存入数据乱码和查询输出乱码)

    一般在安装和设置MySQL的时候会选择将其编码格式设置为UTF8,以保证数据库在不用语言环境下的适应性.但是一般在Win下使用的时候,本机编码集却是GBK或者GB2312.这样在Win下的GBK编码输 ...

最新文章

  1. Linux环境下的堆栈--调试C程序
  2. 动态规划解决方案最长公共子序列问题(开启)
  3. iframe异步加载技术及性能
  4. MySQL使用小技巧(二)——MySQL忘记密码怎么办
  5. ipython 安装_IPYTHON安装.DOC
  6. ​【预测模型】基于粒子群算法优化最小二乘支持向量机实现数据分类matlab代码
  7. “程序员年薪50万到底有多累、多辛苦?”,句句扎心
  8. genymotion配置android模拟器
  9. 删除Mac右上角可恶的状态栏图标
  10. sortby降序java_lodash多列sortBy降序
  11. 正弦余弦编码器与增量编码器的区别
  12. 300套PPT模板+实习僧20套精选简历+其他各种素材PPT模板(免费分享)
  13. 公众号回调,突破每月只能修改三次的限制
  14. cifar 10 最高正确率
  15. JEECG容器化部署:Alpine镜像方式
  16. 持币大户谈PRS投资策略
  17. 概率论(三)- 全概公式逆概公式(贝叶斯公式)
  18. 零基础学Python--------入门篇 第1章 初始Python
  19. Apriori算法和FP-Tree算法简介
  20. 小鹅通课程下载(一)

热门文章

  1. 商宝项目服务器,可照搬实施的商超高可用方案:proxmox + haproxy 等
  2. 计算机管理如何格式化u盘,如何格式化U盘【图文教程】
  3. 电子设计竞赛学习msp430单片机(msp430g2553,msp430f5529,tmec123G)
  4. Introdution(前言)
  5. [python爬虫]暴力破解Wifi密码
  6. 控制台输出——键盘图形
  7. Android studio File Explorer sdcard文件怎么访问
  8. 垃圾大学,想自学 Java 可以吗?难吗?毕业后能找到一份 6k左右的工作吗?
  9. 【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)
  10. L0 Norm 、L1 Norm 和 L2 Norm 的简单理解