问题

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

我有一个方法,有一个InputStream作为参数。

InputStreamcphI具有base64编码,因此我必须使用它进行解码

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

现在我怎么转换decodedBytesagain到InputStream


#1 热门回答(276 赞)

UseByteArrayInputStream

InputStream is = new ByteArrayInputStream(decodedBytes);

#2 热门回答(4 赞)

如果你使用Robert Harder's Base64 utility,那么你可以这样做:

InputStream is = new Base64.InputStream(cph);

或者使用sun的JRE,你可以:

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

但是,不要依赖该类继续成为JRE的一部分,或者甚至继续做它今天似乎做的事情。孙说不要用它。

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


原文链接