0

我目前正在尝试下载一个 .ico 文件的项目,但由于某种奇怪的原因,我似乎无法在下载后以编程方式打开它。但是,我可以使用任何图像编辑器或查看器打开保存的图像。我的代码:

public static BufferedImage parseImageLocal(String url) throws IOException {
        if (url.endsWith(".ico")) {
            return ICODecoder.read(new File(url)).get(0);
        } else if (url.endsWith(".bmp")) {
            return BMPDecoder.read(new File(url));

        } else {
            return ImageIO.read(new File(url));
        }
    }

    public static void saveImage(BufferedImage img, String path)
            throws IOException {

        File outputfile = new File(path.replace("http://", ""));
        File parent = outputfile.getParentFile();
        parent.mkdir();
        if (!outputfile.exists()) {
            outputfile.createNewFile();
        }
        if (path.endsWith(".ico")) {
            ICOEncoder.write(img, outputfile);
        } else if (path.endsWith(".bmp")) {
            BMPEncoder.write(img, outputfile);
        } else {
            ImageIO.write(img, "png", outputfile);
        }
    }

这就是我从互联网上下载图像的方式:

public static BufferedImage parseImage(String url) throws IOException {
        URL dest = new URL(url);
        if (url.endsWith(".ico")) {
            return ICODecoder.read(dest.openStream()).get(0);
        } else if (url.endsWith(".bmp")) {
            return BMPDecoder.read(dest.openStream());

        } else {
            return ImageIO.read(dest);
        }
    }

错误在这一行:

return ICODecoder.read(new File(url)).get(0);
4

1 回答 1

1

“似乎”您正在尝试从 Internet 下载图标,但您正在尝试将其URL视为File.

基本上,这是不可能的,File将无法解析为实际的物理文件。

相反,您应该使用ICODecoder#read(InputStream)URL#openStream

更像...

BufferedImage img = null;
InputStream is = null;
try {
    // url begin an instance of java.net.URL
    is = url.openStream();
    img = ICODecoder.read(is);
} finally {
   try {
       is.close();
   } catch (Exception exp) {
   }
}
return img;

更新了示例

Web 资源不是File,您不能像访问它一样访问它,相反,您需要使用为与 Internet/网络交互而设计的类。

例如...

在此处输入图像描述

import java.awt.EventQueue;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import net.sf.image4j.codec.ico.ICODecoder;

public class ReadFavicon {

    public static void main(String[] args) {
        new ReadFavicon();
    }

    public ReadFavicon() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                try {
                    BufferedImage img = readIcon(new URL("https://secure.gravatar.com/favicon.ico"));
                    JOptionPane.showMessageDialog(null, "My FAVICON", "Icon", JOptionPane.PLAIN_MESSAGE, new ImageIcon(img));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public BufferedImage readIcon(URL url) throws IOException {
        BufferedImage img = null;
        InputStream is = null;
        try {
            // url begin an instance of java.net.URL
            is = url.openStream();
            List<BufferedImage> imgs = ICODecoder.read(is);
            img = imgs != null ? imgs.size() > 0 ? imgs.get(0) : null : null;
        } finally {
            try {
                is.close();
            } catch (Exception exp) {
            }
        }
        return img;
    }

}

更新一些想法

现在。我可能是错的,但是当我运行你的代码时,我遇到了严重的路径问题......

假设原始 url/path 是https://secure.gravatar.com/favicon.ico,当您保存图像时,您会执行类似...

File outputfile = new File(path.replace("http://", ""));
File parent = outputfile.getParentFile();
parent.mkdir();

使用我们的原始路径,这将导致 a outputfileof https://secure.gravatar.com/favicon.ico,这显然是错误的......

我们也可以通过使用来纠正这个问题path.replace("https://", "")......

path = path.replace("http://", "");
path = path.replace("https://", "");

File outputfile = new File(path);
File parent = outputfile.getParentFile();
parent.mkdir();

现在,这导致 a outputfilesecure.gravatar.com/favicon.ico我有点不适应,因为我不确定这是否是你想要的……但它确实对我有用……

现在,当你阅读文件时,你会做这样的事情......

public static BufferedImage parseImage(String url) throws IOException {
    URL dest = new URL(url);
    if (url.endsWith(".ico")) {
        return ICODecoder.read(dest.openStream()).get(0);
    } else if (url.endsWith(".bmp")) {
        return BMPDecoder.read(dest.openStream());

    } else {
        return ImageIO.read(dest);
    }
}

现在,没有相反的证据,我必须假设url没有改变并且仍然https://secure.gravatar.com/favicon.ico......这意味着new File("https://secure.gravatar.com/favicon.ico")将产生无效的文件引用

所以,我再次解析了输入......

url = url.replace("https://", "");
url = url.replace("http://", "");
File outputfile = new File(url);
String parentPath = outputfile.getParent();
String name = outputfile.getName();

url = parentPath + File.separator + name;

哪个生产secure.gravatar.com\favicon.ico

这一切都下载,写入和阅读没有错误。

于 2013-08-26T22:14:42.247 回答