0

我正在尝试从请求的网站中提取图像,然后将其保存到文件夹中。唯一的问题是我从中提取图像的网站有多种图像格式。所以图像的扩展名可以是 png、jpg 或 gif。我需要保存所有这些,但我不知道如何告诉我的程序提取多种格式而不仅仅是一种..

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main { 
    public static void main( String[] args )
    {
        int avatarnum = 1;
        String extension = null;
        BufferedImage image = null;
        extension = "jpg";

        while(avatarnum <= 1000){
            try {
                URL url = new URL("http://forum.blockland.us/avatarUpload/avatar_"+avatarnum+"."+extension);
                image = ImageIO.read(url);

                try{
                    ImageIO.write(image, extension,new File("C:\\test\\"+avatarnum+"."+extension));
                    System.out.println("Write successful");
                    avatarnum ++;
                } catch(Exception e){
                    System.out.println("Printing stacktrace");
                    e.printStackTrace();
                }
                avatarnum ++;

            } catch (IOException e) {
                System.out.println("File not found! " + avatarnum);
                System.out.println("Adding one to variable and retrying..");
                avatarnum ++;
            }
        }
        System.out.println("Done");
    }
}

截至目前,仅从网站中提取 jpg 图像并将其保存到文件夹中。
出于测试目的,它只会从站点中提取 1,000 张图像。

如果有人想知道,我将使用它来制作“头像数据库”。

非常感谢你们!

4

2 回答 2

1

您需要遍历多种图像格式(扩展):

public static void main(String[] args) {
    int avatarnum = 1;
    BufferedImage image = null;

    String[] extensions = { "jpg", "png", "gif", "bmp" };

    while (avatarnum <= 1000) {
        for (String extension : extensions) {
            try {
                URL url = new URL("http://forum.blockland.us/avatarUpload/avatar_" + avatarnum + "." + extension);
                image = ImageIO.read(url);

                try {
                    ImageIO.write(image, extension, new File("C:\\test\\" + avatarnum + "." + extension));
                    System.out.println("Write successful");

                } catch (Exception e) {
                    System.out.println("Error on save the image");
                }

            } catch (IOException e) {
                System.out.println("File not found! " + avatarnum + "." + extension);

            }
        }
        avatarnum++;
    }
    System.out.println("Done");
}
于 2013-05-24T20:58:07.890 回答
-1

使用具有所有有效扩展名的数组

String[] extensions = {"jpg", "png", "gif" };

并在检索 URL 时遍历所有这些

try {
    for (String extension: extensions){
        try {
            URL url = new URL("http://forum.blockland.us/avatarUpload/avatar_"+avatarnum+"."+extension);
            image = ImageIO.read(url);
        } catch (Exception e){
             //Wrong extension (or other kind of error)
        }
    }
    ...

对于每个图像,它会因错误的扩展名而失败,但会检索具有正确扩展名的图像

于 2013-05-24T20:58:06.793 回答