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);
}
推荐答案
String
到 byte[]
的转换过程中需要指定字符集,反之亦然.
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.
相关文章