问题
I can´t display images from my database, they are stored as bytea and I am mapping them like this:
@Entity
@Table(name = "photograph", schema = "public")
public class Photograph{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "photograph_id", unique = true, nullable = false)
private Long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "diagnostic_id", nullable = false, insertable = false, updatable = false)
private Diagnostic diagnostic;
@Column(name = "photo", nullable = false)
private byte[] photo;
@Column(name = "photograph_description", nullable = false, length = 100)
private String photographDescription;
@Column(name = "photograph_content_type")
private String photographContentType;
//Getters and Setters...
}
I can store all the images in the database with no problem. The problem is when I want to show them in a p:dataTable like this:
<p:dataTable id="dataTableLoadedPhotos"
value="#{imageController.photographListUpdate}" var="image">
<p:column headerText="Fotografías cargadas" width="110">
<o:graphicImage value="#{imageStreamer.getById(image.photographId)}"
alt="#{msgs['label.diagnostic.photograph.notFound']}" />
</p:column>
</p:dataTable>
I am using a streamer based on The BalusC Code: ImageServlet and I tried to use o:graphicImage with no success, something is missing in mi code:
@ManagedBean
@ApplicationScoped
public class ImageStreamer {
@EJB
private PhotographService photographService;
public byte[] getById(Long id) {
try {
return photographService.getContent(id);
} catch (ServiceException e) {
FacesMessage mensaje = new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"Error al buscar la fotografía ", e.getMessage());
FacesContext.getCurrentInstance().addMessage(null, mensaje);
}
return null;
}
}
I also have a managed bean with @RequestScoped:
@ManagedBean
@RequestScoped
public class ImageController {
@EJB
private PhotographService photographService;
@ManagedProperty(value = "#{diagnosticDataManager}")
private DiagnosticDataManager diagnosticDataManager;
private List<Photograph> photographListUpdate = new ArrayList<Photograph>();
private Photograph selectedPhoto;
/**
*
*/
public ImageController() {
diagnosticDataManager = new DiagnosticDataManager();
}
@PostConstruct
public void init() {
if (diagnosticDataManager.getDiagnostic().getDiagnosticId() != null)
photographListUpdate = photographService
.findPhotosByDiagnostic(diagnosticDataManager
.getDiagnostic());
for (Photograph photograph : photographListUpdate) {
byte[] imageContent = org.apache.commons.codec.binary.Base64
.decodeBase64(photograph.getPhoto());
ExternalContext ec = FacesContext.getCurrentInstance()
.getExternalContext();
ec.getSessionMap()
.put(photograph.getId().toString(),
imageContent);
}
}
// Getters and setters....
}
I only see two rows in p:dataTable corresponding to the images preloaded with no image, just the message of "alt" attribute of the o:graphicImage is shown
Using firebug I only see this on each row:
<img alt="Imagen no encontrada" src="/patientdiagnostics/javax.faces.resource/ImageStreamer_getById.xhtml?ln=omnifaces.graphic&v=0&p=7">
I also tried something similar to Display database blob images in <p:graphicImage> inside <ui:repeat>
回答1:
I can store all the images in the database with no problem.
This was thus actually not true. Those images appear to be empty or filled with only zeroes.
How to grab the uploaded file content from a <p:fileUpload>
, head to the following answers:
- How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null
- How to insert uploaded image from p:fileUpload as BLOB in MySQL?
Unrelated to the concrete problem, that faces message which you're trying to add in the catch block of the streamer won't appear anywhere as the image request doesn't represent a JSF page. You'd better just log/mail it.
来源:https://stackoverflow.com/questions/30031643/when-i-use-ographicimage-the-image-is-not-displayed