Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageIcon update in Java (from the internet)?

I have some URL with an image there. This image updates during each request (that is, each request to the (same) URL returns a new image). Say, this URL points to CAPTCHA. My goal is to load and display several such images in my program.

The following code loads these images to my local filesystem and works OK (that is, all the images are different, unique):

String filePath;
String urlPath;
int numOfFilesToDownload;

//Here filePath and urlPath are initialized.
//filePath points to the directory, where to save images
//urlPath is the url from where to download images
//numOfFilesToDownload is the number of files to download

for(int i = 0; i < numOfFilesToDownload; i++){
    //Initializing connection
    URL url = new URL(urlPath);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    //Downloading image
    try(InputStream is = conn.getInputStream();
        FileOutputStream os = new FileOutputStream(filePath + "img" + i + ".jpg")){
        int b;
        while((b = is.read()) != -1)
            os.write(b);
    }
}

But something weird happens, when I try the following thing:

for(int i = 0; i < numOfFilesToDownload; i++){
    //Initializing image from the url
    URL url = new URL(urlPath);
    javax.swing.ImageIcon ico = new javax.swing.ImageIcon(url);

    //Showing the graphical dialog window with the image
    javax.swing.JOptionPane.showMessageDialog(null, ico);
}

In the latter case, each dialog contains the same image, namely the one, downloaded during the very first iteration.

Also, the experiments show, that if you concatenate "?r=" to the urlPath (that is, a simple GET request parameter), the url will still be valid. And the following code appears to be valid and does exactly what it has (namely each image shown is different from the previous):

for(int i = 0; i < numOfFilesToDownload; i++){
    //Initializing image from the url
    URL url = new URL(urlPath + "?r=" + i);
    javax.swing.ImageIcon ico = new javax.swing.ImageIcon(url);

    //Showing the graphical dialog window with the image
    javax.swing.JOptionPane.showMessageDialog(null, ico);
}

Hence I can make a conclusion, that ImageIcon somehow remembers the URLs it handled and simply does not bother to perform the same work twice... Why and how? There's nothing in javadocs about it.

like image 481
Angstrem Avatar asked Mar 24 '26 03:03

Angstrem


1 Answers

When I tried a variation of your code, it worked fine. My SSCCE:

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class TestUrls {
   public static final String BASE_URL_PATH = "http://static.ed.edmunds-media.com/" +
        "unversioned/adunit/homepage_showcase/";
   public static final String[] URL_PATHS = {
      "honda-odyssey-2013.png",
      "chevrolet-impala-2013.png",
      "mazda-cx9-2013.png",
      "toyota-rav4-2013-2.png"
   };

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            for (String urlPath : URL_PATHS) {
               String fullUrlPath = BASE_URL_PATH + urlPath;
               try {
                  URL url = new URL(fullUrlPath);
                  BufferedImage img = ImageIO.read(url);
                  ImageIcon icon = new ImageIcon(img);
                  JOptionPane.showMessageDialog(null, icon);
               } catch (MalformedURLException e) {
                  e.printStackTrace();
               } catch (IOException e) {
                  e.printStackTrace();
               } 
            }
         }
      });
   }
}
like image 112
Hovercraft Full Of Eels Avatar answered Mar 25 '26 17:03

Hovercraft Full Of Eels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!