将InputStream转换为base64,然后转换为字节数组



我需要一个字节数组,但要求是首先将输入流转换为base64,然后将base64转换为字节数组。

我已经尝试过直接使用byte数组,但需要将InputStream转换为base64,然后再转换为byte[]。

InputStream input = ....
byte[] byteArray = IOUtils.toByteArray(input);

您可以使用java.util包中的Base64将流编码为基64

类似这样的东西:

String initialString = "original text";
InputStream input = new ByteArrayInputStream(initialString.getBytes());
byte[] byteEncoded = Base64.getEncoder().encode(IOUtils.toByteArray(input));

方法为Base64.getEncoder().encode,有3个候选:

  • public byte[] encode(byte[] src

  • public int encode(byte[] src,byte[] dst)

  • public ByteBuffer encode(ByteBuffer buffer)

希望能帮助

最新更新