首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >保存internet图标,然后重新打开将导致使用image4j的EOF。

保存internet图标,然后重新打开将导致使用image4j的EOF。
EN

Stack Overflow用户
提问于 2013-08-26 22:08:34
回答 1查看 462关注 0票数 0

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

代码语言:javascript
运行
复制
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);
        }
    }

我就是这样从网上下载图片的:

代码语言:javascript
运行
复制
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);
        }
    }

错误在这一行上:

代码语言:javascript
运行
复制
return ICODecoder.read(new File(url)).get(0);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-26 22:14:42

“似乎”你试图从互联网下载图标,但你试图把URL当作一个File

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

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

更像是..。

代码语言:javascript
运行
复制
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,您不能像访问web资源那样访问它,相反,您需要使用设计用于与internet/网络交互的类。

例如..。

代码语言:javascript
运行
复制
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,当您保存映像时,您会执行如下操作.

代码语言:javascript
运行
复制
File outputfile = new File(path.replace("http://", ""));
File parent = outputfile.getParentFile();
parent.mkdir();

对于我们最初的路径,这将导致outputfile of https://secure.gravatar.com/favicon.ico,这显然是错误的.

我们也可以通过使用path.replace("https://", "")对此进行修正。

代码语言:javascript
运行
复制
path = path.replace("http://", "");
path = path.replace("https://", "");

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

现在,这将导致outputfile of secure.gravatar.com/favicon.ico。我变得有点松懈,因为我不确定这是不是你的want...but,它确实对我有用.

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

代码语言:javascript
运行
复制
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...this仍然意味着new File("https://secure.gravatar.com/favicon.ico")将生成无效的文件引用。

所以,再次,我分析了输入..。

代码语言:javascript
运行
复制
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

所有这些都下载、写入和读取,没有错误。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18453855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档