Convert InputStream to byte array in Java

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

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

30条回答
  •  猫巷女王i
    2020-11-21 12:46

    Input Stream is ...
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int next = in.read();
    while (next > -1) {
        bos.write(next);
        next = in.read();
    }
    bos.flush();
    byte[] result = bos.toByteArray();
    bos.close();
    

提交回复
热议问题