Java Can';t使Base64.decodeBase64正常工作(Commons编解码器)

Java Can';t使Base64.decodeBase64正常工作(Commons编解码器),java,base64,apache-commons-codec,Java,Base64,Apache Commons Codec,我不知道这里出了什么问题。我已经尝试了所有可能的组合。设置字符集,toString,no toString。编码工作得很好。我可以把这个数字输入网络解码器,每次都能得到正确的值。就是没法让它工作 输出: String encode = Base64.encodeBase64String("Hello".getBytes()); System.out.println(encode); byte[] decode = Base64.decodeBase64(encode); System.out.p

我不知道这里出了什么问题。我已经尝试了所有可能的组合。设置字符集,toString,no toString。编码工作得很好。我可以把这个数字输入网络解码器,每次都能得到正确的值。就是没法让它工作

输出:

String encode = Base64.encodeBase64String("Hello".getBytes());
System.out.println(encode);
byte[] decode = Base64.decodeBase64(encode);
System.out.println(decode.toString());

如果我使用for循环并手动将字符添加到字符串中,我可以使它工作。但我认为toString是为我做的?

数组上的toString()并不能满足您的期望。。。这只是
Object
中的默认
toString()
实现,它返回一个基于对象hashcode的字符串。尝试
新建字符串(解码)

直接的问题是如何将字节数组转换为字符串

请尝试以下方法:

 run:
SGVsbG8= (encode)

[B@1fb8ee3  (decode)

但是,通常使用未指定字符编码的
String.getBytes()
newstring(byte[])
的重载是个坏主意。他们使用平台默认编码,这立即意味着您的代码不可移植——或者至少,您的数据不可移植。我建议您使用通用编码,如UTF-8。

非常感谢。我想我没有考虑数组上的toString。现在有道理了。
System.out.println(new String(decode));