code: private BufferedImage downloadImage() throws Exception {
          BufferedImage image = null;
          InputStream in = null;
          try { // first try reading with the default class
              URL url = new URL(imageUrl);
  
              HttpURLConnection conn = null;
  
              boolean success = false;
              try {
                  conn = (HttpURLConnection) url.openConnection();
                  conn.setInstanceFollowRedirects(followRedirects);
                  conn.setConnectTimeout(connectionTimeout); // TO DO: add retries when connections times out
                  conn.setReadTimeout(readTimeout);
                  conn.connect();
                  success = true;
              } catch (Exception e) {
                  //System.out.println("Connection related exception at url: " + imageUrl);
                  //throw e;
              } finally {
                  if (!success) {
                      conn.disconnect();
                  }
              }
  
              success = false;
              try {
                  in = conn.getInputStream();
                  success = true;
              } catch (Exception e) {
                  /*System.out.println("Exception when getting the input stream from the connection at url: "
                          + imageUrl);
                  throw e;*/
              } finally {
                  if (!success) {
                      in.close();
                  }
              }
              image = ImageIO.read(in);
          } catch (IllegalArgumentException e) {
              // this exception is probably thrown because of a greyscale jpeg image
              System.out.println("Exception: " + e.getMessage() + " | Image: " + imageUrl);
              image = ImageIOGreyScale.read(in); // retry with the modified class
          } catch (MalformedURLException e) {
              System.out.println("Malformed url exception. Url: " + imageUrl);
              //throw e;
          }
          return image;
      }
docs: Download of an image by URL.
  
  @return The image as a BufferedImage object.
  @throws Exception