0

I'm working on a web application using EJB (netbeans & glassfish server). I want to display pictures on my jsf page. These pictures are stored on my disk and I believe that my application cannot access the pictures using the absolute path. I've been looking for answers on the internet but i've not got a good result besides using servlets. Is there another way of doing it ?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Using primefaces - http://www.primefaces.org/showcase/ui/multimedia/graphicImage.xhtml you can show these images by feeding a fileInputSteam to DefaultStreamedContent. E.g new DefaultStreamedContent(new FileInputStream(new File("C:\Lukas\a.png")), "image/png"); You can also use omnifaces http://showcase.omnifaces.org/components/graphicImage and use return new FileInputStream(new File("/path/to/images", filename)); – Mahendran Ayyarsamy Kandiar Nov 29 '15 at 00:27
  • @MahendranAyyarsamyKandiar I have read this. I'm using primefaces, so the solution 2) and 3) could be ok but i don't want to use servlet and 2. didn't work out. – lukas irides Nov 29 '15 at 00:35
  • Sure. If you are using Primefaces then its great. You can point and then have this variable intialized in PostConstruct method as myFile = new DefaultStreamedContent(new FileInputStream(new File("C:\Lukas\a.png")), "image/png"); My advice is not to do this in getter methods. It will work fine. I also have another method using img tag and base 64 – Mahendran Ayyarsamy Kandiar Nov 29 '15 at 00:40
  • The method will return a DefaultStreamedContent and – lukas irides Nov 29 '15 at 00:45
  • Would it be a good practice to just store the pictures within the web app even if the files are large ? – lukas irides Nov 29 '15 at 00:50
  • Thanks @BalusC. In that case OP you can try what BalusC is doing in the getter method of above link. You can also try . You can get base64 string of image as org.apache.commons.codec.binary.Base64.encodeBase64String(byteArrayOfFileInDisk); You may lose resolution of image though. – Mahendran Ayyarsamy Kandiar Nov 29 '15 at 00:50
  • You might lose res on IE 8/9/ as it was blocking my src limit to 2083 charchters. So i has to limit my base 64 string to 2083 chars and lost resolution. good luck OP. – Mahendran Ayyarsamy Kandiar Nov 29 '15 at 01:02

1 Answers1

0

That depends on just where "on disk" these pictures are stored:

  • If the pictures are stored within the web application, Tomcat (or whatever servlet container you use) will make them available over HTTP, and you can use an ordinary <img src="path/to/your/image.jpg"> to include them into the page.
  • Otherwise, you'll have to write code to make them available over http, for instance through a servlet, or by using p:graphicImage.
meriton
  • 68,356
  • 14
  • 108
  • 175
  • By on disk, I meant that the pictures are stored outside the web application. I have no problem displaying the pictures that are within the web app. Would it be a practice to stored them within the web app ? The pictures are uploaded by users and each user can upload multiple pictures. – lukas irides Nov 29 '15 at 00:31
  • You might wish to put them in a separate "web application", so you can update your actual application easily. You'll also want to verify the kind of files you accept so your server can not be hacked by uploading a JSP. Backups may be something else to think about. Other than that, I think this would work nicely. – meriton Nov 29 '15 at 02:42