请我遇到一个 JEE 问题。我想读取位于 C:\tmp\ 文件夹中的两个图像的问题。我为此使用了以下方法:
// array of supported extensions (use a List if you prefer)
static final String[] EXTENSIONS = new String[]{
"gif", "png", "jpeg", "jpg","bmp" // and other formats you need
};
// File representing the folder that you select using a FileChooser
File dir = new File("C:\\tmp\\");
final File[] fs = dir.listFiles(IMAGE_FILTER);
ArrayList<Image> images = new ArrayList<Image>(100);
// filter to identify images based on their extensions
static final FilenameFilter IMAGE_FILTER = new FilenameFilter() {
@Override
public boolean accept(final File dir, final String name) {
for (final String ext : EXTENSIONS) {
if (name.endsWith("." + ext)) {
return (true);
}
}
return (false);
}
};
public Image image() {
BufferedImage img = null;
if (dir.isDirectory()) { // make sure it's a directory
for (final File f : dir.listFiles(IMAGE_FILTER)) {
try {
img = ImageIO.read(f);
// you probably want something more involved here
// to display in your UI
/* System.out.println("image: " + f.getName());
System.out.println(" width : " + img.getWidth());
System.out.println(" height: " + img.getHeight());
System.out.println(" size : " + f.length());*/
images.add(img);
} catch (final IOException e) {
// handle errors here
}
}
}
return img;
}
///////////////
然后在jsf文件中我把这样的代码:
<ui:repeat value="#{imagesBean.images}" var="s" >
<h:graphicImage value="s" />
</ui:repeat>
结果我得到了图像的图标而不是图像..你能支持..ty