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

发布于 2024-08-12 10:20:06 字数 301 浏览 1 评论 0原文

我们可以在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

初见 2024-08-19 10:20:06

使用 ByteArrayInputStream :

InputStream is = new ByteArrayInputStream(decodedBytes);

Use ByteArrayInputStream:

InputStream is = new ByteArrayInputStream(decodedBytes);
被你宠の有点坏 2024-08-19 10:20:06

java.io.ByteArrayInputStream< /a> 扩展了 InputStream 来做到这一点:

InputStream myInputStream = new ByteArrayInputStream(myBytes); 

java.io.ByteArrayInputStream extends InputStream to do exactly this:

InputStream myInputStream = new ByteArrayInputStream(myBytes); 
Saygoodbye 2024-08-19 10:20:06

它位于 javadocs 中的

byte[] byteArr = new byte[] { 0xC, 0xA, 0xF, 0xE };
InputStream is = new ByteArrayInputStream(byteArr);

It is in the javadocs:

byte[] byteArr = new byte[] { 0xC, 0xA, 0xF, 0xE };
InputStream is = new ByteArrayInputStream(byteArr);
花开半夏魅人心 2024-08-19 10:20:06

如果您使用 Robert Harder 的 Base64 实用程序,那么您可以执行以下操作:

InputStream is = new Base64.InputStream(cph);

或者使用 sun 的 JRE ,您可以这样做:

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

但是,不要依赖该类继续成为 JRE 的一部分,甚至不要继续做它今天看起来要做的事情。 Sun说不要用它。

还有其他有关 Base64 解码的 Stack Overflow 问题,例如这个。

If you use Robert Harder's Base64 utility, then you can do:

InputStream is = new Base64.InputStream(cph);

Or with sun's JRE, you can do:

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.

There are other Stack Overflow questions about Base64 decoding, such as this one.

扮仙女 2024-08-19 10:20:06

也可以使用 Guava 库将 byte[] 转换为 InputStream

     <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>19.0</version>
    </dependency>



    public static void guavaConversion() throws IOException{
       byte[] bytesArray = {1, 2, 3};
       InputStream inputStream = ByteSource.wrap(bytesArray).openStream();

       System.out.println(inputStream.toString().getBytes().length); //just 
       //for testing
       inputStream.close();
    }

Its possible to convert byte[] to InputStream using Guava library as well.

     <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>19.0</version>
    </dependency>



    public static void guavaConversion() throws IOException{
       byte[] bytesArray = {1, 2, 3};
       InputStream inputStream = ByteSource.wrap(bytesArray).openStream();

       System.out.println(inputStream.toString().getBytes().length); //just 
       //for testing
       inputStream.close();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文