Java BASE64 utf8 字符串解码 [英] Java BASE64 utf8 string decoding

查看:117
本文介绍了Java BASE64 utf8 字符串解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 org.apache.commons.codec.binary.Base64 解码字符串,即 utf8.有时我会得到 base64 编码的字符串,解码后看起来像 ^@k��@@.如何检查base64是否正确或解码的utf8字符串是否为有效的utf8字符串?

I'm using org.apache.commons.codec.binary.Base64 do decode string which is utf8. Sometimes I get base64 encoded string which after decode looks like for example ^@k��@@. How can I check if base64 is correct or if decoded utf8 string is valid utf8 string?

澄清一下.我正在使用

public static String base64Decode(String str) {
    try {
        return new String(base64Decode(str.getBytes(Constants.UTF_8)), Constants.UTF_8);
    } catch (UnsupportedEncodingException e) {
         ...
    }
}

public static byte[] base64Decode(byte[] byteArray) {
    return Base64.decodeBase64(byteArray);
}

推荐答案

Stringbyte[] 的转换过程中需要指定字符集,反之亦然.

You should specify the charset during converting String to byte[] and vice versa.

byte[] bytes = string.getBytes("UTF-8");
// feed bytes to Base64

// get bytes from Base64
String string = new String(bytes, "UTF-8");

否则将使用平台默认编码,它本身不一定是 UTF-8.

Otherwise the platform default encoding will be used which is not necessarily UTF-8 per se.

这篇关于Java BASE64 utf8 字符串解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆