Here you can find the source of downloadAndSaveImage(URL imageUrl, String path)
public static boolean downloadAndSaveImage(URL imageUrl, String path)
//package com.java2s; //License from project: Open Source License import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; public class Main { public static boolean downloadAndSaveImage(URL imageUrl, String path) { try {// w ww. j ava 2 s . c om InputStream in; in = new BufferedInputStream(imageUrl.openStream()); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int n = 0; while (-1 != (n = in.read(buf))) { out.write(buf, 0, n); } out.close(); in.close(); byte[] response = out.toByteArray(); FileOutputStream fos; fos = new FileOutputStream(path); fos.write(response); fos.close(); } catch (IOException e1) { e1.printStackTrace(); return false; } return true; } }