-1

I am Referencing an array to generate urls to download images from however if the url is dead the program errors and stops i need it to skip that url if an error occurs but I can not figure out how any ideas?

package getimages2;
public class Extractimages {

public static String url_to_get = "http://www.url.com/upc/11206007076";
public static long urlupc;
public static String URLarray[] = new String[3000];

 public static void main(String args[]) throws Exception {

     for(int apples = 0; apples<URLarray.length; apples++){

        String UPCarray[] = {"051000012616","051000012913","051000012937"};
        url_to_get = "http://www.url/upc/" + UPCarray[apples];

        String webUrl = url_to_get;
        String url2 = url_to_get;
        URL url = new URL(webUrl);
        URLConnection connection = url.openConnection();
        InputStream is = connection.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        HTMLEditorKit htmlKit = new HTMLEditorKit();
        HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
        HTMLEditorKit.Parser parser = new ParserDelegator();
        HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
        parser.parse(br, callback, true);
        for (HTMLDocument.Iterator iterator = htmlDoc.getIterator(HTML.Tag.IMG); iterator.isValid(); iterator.next()) {

            AttributeSet attributes = iterator.getAttributes();
            String imgSrc = (String) attributes.getAttribute(HTML.Attribute.SRC);

            if (imgSrc != null && (imgSrc.endsWith(".jpg") || (imgSrc.endsWith(".png")) || (imgSrc.endsWith(".jpeg")) || (imgSrc.endsWith(".bmp")) || (imgSrc.endsWith(".ico")))) {
                try {
                    downloadImage(webUrl, imgSrc);
                } catch (IOException ex) {
                    System.out.println(ex.getMessage());
                }
            }
        }
    }
 }
 public static String right(String value, int length) {
     return value.substring(value.length() - length);}

 private static void downloadImage(String url, String imgSrc) throws IOException {
        BufferedImage image = null;
        try {
            if (!(imgSrc.startsWith("http"))) {
                url = url + imgSrc;
            } else {
                url = imgSrc;
            }
            String webUrl = url_to_get;
            String imagename = right(webUrl , 12);
            imgSrc = imgSrc.substring(imgSrc.lastIndexOf("/") + 1);
            String imageFormat = null;
            imageFormat = imgSrc.substring(imgSrc.lastIndexOf(".") + 1);
            String imgPath = null;
            imgPath = "C:/Users/Noah/Desktop/photos/" + imagename + ".jpg";
            URL imageUrl = new URL(url);
            image = ImageIO.read(imageUrl);
            if (image != null) {
                File file = new File(imgPath);
                ImageIO.write(image, imageFormat, file);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}

The program runs through the array fine but once it generates a dead url it errors and exits i need it to skip that url and move on. Thanks for any help.

La Fon
  • 145
  • 1
  • 1
  • 9
  • you "can not figure out how any ideas?" what ideas? didnt you see the Stop sign on the road? and good luck – gpasch May 10 '16 at 21:49

1 Answers1

0

Well, you'll need to have a way to check if the URL you've generated is valid. There's some answers on that here: How to check for a valid URL in Java?

Community
  • 1
  • 1