Java 如何将字节数组转换为 base64 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37923237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert byte array to base64 String
提问by cool rock
List<Map<String, Object>> lists = baseDao.getJdbcTemplate().queryForList(queryForImages, productId);
**System.out.println("size----" + lists.size());**
ArrayList<String> arrayList = new ArrayList<String>();
for (Map<String, Object> base64 : lists) {
byte[] imgData = (byte[]) base64.get("images");
String base64Encoded = new String(imgData, StandardCharsets.UTF_8);
// byte[] encodedImage = Base64.encodeBase64(imgData);
System.out.println("encoded---2" + base64Encoded);
}
i retrieve list of images from db and convert byte array to base64 string ,if i sysout only the last image in the list prints in sysout , even sysout list.size under the method is not printing .
我从数据库中检索图像列表并将字节数组转换为 base64 字符串,如果我 sysout 只有列表中的最后一个图像在 sysout 中打印,即使该方法下的 sysout list.size 也不打印。
回答by Jesper
To start with, this is wrong:
首先,这是错误的:
String base64Encoded = new String(imgData, StandardCharsets.UTF_8);
The image bytes you have do not represent UTF-8 encoded text, so it makes no sense to create a String
out of the bytes, pretending that this is UTF-8 encoded text.
您拥有的图像字节不代表 UTF-8 编码的文本,因此String
从字节中创建一个假装这是 UTF-8 编码的文本是没有意义的。
If you are using Java 8, then use the Base64
class available in the standard library:
如果您使用的是 Java 8,请使用Base64
标准库中提供的类:
import java.util.Base64;
// ...
byte[] imgData = (byte[]) base64.get("images");
String base64Encoded = Base64.getEncoder().encodeToString(imgData);
If you are not using Java 8, you can use a third-party library, for example Apache Commons Codec, which includes classes for Base64 encoding and decoding.
如果您不使用 Java 8,则可以使用第三方库,例如Apache Commons Codec,其中包含用于 Base64 编码和解码的类。
回答by Mike Adamenko
Prior to java 1.8 use apache commons library
在 java 1.8 之前使用apache commons 库
String base64 = new String(Base64.encodeBase64(imgData))
回答by narko
If you are working with Android you can use the following code by importing the Android version of the Base64 class:
如果您使用的是 Android,您可以通过导入Base64 类的 Android 版本来使用以下代码:
Base64.encodeToString(payload, Base64.DEFAULT);
From Android API 26, you can use java.util.Base64 as well.
从 Android API 26 开始,您也可以使用 java.util.Base64。
回答by Mohammad Badiuzzaman
In android, you can use Base64 (android.util.Base64) to encoded byte[] to String;
在android中,您可以使用Base64(android.util.Base64)将byte[]编码为String;
String encodedString = Base64.encodeToString(byteArray, Base64.DEFAULT);
and
和
Decoding part as like following;
解码部分如下;
byte[] byteArray = org.apache.commons.codec.binary.Base64.decodeBase64(encodedString);
Usually this technique used in android app to send binary file/data to Server, using Web Service;
通常在android应用程序中使用这种技术将二进制文件/数据发送到服务器,使用Web服务;