java.net.URL read stream to byte[]

后端 未结 8 454
失恋的感觉
失恋的感觉 2020-11-29 03:11

I`m trying to read an image from an URL (with the java package java.net.URL) to a byte[]. \"Everything\" works fine, except that the content isnt being ent

8条回答
  •  -上瘾入骨i
    2020-11-29 04:08

    byte[] b = IOUtils.toByteArray((new URL( )).openStream()); //idiom
    

    Note however, that stream is not closed in the above example.

    if you want a (76-character) chunk (using commons codec)...

    byte[] b = Base64.encodeBase64(IOUtils.toByteArray((new URL( )).openStream()), true);
    

提交回复
热议问题