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

旧城冷巷雨未停 提交于 2019-12-06 16:11:59
BalusC

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:

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

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 .

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