0

页:

<ui:repeat value="#{testController.photos}" var="item">
    <p:graphicImage value="#{item}"/>
</ui:repeat>

豆:

private TestEntity current;

public List getPhotos() {
    StreamedContent image;
    Collection rawPhotoCollection = current.getPhotoCollection();

    List<StreamedContent> imageList = new ArrayList<StreamedContent>(rawPhotoCollection);
    List<Photo> photoList = new ArrayList<Photo>(rawPhotoCollection);

    for (int i = 0; i < photoList.size(); i++) {
        byte[] data = photoList.get(i).getImage();
        ByteArrayInputStream is = new ByteArrayInputStream(data);
        image = new DefaultStreamedContent(is, "image/png");
        imageList.set(i, image);
    }

    return imageList;
}

TestEntity 有一组照片实体。我将照片集转换为照片数组列表。Photo 有一列 Image 声明为 BLOB。创建了一个循环来检索每个图像,以便我可以将其转换为 StreamedContent。将每个 StreamedContent 插入 imageList(StreamedContent 列表)返回 imageList 并将其显示为 p:graphicImage。

问题是,页面给了我一个破损图像的图标。它不显示图像。我很确定它有一个图像,因为如果我不使用 ui:repeat 并且只使用 p:graphicImage 显示选定的照片实体,它会显示正确的图像。

就像是:

页:

<p:graphicImage value="#{testController.firstImage}"/>

豆:

public StreamedContent getFirstImage() {
    Collection rawPhotoCollection = current.getPhotoCollection();
    List<Photo> photoList = new ArrayList<Photo>(rawPhotoCollection);
    byte[] data = photoList.get(0).getImage();
    ByteArrayInputStream is = new ByteArrayInputStream(data);
    StreamedContent image = new DefaultStreamedContent(is, "image/png");
    return image;
}

上面的代码有效。

4

0 回答 0