我们可以在 Java 中将字节数组转换为 InputStream 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1802123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we convert a byte array into an InputStream in Java?
提问by rover12
Can we convert a byte array into an InputStream in Java? I have been looking on the internet but couldn't find it.
我们可以在 Java 中将字节数组转换为 InputStream 吗?我一直在互联网上寻找,但找不到。
I have a method that has an InputStream as argument.
我有一个以 InputStream 作为参数的方法。
The InputStream cph
I have is base64 encoded so I had to decode it using
cph
我拥有的 InputStream是 base64 编码的,所以我必须使用
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(cph);
Now how do I convert decodedBytes
again to InputStream
?
现在我如何decodedBytes
再次转换为InputStream
?
采纳答案by Daniel Rikowski
Use ByteArrayInputStream
:
InputStream is = new ByteArrayInputStream(decodedBytes);
回答by Stephen Denne
If you use Robert Harder's Base64 utility, then you can do:
如果您使用Robert Harder 的 Base64 实用程序,则可以执行以下操作:
InputStream is = new Base64.InputStream(cph);
Or with sun's JRE, you can do:
或者使用 sun 的 JRE,您可以:
InputStream is = new
com.sun.xml.internal.messaging.saaj.packaging.mime.util.BASE64DecoderStream(cph)
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.
但是,不要指望该类继续成为 JRE 的一部分,或者甚至继续做它今天似乎在做的事情。孙说不要用它。
There are other Stack Overflow questions about Base64 decoding, such as this one.
还有其他关于 Base64 解码的 Stack Overflow 问题,比如这个。