[英]How to check that byte array is Base64 encoded?
How to check that byte array is Base64 encoded? 如何检查字节数组是Base64编码的?
I have an array of bytes, which I need to test if it is base64 encoded. 我有一个字节数组,需要测试它是否是base64编码的。
You can use apache's Base64.isBase64(byte[]) method: 您可以使用apache的Base64.isBase64(byte [])方法:
import org.apache.commons.codec.binary.Base64;
...
boolean isBase64 = Base64.isBase64(bytes);
Base64-encoded strings have length divisible by 4, and consist of characters A
- Z
, a
- z
, 0
- 9
, +
, and /
. Base64编码串具有长度被4整除,和由字符
A
- Z
, a
- z
, 0
- 9
, +
,和/
。 If your array of bytes has length that is not divisible by 4, or contains bytes that map to characters outside these ranges, then it does not represent a Base64-encoded string; 如果您的字节数组的长度不能被4整除,或者包含映射到这些范围之外的字符的字节,则它不表示Base64编码的字符串; otherwise, your array of bytes represents a valid Base-64 encoded string.
否则,您的字节数组代表有效的Base-64编码字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.