简体   繁体   English

将字节数组转换为base64字符串Java

[英]Converting byte array to base64 string java

Trying to convert a byte[] to base64 string using org.apache.commons.codec.binary.Base64 ..For this my java code looks like: 尝试使用org.apache.commons.codec.binary.Base64 ..将byte[]转换为base64字符串。为此,我的Java代码如下所示:

 base64String = Base64.encodeBase64URLSafeString(myByteArray);

But what i see is some invalid characters in the generated base64 string.. 但是我看到的是生成的base64字符串中的一些无效字符。 屏幕截图

Why do I see these ____ lines in my generated base64 String? 为什么在生成的base64字符串中看到这些____行? Is it a valid string? 它是有效的字符串吗? Note the length of the generated string is dividable by four. 请注意,生成的字符串的长度可以除以四。

have you tried with the encodeBase64String method instead of using encodeBase64URLSafeString ? 您是否尝试使用encodeBase64String方法而不是使用encodeBase64URLSafeString

encodeBase64URLSafeString: encodeBase64URLSafeString:

Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. 使用base64算法的URL安全变体对二进制数据进行编码,但不对输出进行分块。 The url-safe variation emits - and _ instead of + and / characters. 网址安全变体发出-和_而不是+和/字符。

encodeBase64String: encodeBase64String:

Encodes binary data using the base64 algorithm but does not chunk the output. 使用base64算法对二进制数据进行编码,但不对输出进行分块。 NOTE: We changed the behaviour of this method from multi-line chunking (commons-codec-1.4) to single-line non-chunking (commons-codec-1.5). 注意:我们将此方法的行为从多行分块(commons-codec-1.4)更改为单行非分块(commons-codec-1.5)。

source : org.apache.commons.codec.binary.Base64 javadoc 来源: org.apache.commons.codec.binary.Base64 javadoc

This may be helpful 这可能会有所帮助

sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
encoder.encode(byteArray);

使用

String sCert = javax.xml.bind.DatatypeConverter.printBase64Binary(pk); 

you've got (at least) two flavours of base64, the original one using '+' and '/' in addition to alphanumeric characters, and the "url safe" one, using "-" and "_" so that the content can be enclosed in a URL (or used as a filename, btw). 您(至少)有两种口味的base64,最初的一种除字母数字字符外还使用'+'和'/',另一种是“ url safe”,使用“-”和“ _”使内容可以包含在URL中(或用作文件名,btw)。

It looks like you're using a base64 encoder that has been turned into "url-safe mode". 看来您使用的是已变成“网址安全模式”的base64编码器。

apache's javadoc for URLSafeString() apache的URLSafeString()的javadoc

Oh, and '_' being the last character of the base64 alphabet, seeing strings of "____" means you've been encoding chunks of 0xffffff ... , just like seeing "AAAAAA" means there's a lot of consecutive zeroes. 哦, '_'是base64字母的最后一个字符,看到字符串"____"意味着您已经编码了0xffffff ...的块,就像看到“ AAAAAA”意味着有很多连续的零一样。

If you want to be convinced that it's a normal case, just pick your favourite hexadecimal dumper/editor and check what your binary input looked like. 如果您想确定这是正常情况,只需选择您喜欢的十六进制转储器/编辑器,然后检查您的二进制输入是什么样。

By the below process you can convert byte array to Base64 通过以下过程,您可以将字节数组转换为Base64

 // Convert a byte array to base64 string
    byte[] bytearray= new byte[]{0x12, 0x23};
    String s = new sun.misc.BASE64Encoder().encode(bytearray);

    // Convert base64 string to a byte array
    bytearray = new sun.misc.BASE64Decoder().decodeBuffer(s);

Edit: 编辑:

Please check for guide below link commons apache 请在下面的链接共享库Apache中查看指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM