[英]How to decode base64 in java
I am getting a base64encoded
xml data in webservice response and I want to decode it in Java . 我在Web服务响应中获取了以
base64encoded
xml数据,我想在Java中对其进行解码。 I tried org.apache.commons.codec.binary.Base64;
我尝试了
org.apache.commons.codec.binary.Base64;
byte bytesEncoded[] = base64encodedStringfromWebservice.getBytes();
// Decrypt data on other side, by processing encoded data
byte[] valueDecoded= Base64.decodeBase64(bytesEncoded );
System.out.println("Decoded value is " + new String(valueDecoded));
but still some characters like (< , />) are not getting decoded properly . 但是仍然不能正确解码像(<,/>)这样的一些字符。
Please suggest how to decode it correctly? 请提出如何正确解码的建议?
The getBytes
method from String
is platform specific . String
的getBytes
方法是特定于平台的 。 You need to specify an encoding , and later use that same encoding to decode the string. 您需要指定一个encoding ,以后再使用相同的编码来解码字符串。 You can just use UTF8.
您可以只使用UTF8。
You also need to do the steps in reverse order: string -> base64 -> raw utf8 bytes -> base64 -> string 您还需要以相反的顺序执行步骤:字符串-> base64->原始utf8字节-> base64->字符串
// ENCODE data
byte bytesEncoded[] = base64encodedStringfromWebservice.getBytes("UTF8");
// DECODE data on other side, by processing encoded data
String base64encodedStringfromWebservice = new String(bytesEncoded, "UTF8");
byte[] valueDecoded = Base64.decodeBase64(base64encodedStringfromWebservice);
System.out.println("Decoded value is " + new String(valueDecoded));
Try specifiying the charset you are using. 尝试指定您正在使用的字符集。 for example, utf-8, and as you can see here: Decoding a Base64 string in Java
例如utf-8,您将在此处看到: 用Java解码Base64字符串
byte[] valueDecoded= Base64.decodeBase64(bytesEncoded);
System.out.println("Decoded value is " + new String(valueDecoded, "UTF-8"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.