使用Base64类的getEncoder()方法获取一个Base64编码器对象,encodeToString()方法将图片的二进制数据转换为base64编码全为A
时间: 2024-05-05 07:16:14 浏览: 94
AAAA的字符串:
```
import java.util.Base64;
public class Base64Example {
public static void main(String[] args) {
byte[] imageBytes = {0x12, 0x34, 0x56, 0x78};
Base64.Encoder encoder = Base64.getEncoder();
String base64String = encoder.encodeToString(imageBytes);
System.out.println(base64String); // 输出:EjRWeA==
// 将base64编码字符串还原为二进制数据
Base64.Decoder decoder = Base64.getDecoder();
byte[] decodedBytes = decoder.decode(base64String);
System.out.println(decodedBytes[0]); // 输出:18
System.out.println(decodedBytes[1]); // 输出:52
System.out.println(decodedBytes[2]); // 输出:86
System.out.println(decodedBytes[3]); // 输出:120
}
}
```
阅读全文