如何从使用Java的网站获取favicon.ico? [英] How to get favicon.ico from a website using Java?

查看:1079
本文介绍了如何从使用Java的网站获取favicon.ico?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在创建一个应用程序来存储所有用户最喜欢的应用程序的快捷方式,就像一个集线器。我可以支持实际文件,我有一个 .lnk 解析器用于快捷方式。我认为应用程序支持Internet快捷方式也不错。这就是我正在做的事情:

So I'm making an application to store shortcuts to all the user's favorite applications, acting kind of like a hub. I can have support for actual files and I have a .lnk parser for shortcuts. I thought it would be pretty good for the application to support Internet shortcuts, too. This is what I'm doing:

假设我想要 Google的图标 http://www.google.com/favicon.ico )。


  1. 我首先要删掉额外的页面(例如 www.google.com/anotherpage 将成为 www.google.com

然后,我使用 ImageIO.read(java.net.URL)获取图片。

Then, I use ImageIO.read(java.net.URL) to get the Image.

问题是 ImageIO 在我调用此方法时永远不会返回Image:

The problem is that ImageIO never returns an Image when I call this method:

String trimmed = getBaseURL(page); //This removes the extra pages
Image icon = null;    
try {
    String fullURLString = trimmed + "/favicon.ico";
    URL faviconURL = new URL(fullURLString);
    icon = ImageIO.read(faviconURL);
} catch (IOException e) {
    e.printStackTrace();
}

return icon;

现在我有两个问题:


  1. Java是否支持ICO格式,即使它来自Microsoft?

  2. 为什么 ImageIO 无法从网址读取?

  1. Does Java support the ICO format even though it is from Microsoft?
  2. Why does ImageIO fail to read from the URL?

提前感谢您!

推荐答案

尝试 Image4J

这个快速的Scala REPL会话显示(可以粘贴为Java代码):

As this quick Scala REPL session shows (paste-able as Java code):

> net.sf.image4j.codec.ico.ICODecoder.read(new java.net.URL("http://www.google.com/favicon.ico").openStream())

res1: java.util.List[java.awt.image.BufferedImage] = [BufferedImage@65712a80: type = 2 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=ff000000 IntegerInterleavedRaster: width = 16 height = 16 #Bands = 4 xOff = 0 yOff = 0 dataOffset[0] 0]






更新

回答您的问题:Java是否支持ICO?看起来不像:

To answer your questions: Does Java support ICO? Doesn't seem like it:

> javax.imageio.ImageIO.read(new java.net.URL("http://www.google.com/favicon.ico"))

java.lang.IllegalArgumentException: Empty region!

为什么 ImageIO 无法从网址是什么?好吧,URL本身似乎对我有用,所以你可能有代理/防火墙问题,或者它可能是上面的问题。

Why does ImageIO fail to read from the URL? Well, the URL itself seems to work for me, so you may have a proxy/firewall issue, or it could be the problem above.

这篇关于如何从使用Java的网站获取favicon.ico?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆