我意识到这可能更像是一个一般性的 Java 问题,但由于它在 Notes\Domino 环境中运行,我想我应该先检查该社区。p>
总结:
我似乎无法使用 lotus.domino.axis.encoding.Base64 或 sun.misc.BASE64Decoder 解码字符串:dABlAHMAdAA=
我知道原文是:test
我在 http://www5.rptea.com/base64/ 解码确认它似乎是 UTF-16。
作为简单测试,使用以下任一方法:
String s_base64 = "dABlAHMAdAA=";
byte[] byte_base64 = null;
String s_decoded = "";
byte_base64 = new sun.misc.BASE64Decoder().decodeBuffer(s_base64);
s_decoded = new String(byte_base64, "UTF-16");
System.out.println("Test1: " + s_decoded);
byte_base64 = lotus.domino.axis.encoding.Base64.decode(s_base64);
s_decoded = new String(byte_base64, "UTF-16");
System.out.println("Test2: " + s_decoded);
System.out.println("========= FINISH.");
我得到输出:
测试一:????
测试 2:????
如果我将字符串创建为 UTF-8
s_decoded = new String(byte_base64, "UTF-8");
它输出:
没有错误被抛出,但它没有完成代码,没有到达“FINISH”。
详情
我正在访问一个 asmx 网络服务,在 SOAP 响应中,一些节点包含 base64 编码的数据。此时,无法更改服务,所以我不得不自己进行 XPath 和解码。编码数据是文本或 html。如果我通过 http://www5.rptea.com/base64/ 传递编码数据并选择 UTF-16,它解码正确,所以我一定是做错了什么。
作为旁注,我对“测试”进行了编码:
s_base64 = lotus.domino.axis.encoding.Base64.encode(s_text.getBytes());
System.out.println("test1 encodes to: " + s_base64);
s_base64 = new sun.misc.BASE64Encoder().encode(s_text.getBytes());
System.out.println("test2 encodes to: " + s_base64);
它们都编码为:
dGVzdA==
...如果您随后按预期将其输入上面的 2 个解码器,则会正确解码。
如果我转到上面的站点,并将“测试”编码为 UTF-16,我会得到:dABlAHMAdAA= 从而确认数据是 UTF-16。
这就像数据是真正的 base64 数据,但解码器无法识别它。我现在有点难过。
如有任何指点或意见,我们将不胜感激。
最佳答案
该字符串已采用 UTF-16LE(小端字节序)编码,首先存储最低有效字节。 Java 默认为大端。您需要使用:
s_decoded = new String(byte_base64, "UTF-16LE");
关于java解码base64字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17837440/