0

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>

Community
  • 1
  • 1
Danny P
  • 93
  • 2
  • 13
  • The servlet's doGet() is not invoked, I checked by putting a break point at the begining of the method. I also try value="#{request.contextPath}/images?id=#{image.photographId}" and after I look at browsers' HTTP console, I saw `Imagen no encontrada`. Now I note that there is **/patientdiagnostics/patientdiagnostics/** twice (before there was only once in the src attribute) – Danny P May 04 '15 at 14:39
  • In browser HTTP traffic monitor it says 404 at `http://localhost:8080/patientdiagnostics/patientdiagnostics/images?id=11` – Danny P May 04 '15 at 14:45
  • I removed `#{request.contextPath}` and at browser's network console it still says 404. – Danny P May 04 '15 at 14:57
  • URL is `http://localhost:8080/patientdiagnostics/pages/images?id=11` right after I note that _pages_ is in the URL, I have created an images folder inside pages folder, but I still see 404 – Danny P May 04 '15 at 15:08
  • Use `` instead. – BalusC May 04 '15 at 15:10
  • Yes, indeed ``. – BalusC May 04 '15 at 16:26
  • my p:dataTable is empty now, I replaced through HTTP console, – Danny P May 04 '15 at 17:22
  • Thank you BalusC you pointed me to the right direction, I was wrong saving my images into the db, now I can watch them through o:graphicImage instead of h:graphicImage and thanks to this question [link](http://stackoverflow.com/questions/8528671/primefaces-fileupload-to-byte) – Danny P May 06 '15 at 00:49

1 Answers1

-2

This is not really and true answer but may help, in case of database not being necessary depending on you IDE you may store pictures directly with your source code files and directly access them with:

getClass().getResoure(name of image.fileType); 
//fileType example png jpg gif so image.png`

examples:

ImageIcon icon = new ImageIcon(getClass().getResource("Icon.png"));
BufferedImage bf = ImageIO.read(getClass().getResource("image.png"));

to paint buffer image

ga.drawImage(image, int distFromLeft, int distFromTop, horrizontalSize, verticleSize, null)

btgui
  • 7
  • 5