1414import java .io .FileOutputStream ;
1515import java .io .IOException ;
1616import java .io .InputStream ;
17+ import java .net .HttpURLConnection ;
1718import java .net .URL ;
1819import java .nio .charset .StandardCharsets ;
1920import java .nio .file .Files ;
@@ -313,8 +314,9 @@ public static File findFirstFile(File searchFolder, String regex) {
313314 * @param file File to copy the downloaded resource
314315 * @throws IOException Resource cannot be copied/downloaded
315316 */
316- public static void downloadFromUrl (URL url , File file ) throws IOException {
317- org .apache .commons .io .FileUtils .copyURLToFile (url , file );
317+ public static void downloadFromUrl (URL url , File file ) throws Exception {
318+ // org.apache.commons.io.FileUtils.copyURLToFile(url, file);
319+ copyUrlToFile (url ,file );
318320 Logger .info ("File downloaded from [" + url + "] to [" + file .getAbsolutePath () + "]" );
319321 }
320322
@@ -325,7 +327,7 @@ public static void downloadFromUrl(URL url, File file) throws IOException {
325327 * @param file File to copy the downloaded resource
326328 * @throws IOException Resource cannot be copied/downloaded
327329 */
328- public static void downloadFromUrl (String url , File file ) throws IOException {
330+ public static void downloadFromUrl (String url , File file ) throws Exception {
329331 downloadFromUrl (new URL (url ), file );
330332 }
331333
@@ -366,4 +368,36 @@ public static void writeStringToFileWithBOM(File output, String data) throws Exc
366368 fileOutputStream .close ();
367369 }
368370
371+ /**
372+ *
373+ * @param url URL to download
374+ * @param file File to copy the downloaded resource
375+ * @throws Exception if something goes wrong
376+ */
377+ public static void copyUrlToFile (URL url ,File file ) throws Exception {
378+ copyUrlToFile (url ,file ,30000 );
379+ }
380+
381+ /**
382+ *
383+ * @param url URL to download
384+ * @param file File to copy the downloaded resource
385+ * @param timeout Download timeout (ms)
386+ * @throws Exception if something goes wrong
387+ */
388+ public static void copyUrlToFile (URL url ,File file ,int timeout ) throws Exception {
389+ HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
390+ connection .setConnectTimeout (timeout );
391+ connection .setReadTimeout (timeout );
392+ if (connection .getResponseCode () == HttpURLConnection .HTTP_OK ) {
393+ try (InputStream in = connection .getInputStream ();
394+ FileOutputStream out = new FileOutputStream (file )) {
395+ IOUtils .copy (in , out );
396+ }
397+ } else {
398+ Logger .warn ("Failed to download the file. Response code: " + connection .getResponseCode ());
399+ }
400+ connection .disconnect ();
401+ }
402+
369403}
0 commit comments