在Java中,如何将InputStream转换为字节数组(byte[])?
我的背景是 .net,我对 Java 还很陌生。我正在为我们公司的 java 团队做一些工作,架构师需要我实现一个采用 InputStream (java.io) 对象的方法。为了实现该方法的目的,我需要将其转换为字节数组。有没有简单的方法可以做到这一点?
My background is .net, I'm fairly new to Java. I'm doing some work for our company's java team and the architect needs me to implement a method that takes an InputStream (java.io) object. In order to fulfill the method's purpose I need to convert that into a byte array. Is there an easy way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法是创建一个新的 ByteArrayOutputStream,将字节复制到其中,然后调用 toByteArray:
The simplest way is to create a new
ByteArrayOutputStream
, copy the bytes to that, and then calltoByteArray
:一种简单的方法是使用 org.apache.commons.io.IOUtils.toByteArray( inputStream ) ,请参阅 apache commons io。
A simple way would be to use
org.apache.commons.io.IOUtils.toByteArray( inputStream )
, see apache commons io.