Last active
July 6, 2018 09:35
-
-
Save chandu-io/4678746 to your computer and use it in GitHub Desktop.
java :: save image from url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.net.URL; | |
public class SaveImageFromUrl { | |
public static void main(String[] args) throws Exception { | |
String imageUrl = "https://www.google.com/images/srpr/logo3w.png"; | |
String destinationFile = "savedImage.jpg"; | |
saveImage(imageUrl, destinationFile); | |
} | |
public static void saveImage(String imageUrl, String destinationFile) throws IOException { | |
URL url = new URL(imageUrl); | |
InputStream is = url.openStream(); | |
OutputStream os = new FileOutputStream(destinationFile); | |
byte[] b = new byte[2048]; | |
int length; | |
while ((length = is.read(b)) != -1) { | |
os.write(b, 0, length); | |
} | |
is.close(); | |
os.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment