public StreamedContent getPhoto() { byte[] photo = recipe.getPhoto(); if (photo != null && photo.length > 0) { return new DefaultStreamedContent(new ByteArrayInputStream(photo)); } return null; }
public StreamedContent getImage() throws IOException { FacesContext context = FacesContext.getCurrentInstance(); if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) { // So, we're rendering the HTML. Return a stub StreamedContent so that it will generate right // URL. return new DefaultStreamedContent(); } else { // So, browser is requesting the image. Return a real StreamedContent with the image bytes. Integer recipeId = Integer.parseInt(context.getExternalContext().getRequestParameterMap().get("recipeId")); Recipe recipe = service.getById(recipeId); byte[] photo = recipe.getPhoto(); if (photo == null) { return null; } return new DefaultStreamedContent(new ByteArrayInputStream(photo)); } }