“似乎”您正在尝试从 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 outputfile
of 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 outputfile
。secure.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
这一切都下载,写入和阅读没有错误。