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">
<h:graphicImage value="images?id=#{image.photographId}" style="width:100px;"
alt="#{msgs['label.diagnostic.photograph.notFound']}" />
</p:column>
</p:dataTable>
I am using a servlet based on The BalusC Code: ImageServlet and I tried to use o:graphicImage with no success, something is missing in mi code:
@WebServlet("/images/*") //<<<<<<<I am not sure if this is ok
public class ImageServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@EJB
private PhotographService photographService;
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// Get ID from request.
String imageId = request.getParameter("id");
// Check if ID is supplied to the request.
if (imageId == null) {
// Do your thing if the ID is not supplied to the request.
// Throw an exception, or send 404, or show default/warning image,
// or just ignore it.
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
// Lookup Image by ImageId in database.
// Do your "SELECT * FROM Image WHERE ImageID" thing.
Photograph image = null;
try {
image = photographService.findPhotoById(Long.valueOf(imageId));
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Check if image is actually retrieved from database.
if (image == null) {
// Do your thing if the image does not exist in database.
// Throw an exception, or send 404, or show default/warning image,
// or just ignore it.
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
// Init servlet response.
response.reset();
response.setContentType(image.getPhotographContentType());
response.setContentLength(image.getPhoto().length);
// Write image content to response.
response.getOutputStream().write(image.getPhoto());
}
}
As I am using maven, I have my app directory like this:
src
|----main
|----webapp
|----images
I don't have errors in my server.log, but I can't see the image in the page, what is missing in my code?
I also tried something similar to Display database blob images in <p:graphicImage> inside <ui:repeat>