how can i display a dynamic photo in the user detail's page [duplicate]

流过昼夜 提交于 2019-12-08 08:10:06

问题


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 ??


回答1:


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:

  • How to show the server path image to PrimeFaces p:graphicImage?
  • Simplest way to serve static data from outside the application server in a Java web application
  • Load images from outside of webapps / webcontext / deploy folder using <h:graphicImage> or <img> tag



回答2:


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




回答3:


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 .



来源:https://stackoverflow.com/questions/12715697/how-can-i-display-a-dynamic-photo-in-the-user-details-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!