Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java图像下载程序可以';如果有错误,请不要跳过_Java_Image_Http_Url_Jpeg - Fatal编程技术网

Java图像下载程序可以';如果有错误,请不要跳过

Java图像下载程序可以';如果有错误,请不要跳过,java,image,http,url,jpeg,Java,Image,Http,Url,Jpeg,我引用了一个数组来生成url以从中下载图像,但是如果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 URLarra

我引用了一个数组来生成url以从中下载图像,但是如果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();
        }

    }

}
包getimages2;
公共类图像提取{
公共静态字符串url_to_get=”http://www.url.com/upc/11206007076";
公共静态长URLC;
公共静态字符串URLARY[]=新字符串[3000];
公共静态void main(字符串args[])引发异常{

对于(int apples=0;apples那么,您需要有一种方法来检查您生成的URL是否有效。这里有一些答案:

你“想不出怎么会有什么想法?”什么想法?你没看到路上的停车标志吗?好运气的汉克斯会告诉你,我创建了一个URL检查方法。但是,由于网站为无效URL创建了一个页面,它总是返回TRUE。我不知道是否有办法解决这个问题