Redis 乱码解决与思考

版权声明:欢迎转载,转载请说明出处https://csdn.yanxml.com。大数据Github项目地址https://github.com/SeanYanxml/bigdata。 https://blog.csdn.net/u010416101/article/details/80753980

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 attribute
    private static String address = "192.168.100.76";
    private static int port = 6379;
    private static int timeout = 10000;
    private static String auth = "admin";

    // JedisConfig attribute
    private 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 block
            e.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 block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.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

没有更多推荐了,返回首页