ImageIcon缓存有关问题

ImageIcon缓存问题
在2BizBox 里面选择Image的时候(比如上传图纸的时候),发现一个这样的问题:


第一次上传的时候,选择一个 test.png 文件。

然后把test.png 删除,然后重新建一个 test.png的文件,再次选择test.png的时候,得到的总是前一个图片。 但是如果重新登录就没有这个问题了,所以怀疑是ImageIcon 的缓存造成的。 所以改了一把方法:


 public static ImageIcon getSelectedImageIcon(Component parent) {
File file = getSelectedImageFile(parent);
if (file != null) {
try {
ImageIcon icon = new ImageIcon(file.toURI().toURL());
icon.getImage().flush();
icon = new ImageIcon(file.toURI().toURL());
return icon;
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
}
return null;
}




每次拿到图片后,都flush 一下,然后在重新拿。 不过这样的问题是导致每次拿了两次。

或者在每个用到的地方,调用flush方法,不过这样就会要改的地方很多。