Here you can find the source of downloadImg(String urlPath, File file)
public static void downloadImg(String urlPath, File file) throws IOException
//package com.java2s; //License from project: Apache License import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; public class Main { public static void downloadImg(String urlPath, File file) throws IOException { URL url = new URL(urlPath); try (InputStream in = new BufferedInputStream(url.openStream()); ByteArrayOutputStream out = new ByteArrayOutputStream(); FileOutputStream fos = new FileOutputStream(file);) { byte[] buf = new byte[1024]; int n = 0; while (-1 != (n = in.read(buf))) { out.write(buf, 0, n);// w ww.j a v a2s .c om } byte[] bytes = out.toByteArray(); fos.write(bytes); } } }