【问题标题】:Convert Blob to String with Special Character将 Blob 转换为带有特殊字符的字符串
【发布时间】:2019-09-10 14:12:49
【问题描述】:

我正在尝试将 blob 变量转换为字符串。在 DB 级别,变量包含带有意大利字符的 XML 文件,如

(è, ò, à e ...)

我已经使用的代码如下:

   byte[] result = blob.getBytes(1, (int) blob.length());
   String b = new String(result);
   System.out.println(b);

输出无法解析特殊字符。我猜这是因为将这些字母转换为字节会消除它们的实际值。

【问题讨论】:

  • 数据库列是如何定义的?如果是varchartext,您可能需要Clob 而不是Blob。

标签: java blob data-conversion


【解决方案1】:

意大利语:

String b = new String(result, "UTF-8"); // All languages of the world
String b = new String(result, "Windows-1252"); // Some European languages

并且blob必须已经被相应地填充了:

byte[] bytes = b.getBytes("UTF-8");
byte[] bytes = b.getBytes("Windows-1252");

选择 UTF-8 - 然后你就有表情符号了。

请注意 System.out 使用本地编码,因此在希腊电脑上可能无法正确显示文本。

【讨论】:

  • Blob 没有任何采用字符集的方法。
  • @VGR 上面我只使用从 blob 中获取的`byte[](result`、bytes 存储到 blob) 和 String (b)。当然,带有 InputStreamReader/OutputStreamWriter 的 InputStream/OutputStream 是处理较大 blob 的其他方法。
【解决方案2】:

这实际上取决于您如何将 String 转换为非 ASCII 字符(特殊字符)。在将 blob 转换为 string 时,您需要指定您使用的 encoding

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-10
    • 2013-03-08
    • 2016-01-30
    • 1970-01-01
    • 2015-02-20
    • 2019-02-17
    • 2016-06-09
    • 1970-01-01
    相关资源
    最近更新 更多