zoukankan      html  css  js  c++  java
  • mysql表utf-8 字符串入库编码异常

    分析:http://www.myexception.cn/mysql/639943.html

    解决方法:http://blog.sina.com.cn/s/blog_3f78232201011o26.html

    public class Utf8Filter {
    
     
    
    public static void main(String[] args) throws Exception {
    
     
    
    System.out.println(filterMt4BytesUtf8(new String(new byte[] { (byte) 0x41 }, "utf-8")));
    
    System.out.println(filterMt4BytesUtf8(new String(new byte[] { (byte) 0xf3, (byte) 0xb7,
    
    (byte) 0xa2, (byte) 0xbe }, "utf-8")));
    
    System.out.println(filterMt4BytesUtf8(new String(new byte[] { (byte) 0x41, (byte) 0xf3,
    
    (byte) 0xb7, (byte) 0xa2, (byte) 0xbe }, "utf-8")));
    
    System.out.println(filterMt4BytesUtf8(new String(new byte[] { (byte) 0xf3, (byte) 0xb7,
    
    (byte) 0xa2, (byte) 0xbe, (byte) 0x41 }, "utf-8")));
    
    System.out.println(filterMt4BytesUtf8(new String(new byte[] { (byte) 0x41, (byte) 0xf3,
    
    (byte) 0xb7, (byte) 0xa2, (byte) 0xbe, (byte) 0x41 }, "utf-8")));
    
    System.out.println(filterMt4BytesUtf8(new String(new byte[] { (byte) 0xf3, (byte) 0xb7,
    
    (byte) 0xa2, (byte) 0xbe, (byte) 0xf3, (byte) 0xb7, (byte) 0xa2, (byte) 0xbe,
    
    (byte) 0x41 }, "utf-8")));
    
    System.out.println(filterMt4BytesUtf8(new String(new byte[] { (byte) 0x41, (byte) 0xf3,
    
    (byte) 0xb7, (byte) 0xa2, (byte) 0xbe, (byte) 0xf3, (byte) 0xb7, (byte) 0xa2,
    
    (byte) 0xbe }, "utf-8")));
    
    System.out.println(filterMt4BytesUtf8(new String(new byte[] { (byte) 0x41, (byte) 0xf3,
    
    (byte) 0xb7, (byte) 0xa2, (byte) 0xbe, (byte) 0xf3, (byte) 0xb7, (byte) 0xa2,
    
    (byte) 0xbe, (byte) 0x41 }, "utf-8")));
    
    }
    
     
    
    private static String filterMt4BytesUtf8(String input) {
    
    if (StringUtils.isBlank(input))
    
    return input;
    
     
    
    ByteArrayOutputStream is = new ByteArrayOutputStream();
    
    byte[] bytes = input.getBytes();
    
     
    
    outter: for (int i = 0, length = bytes.length; i < length; i++) {
    
    byte b = bytes[i];
    
    while (((b & 0xF8) == 0xF0) || ((b & 0xFc) == 0xF8) || ((b & 0xFe) == 0xFc)) {
    
    // found a byte of 4, 5, 6 UTF-8 bytes for a character
    
    // eat the following bytes of this character
    
    while (++i < length && ((b = bytes[i]) & 0xC0) == 0x80)
    
    ;
    
    // insert a replacement character
    
    is.write((byte) 0xEF);
    
    is.write((byte) 0xBF);
    
    is.write((byte) 0xBD);
    
    if (i >= length) {
    
    // we reach the end of byte array
    
    break outter;
    
    }
    
    }
    
    // found byte of 1, 2, 3 UTF-8 bytes for a character
    
    is.write(b);
    
    }
    
    try {
    
    return new String(is.toByteArray(), "utf-8");
    
    } catch (UnsupportedEncodingException e) {
    
    logger.error("filter more than 4 bytes utf-8 character failed!", e);
    
    }
    
    return input;
    
    }
    
    }
  • 相关阅读:
    对于基础资料的关联操作
    单据关联关系记录
    单据转换插件中新增行
    APK签名校验绕过
    android 安全需要关注
    安卓从业者应该关注:Android 6.0的运行时权限
    让阿里云的Centos,PHP组件 ImageMagick支持png和jpeg格式
    cocos2d-x 常规库的图文件配置
    cocos2d-x 添加 libLocalStorage 库...
    cocos2d-x3.9 默认是 gnustl_static 配置,但是 这个库缺少c++的基础功能... c++_static 功能全面些
  • 原文地址:https://www.cnblogs.com/sunxucool/p/3290728.html
Copyright © 2011-2022 走看看