Today, I only have a little code snippet to take a URL with an image and transform it in a byte array in Java 8, the image, not the URL. It is something very simple, but sometimes, it is very useful to have this kind of snippets in a place where we can find it easily.
package com.wordpress.binarycoders.image.recovery; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.URL; import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger; public class ImageRecover { public byte[] recoverImageFromUrl(String urlText) throws Exception { URL url = new URL(urlText); ByteArrayOutputStream output = new ByteArrayOutputStream(); try (InputStream inputStream = url.openStream()) { int n = 0; byte [] buffer = new byte[ 1024 ]; while (-1 != (n = inputStream.read(buffer))) { output.write(buffer, 0, n); } } return output.toByteArray(); } }
See you.
Thanks, its helpful.
LikeLike