produto = produtoDAO.foto();
img = ImageIO.read(new ByteArrayInputStream(produto.getFoto()));
File file = new File("nome");
boolean write = ImageIO.write(img, "png", file);
FileInputStream fi = new FileInputStream(file);
imagem = new DefaultStreamedContent(fi);
Mas dessa forma é mostrada apenas uma imagem, então para mostrar todas as imagens do banco eu fiz assim:
Controllerpublic void visualizaFoto() throws IOException {
ProdutoDAO produtoDAO = new ProdutoDAO();
try {
lista = produtoDAO.getProduto();
Iterator it = lista.iterator();
while(it.hasNext()) {
mostraProduto = (Produto) it.next();
img = ImageIO.read(new ByteArrayInputStream(mostraProduto.getFoto()));
File file = new File("nome");
boolean write = ImageIO.write(img, "png", file);
FileInputStream fi = new FileInputStream(file);
imagem = new DefaultStreamedContent(fi);
mostraProduto.setImagem(imagem);
mostraListaProduto.add(mostraProduto);
}
} catch(Exception e) {
e.printStackTrace();
}
public class Produto {
private Integer id = null;
private String nome = null;
private String descricao = null;
private byte[] foto = null;
private StreamedContent imagem = null;
public StreamedContent getImagem() {
return imagem;
}
public void setImagem(StreamedContent imagem) {
this.imagem = imagem;
}
public byte[] getFoto() {
return foto;
}
public void setFoto(byte[] foto) {
this.foto = foto;
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
<ui:repeat var="lista" value="#{produtoController.mostraListaProduto}">
<td>
<p:fieldset legend="#{lista.nome}" style="width: 205px;
background:url(../img/transparencia.png); border-color: #ffffff;
margin-left: 120px; margin-top: 10px">
<h:panelGrid columns="1" cellpadding="10">
<p:graphicImage value="#{lista.imagem}" height="150px" width="200px"/>
<h:outputLabel value="#{lista.descricao}"/>
</h:panelGrid>
</p:fieldset>
</td>
</ui:repeat>