Java 图片下载器出错无法跳过

Java Image Downloader Can't Skip if Error

我正在引用一个数组来生成 urls 以从中下载图像但是如果 url 死了程序错误并停止我需要它跳过 url 如果一个出现错误但我想不通怎么办?

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();
        }

    }

}

程序在数组中运行良好,但一旦它生成一个死url它就会出错并退出我需要它跳过那个url并继续前进。 感谢您的帮助。

那么,您需要有一种方法来检查您生成的 URL 是否有效。这里有一些答案: How to check for a valid URL in Java?