我目前正在进行一个项目,尝试下载一个.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);
发布于 2013-08-26 14:14:42
“似乎”你试图从互联网下载图标,但你试图把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
,您不能像访问web资源那样访问它,相反,您需要使用设计用于与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();
对于我们最初的路径,这将导致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();
现在,这将导致outputfile
of secure.gravatar.com/favicon.ico
。我变得有点松懈,因为我不确定这是不是你的want...but,它确实对我有用.
现在,当你读文件的时候,你会做这样的事情.
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")
将生成无效的文件引用。
所以,再次,我分析了输入..。
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
所有这些都下载、写入和读取,没有错误。
https://stackoverflow.com/questions/18453855
复制相似问题