Primefaces 来自DB的图像不是';在JSF页面上显示

Primefaces 来自DB的图像不是';在JSF页面上显示,primefaces,Primefaces,我在blob中使用MySql db和图像,并尝试使用PrimeFaces galleria在我的JSF页面上显示它们。在我的页面上,我看到了这个画廊,但没有显示图像。我在每张图片的一个角落里只看到绿色的小图标。我只使用DB来解决这个问题。如果我的图像在我的文件系统中,一切都会好的。有人能解释为什么我看不到DB中的图像吗?我写了这段代码: 凯丹广场: <p:galleria value="#{imagesView.images}" var="image" panelWidth="500" p

我在blob中使用MySql db和图像,并尝试使用PrimeFaces galleria在我的JSF页面上显示它们。在我的页面上,我看到了这个画廊,但没有显示图像。我在每张图片的一个角落里只看到绿色的小图标。我只使用DB来解决这个问题。如果我的图像在我的文件系统中,一切都会好的。有人能解释为什么我看不到DB中的图像吗?我写了这段代码:

凯丹广场:

<p:galleria value="#{imagesView.images}" var="image" panelWidth="500" panelHeight="250" showCaption="true">
        <p:graphicImage value="#{image}" alt="Image Description" title="Title"/>
</p:galleria>
@ManagedBean
public class ImagesView {

    private List<StreamedContent> images;

    @PostConstruct
    public void init() {
        images = new ArrayList<>();
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        int itemId = Integer.parseInt(request.getParameter("item_id"));
        for (int i = 1; i <= 2; i++) {
            images.add(new GraphicImage().getImageFromDB(i, itemId)); //It's so simple because I need it just for test and that's all
        }
    }

    public List<StreamedContent> getImages() {
        return images;
    }
}
@RequestScoped
class GraphicImage implements Serializable {

    StreamedContent getImageFromDB(int id, int itemId) {
        if (FacesContext.getCurrentInstance().getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
            return new DefaultStreamedContent();
        } else {
            byte[] image = DBUtils.getImage(id, itemId);
            return new DefaultStreamedContent(new ByteArrayInputStream(image), "image/jpg");
        }
    }
}
public static byte[] getImage(int id, int itemId) {
        byte[] image = null;
        ResultSet resultSet = null;
        try (Connection connection = DataBase.getConnection();
             PreparedStatement statement = connection.prepareStatement("SELECT content FROM images WHERE id = ? AND item_id = ?")) {
            statement.setInt(1, id);
            statement.setInt(2, itemId);
            resultSet = statement.executeQuery();
            resultSet.next();
            image = resultSet.getBytes("content");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return image;
    }
DBUtils方法:

<p:galleria value="#{imagesView.images}" var="image" panelWidth="500" panelHeight="250" showCaption="true">
        <p:graphicImage value="#{image}" alt="Image Description" title="Title"/>
</p:galleria>
@ManagedBean
public class ImagesView {

    private List<StreamedContent> images;

    @PostConstruct
    public void init() {
        images = new ArrayList<>();
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        int itemId = Integer.parseInt(request.getParameter("item_id"));
        for (int i = 1; i <= 2; i++) {
            images.add(new GraphicImage().getImageFromDB(i, itemId)); //It's so simple because I need it just for test and that's all
        }
    }

    public List<StreamedContent> getImages() {
        return images;
    }
}
@RequestScoped
class GraphicImage implements Serializable {

    StreamedContent getImageFromDB(int id, int itemId) {
        if (FacesContext.getCurrentInstance().getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
            return new DefaultStreamedContent();
        } else {
            byte[] image = DBUtils.getImage(id, itemId);
            return new DefaultStreamedContent(new ByteArrayInputStream(image), "image/jpg");
        }
    }
}
public static byte[] getImage(int id, int itemId) {
        byte[] image = null;
        ResultSet resultSet = null;
        try (Connection connection = DataBase.getConnection();
             PreparedStatement statement = connection.prepareStatement("SELECT content FROM images WHERE id = ? AND item_id = ?")) {
            statement.setInt(1, id);
            statement.setInt(2, itemId);
            resultSet = statement.executeQuery();
            resultSet.next();
            image = resultSet.getBytes("content");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return image;
    }

这不是
p:graphicImage
的工作方式。您正试图使用字节数组显示图像,而此标记需要图像(或流内容)的URL


现在,您有2个选项(至少)

  • 从字节数组(图像数据)创建图像并存储 它位于web应用程序可以访问的位置。value属性仍应包含URL而不是字节数组

  • 不要在数据库中存储图像,而是存储图像名称(或 服务器上映像的路径(例如,您的数据库将保持不变) img001.png、img002 png等等,你知道它们都在img里面 然后,您所要做的就是更改图像URL

    <p:graphicImage value=#{imgUrl} />
    
    
    
  • 实际上,特别是对于大型图像,建议使用选项2,但即使图像不大,这种方法也具有不必将图像(字节数组)转换为文件的优点

    在HTML中,这将呈现为

    <img src="img/img001.png" >