Convert InputStream to byte array in Java

前端 未结 30 3578
無奈伤痛
無奈伤痛 2020-11-21 12:08

How do I read an entire InputStream into a byte array?

30条回答
  •  太阳男子
    2020-11-21 12:39

    Java 9 will give you finally a nice method:

    InputStream in = ...;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    in.transferTo( bos );
    byte[] bytes = bos.toByteArray();
    

提交回复
热议问题