1

Possible Duplicate:
Display image from database with p:graphicImage

I want to display a photo from its url stored in the database , so i used as follow

                <h:panelGrid columns="1">
                    <p:graphicImage value="#{userController.photo}"/> 
                </h:panelGrid>

and the photo attribute is a url string that points to the photo location

public String getPhoto() {
   String path = new File("").getAbsolutePath();
   ph =ejbPhoto.find(current.getIdPhoto()); 
   System.out.println("+PHOTO +++++++++++++++"+path+"\\"+ph.getPhotoName()); 
   return path+"\\"+ph.getPhotoName();
}

the system.out.println shows the right path of the image but in the display page the photo is not displayed, So any idea ??

Community
  • 1
  • 1
MarwaInsat
  • 293
  • 1
  • 4
  • 17

3 Answers3

3

First, new File("").getAbsolutePath() returns the current working directory. I.e. the local disk file system directory which was opened at the moment the command was given to start the server. This value is not guaranteed to be the same everytime the server is started. It depends on the way how the server is started. E.g. in an IDE, or in a command console, or by a service, etc.

You should absolutely not let your business logic rely on such an environmental inconsistence.

Second, the generated HTML <img src> has to point to an URL (a HTTP path), not to a local disk file system path. It's namely the webbrowser who has got to download the image individually by an URL like http://example.com/contextname/images/foo.png (and thus not the webserver who has to automagically embed them in the HTML code or so). Your current code approach would only work if the new File("") points to the public web content folder of the deployed webapp, which is very, very unlikely.

You should make sure that the <img src> points to a valid HTTP URL, exactly the one which you can copypaste in webbrowser's address bar.


It's unclear why you've designed it like that and what your business restrictions are, so it's not possible to post a suitable answer how to properly solve it. So here are several links showing (slight) different ways of solving this:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • +1 for the " You should absolutely not let your business logic rely on such an environmental inconsistence." – siebz0r Oct 04 '12 at 05:09
1

My two cents. Using image URLs may expose your directory structure to anyone with firebug or something similar. A number of bad things can happen thus, I'm not a fan of using filepaths relating to my application server's file system if I can help it. If your implementation supports it, I'd advise you serve the images from the database using primefaces' DynaImage

kolossus
  • 19,953
  • 3
  • 45
  • 94
  • The OP can also use dynamic images if they are stored on disk. I'm also skeptical about the security issue. – siebz0r Oct 04 '12 at 05:11
-1

the solution is simple and the path of the photo is under the web folder of the netbeans project (Project\build\web\photo)

<h:panelGrid columns="1" width="50%" >
                    <p:graphicImage value="/photo/#{userController.photo}"/> 
  </h:panelGrid>

and in the photo get method i put :

public String getPhoto() {

   ph =ejbPhoto.find(current.getIdPhoto()); 
   if(ph==null) return "noPhoto.png";
   System.out.println(ph.getPhotoName()); 
   current=null;
    return ph.getPhotoName();
}

and the photo name includes the extension .

MarwaInsat
  • 293
  • 1
  • 4
  • 17
  • This may 'work' for yoy, but this is unreliable. Please see BalusC's answer. – siebz0r Oct 04 '12 at 05:07
  • You're going completely the wrong direction. Please stop before it's too late (you'll most certainly get bugged/fired once you bring such a major conceptual mistake into production environment). – BalusC Oct 04 '12 at 22:48