我们可以将字节数组转换为Java中的InputStream吗?

时间:2009-11-26 07:42:47

标签: java base64 inputstream

我们可以将字节数组转换为Java中的InputStream吗?我一直在网上看,但找不到它。

我有一个以InputStream为参数的方法。

我的InputStream cph是base64编码所以我必须使用

解码它
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(cph);

现在如何将decodedBytes再次转换为InputStream

2 个答案:

答案 0 :(得分:280)

使用ByteArrayInputStream

InputStream is = new ByteArrayInputStream(decodedBytes);

答案 1 :(得分:4)

如果您使用Robert Harder's Base64 utility,则可以执行以下操作:

InputStream is = new Base64.InputStream(cph);

或者使用太阳的JRE,你可以这样做:

InputStream is = new
com.sun.xml.internal.messaging.saaj.packaging.mime.util.BASE64DecoderStream(cph)

然而,不要依赖那个继续成为JRE一部分的课程,或者甚至继续做它今天似乎做的事情。孙说不要用它。

还有其他关于Base64解码的Stack Overflow问题,例如this one.

相关问题