Skip to main content
 首页 » 编程设计

java中从给定 URL 下载所有图像并将其保存到桌面无法完全正常工作

2024年11月24日67飞鱼

我有以下代码来提取并保存 here 中的所有图像到我的桌面。
它在某种程度上有效,即它仅下载 15 个图像并停在那里。不知道为什么其余图像没有被下载。我桌面上保存的最后一张图片是 1402_NYFW_0300_GoRed_w95_h95_cw95_ch95_thumb.jpg

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import javax.swing.text.html.HTML; 
import javax.swing.text.html.HTMLEditorKit; 
import javax.swing.text.html.parser.ParserDelegator; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.URL; 
import java.net.URLConnection; 
import javax.imageio.ImageIO; 
import javax.swing.text.AttributeSet; 
import javax.swing.text.html.HTMLDocument; 
 
public class ExtractAllImages { 
public static void main(String args[]) throws Exception { 
 
    String webUrl = "http://ramp.sdr.co.za/cache/1402NYFW/GoRedForWomen/"; 
    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.A); iterator.isValid(); iterator.next()) { 
        AttributeSet attributes = iterator.getAttributes(); 
        String imgSrc = (String) attributes.getAttribute(HTML.Attribute.HREF); 
 
        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()); 
            } 
        } 
    } 
} 
private static void downloadImage(String url, String imgSrc) throws IOException { 
    BufferedImage image = null; 
    try { 
        if (!(imgSrc.startsWith("http"))) { 
            url = url + imgSrc; 
        } else { 
            url = imgSrc; 
        } 
        imgSrc = imgSrc.substring(imgSrc.lastIndexOf("/") + 1); 
        String imageFormat = null; 
        imageFormat = imgSrc.substring(imgSrc.lastIndexOf(".") + 1); 
        String imgPath = null; 
        imgPath = "C:/temp/" + imgSrc + ""; 
        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(); 
    } 
 
} 
} 

请您参考如下方法:

好的,页面又可以工作了,我得仔细看看。试试这个:

public static void main(String args[]) throws Exception { 
 
String webUrl = "http://ramp.sdr.co.za/cache/1402NYFW/GoRedForWomen/"; 
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(); 
htmlKit.read(br, htmlDoc, 0); 
 
for (HTMLDocument.Iterator iterator = htmlDoc.getIterator(HTML.Tag.A); iterator.isValid(); iterator.next()) { 
    AttributeSet attributes = iterator.getAttributes(); 
    String imgSrc = (String) attributes.getAttribute(HTML.Attribute.HREF); 
 
    System.out.println(imgSrc); 
    if (imgSrc != null && (imgSrc.toLowerCase().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()); 
        } 
    } 
} 

}

我直接使用 HTMLEditorKit 的 read() 方法,而不是使用回调。这似乎有效。