java使用redis时,中文乱码问题
2016-12-01 14:54
465 查看
今天测试项目时发现页面有些数据乱码了,检查了一下发现数据存入redis还是中文,取出来就乱码了T T
代码:
这里查了一下String的getBytes()方法的源码,如果你不设置编码集,默认的编码集是 “ISO-8859-1”,源码:
我们只需要在设置key对应的value时设置对应的字符集与取出来的一直即可,在此我们设置为 “utf-8”,代码如下:
查阅资料时,发现直接操作redis也会有乱码的情况,记录一下
如何在get时取到它的中文呢?只需要在redis-cli 后面加上 –raw
感谢 http://blog.csdn.net/cwallow/article/details/8690309
代码:
/** 存入相应的key和value,并设置其生命周期(单位秒) * @param key * @param value * @param liveTime * @throws Exception */ public void set(String key, String value, long liveTime) throws Exception { this.set(key.getBytes(), value.getBytes(), liveTime); }
/** 根据key取出对应的数据 * @param key * @return */ public String get(final String key) { return redisTemplate.execute(new RedisCallback<String>() { public String doInRedis(RedisConnection connection) throws DataAccessException { try { byte[] b = connection.get(key.getBytes()); if(b==null){ return null; } return new String(b, "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return ""; } }); }
这里查了一下String的getBytes()方法的源码,如果你不设置编码集,默认的编码集是 “ISO-8859-1”,源码:
/** * Encodes this {@code String} into a sequence of bytes using the * platform's default charset, storing the result into a new byte array. * * <p> The behavior of this method when this string cannot be encoded in * the default charset is unspecified. The {@link * java.nio.charset.CharsetEncoder} class should be used when more control * over the encoding process is required. * * @return The resultant byte array * * @since JDK1.1 */ public byte[] getBytes() { return StringCoding.encode(value, 0, value.length); } static byte[] encode(char[] ca, int off, int len) { String csn = Charset.defaultCharset().name(); try { // use charset name encode() variant which provides caching. return encode(csn, ca, off, len); } catch (UnsupportedEncodingException x) { warnUnsupportedCharset(csn); } try { return encode("ISO-8859-1", ca, off, len); } catch (UnsupportedEncodingException x) { // If this code is hit during VM initialization, MessageUtils is // the only way we will be able to get any kind of error message. MessageUtils.err("ISO-8859-1 charset not available: " + x.toString()); // If we can not find ISO-8859-1 (a required encoding) then things // are seriously wrong with the installation. System.exit(1); return null; } } static byte[] encode(String charsetName, char[] ca, int off, int len) throws UnsupportedEncodingException { StringEncoder se = deref(encoder); String csn = (charsetName == null) ? "ISO-8859-1" : charsetName; if ((se == null) || !(csn.equals(se.requestedCharsetName()) || csn.equals(se.charsetName()))) { se = null; try { Charset cs = lookupCharset(csn); if (cs != null) se = new StringEncoder(cs, csn); } catch (IllegalCharsetNameException x) {} if (se == null) throw new UnsupportedEncodingException (csn); set(encoder, se); } return se.encode(ca, off, len); }
我们只需要在设置key对应的value时设置对应的字符集与取出来的一直即可,在此我们设置为 “utf-8”,代码如下:
/** * @param key * @param value * @param liveTime * @throws Exception */ public void set(String key, String value, long liveTime) throws Exception { this.set(key.getBytes("utf-8"), value.getBytes("utf-8"), liveTime); }
查阅资料时,发现直接操作redis也会有乱码的情况,记录一下
redis> set test "我们" OK redis> get test "\xe6\x88\x91\xe4\xbb\xac"
如何在get时取到它的中文呢?只需要在redis-cli 后面加上 –raw
redis> get test "我们"
感谢 http://blog.csdn.net/cwallow/article/details/8690309
相关文章推荐
- JAVA ,SSH中文及其乱码问题的解决 6大配置点 使用UTF-8编码
- Java web开发中使用get提交表单时的中文乱码问题
- java如何发送邮件以及使用Velocity模板解决模板文件中文乱码的问题
- 使用Java 导入Zip文件遇到中文命名乱码问题
- JAVA ,SSH中文及其乱码问题的解决 6大配置点 使用UTF-8编码
- Java使用Velocity模板发送HTML格式邮件并解决中文乱码问题
- java后台请求使用URLEncoder.encode给url追加中文乱码问题
- 使用Java进行微信开发5 - 向用户发消息(无中文乱码问题)
- Java使用SQL Server数据库时注册或添加中文数据时乱码问题
- Java使用System.out.print()输出中文成乱码的问题
- php使用NuSoap调用java/C# webservice的中文乱码问题
- javaweb开发中的中文乱码问题,使用Filter过滤器解决
- Java学习笔记37:Spring 使用 @ResponseBody 返回中文乱码问题解决
- 关于spring中使用redis中文乱码问题(redistemplate)
- OSX下terminal终端中使用javac编译java时中文乱码的问题解决
- 使用过滤器Filter解决Java系统中中文乱码的问题
- 关于使用java自带ZipOutputStream压缩文件名为中文的乱码问题
- Java 使用 Velocity 发送邮件中文内容乱码问题的解决办法
- java web 在jsp中使用include指令导入带中文的HTML文件乱码问题
- 使用java.nio及其子包中的类SocketChanel完成如下功能:读取www.baidu.com等网页的内容,打印到控制台,避免中文乱码问题。