Here you can find the source of downloadFileAndRetry(final File file, final URL url, final int retry)
private static void downloadFileAndRetry(final File file, final URL url, final int retry) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.*; import java.net.*; public class Main { private static void downloadFileAndRetry(final File file, final URL url, final int retry) throws IOException { final FileOutputStream fos = new FileOutputStream(file); try {//from w ww .java2 s .c o m final byte[] buffer = new byte[8192]; final InputStream stream = new BufferedInputStream(url.openConnection().getInputStream()); try { int len; while ((len = stream.read(buffer)) != -1) { fos.write(buffer, 0, len); } } finally { stream.close(); } } catch (IOException io) { if (retry > 0) { downloadFileAndRetry(file, url, retry - 1); } else throw io; } finally { fos.close(); } } public static String read(final InputStream stream) throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final byte[] buffer = new byte[8192]; int len; while ((len = stream.read(buffer)) != -1) { baos.write(buffer, 0, len); } return baos.toString("UTF-8"); } }