我们可以在Java中将字节数组转换为InputStream吗?
我们可以在Java中将字节数组转换为InputStream吗?我一直在网上寻找但找不到。
我有一个以 InputStream 作为参数的方法。
我的InputStream cph 是base64 编码的,所以我必须使用它来解码。
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(cph);
现在如何将decodedBytes 再次转换为InputStream?
Can we convert a byte array into an InputStream in Java? I have been looking on the internet but couldn't find it.
I have a method that has an InputStream as argument.
The InputStream cph
I have is base64 encoded so I had to decode it using
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(cph);
Now how do I convert decodedBytes
again to InputStream
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
ByteArrayInputStream
:Use
ByteArrayInputStream
:java.io.ByteArrayInputStream
< /a> 扩展了InputStream
来做到这一点:java.io.ByteArrayInputStream
extendsInputStream
to do exactly this:它位于 javadocs 中的:
It is in the javadocs:
如果您使用 Robert Harder 的 Base64 实用程序,那么您可以执行以下操作:
或者使用 sun 的 JRE ,您可以这样做:
但是,不要依赖该类继续成为 JRE 的一部分,甚至不要继续做它今天看起来要做的事情。 Sun说不要用它。
还有其他有关 Base64 解码的 Stack Overflow 问题,例如这个。
If you use Robert Harder's Base64 utility, then you can do:
Or with sun's JRE, you can do:
However don't rely on that class continuing to be a part of the JRE, or even continuing to do what it seems to do today. Sun say not to use it.
There are other Stack Overflow questions about Base64 decoding, such as this one.
也可以使用 Guava 库将 byte[] 转换为 InputStream。
Its possible to convert byte[] to InputStream using Guava library as well.