0

I have a web app and want to save images to my DB and then want retrieve the saved image back from the DB. I am able to upload the image to my web app by PrimeFaces fileUpload and save image properties to my DB. However, I cannot save the uploaeded image to some directory in my web app so that later I can retrieve its path and properties from DB and the image from the directory. I am getting the following error:

javax.el.ELException: /compositions/defineType.xhtml @122,56 fileUploadListener="#{typeBean.handleTypeImageUpload}": java.io.FileNotFoundException: C:\Users\mta\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\MyWebApp\webapp\images\uploadedImages\alarmPassive.gif

my file upload listener:

public void handleTypeImageUpload(FileUploadEvent event) throws IOException {  
    UploadedFile file = event.getFile();
    Type newTypeProp = new Type();
    newTypeProp .setTypeName("someName");
    newTypeProp .setIconFileName(imageFile.getFileName());
    newTypeProp .setIcon(imageFile.getContents());

    databaseDao.saveType(newTypeProp );

    FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");  
    FacesContext.getCurrentInstance().addMessage(null, msg);  

    String prefix = FilenameUtils.getBaseName(file.getFileName());
    String suffix = FilenameUtils.getExtension(file.getFileName());

    File fileToDirectory = File.createTempFile(prefix + "-", "." + suffix, new File("C:\\temp"));

    InputStream is = event.getFile().getInputstream();
    OutputStream out = new FileOutputStream(fileToDirectory);
    byte buf[] = new byte[1024];
    int len;
    while ((len = is.read(buf)) > 0)
        out.write(buf, 0, len);
    is.close();
    out.close();
}

my type backing bean that is saved to DB:

public class Type {
    private Integer typeId;
    private String typeName;
    private byte[] icon;
    private String iconFileName;

    // getters & setters
    ...
}

xhtml file uploading:

<p:fileUpload style="font-size:11px;" fileUploadListener="#{typeBean.handleTypeImageUpload}" 
    mode="advanced" dragDropSupport="true"  
    sizeLimit="100000" fileLimit="3" update="messages"
    allowTypes="/(\.|\/)(gif|jpe?g|png)$/" 
    label="#{general.select}" uploadLabel="#{general.upload}" 
    cancelLabel="#{general.cancel}">
</p:fileUpload>

Now, how can I save the image file to some directory in my web app so that later I can get it? Any help would greatly be appreciated.

nudastack
  • 155
  • 1
  • 5
  • 18
  • Sir, I wrote my problems in detail and state what I really wanted. Could you please check it if possible. – nudastack Nov 07 '13 at 09:00
  • 2
    Now this question is a duplicate of among others http://stackoverflow.com/questions/14211843/how-to-save-uploaded-file/14214223#14214223 and http://stackoverflow.com/questions/18664579/recommended-way-to-save-files-uploaded-to-a-tomcat-servlet/18664715#18664715 – BalusC Nov 07 '13 at 09:14
  • Finally, I am able to save the image file to local computer and server by specifying a directory. However, when loading the xhtml page **p:graphicImage** can't accept the saved path as follows: **"C:\TrackerWeb\MtsTrackerWebApp\uploadedImages\vehicleTypeImages\alarmPassive.gif"**. Is this path wrong? – nudastack Nov 07 '13 at 12:42
  • Actually, I wanted to save the image files into the project webapp directory but the posts you have shared above considers that as a bad approach. I know I can easily feed **p:graphicImage** path from my project directory. What I did here was I created the same directories in my local machine and server. This way I save the image properties to DB and get the real image file from the created directories. Which way should I choose. The local disk file system path or the path inside my webapp project? – nudastack Nov 07 '13 at 12:55
  • I read them. That is why I was able to upload and save the file but there are bunch of links. As far as I understood I should add a context to my Tomcat server.xml. – nudastack Nov 07 '13 at 13:28
  • If I add a context like **** to Tomcat server.xml, would it a bad design? – nudastack Nov 07 '13 at 13:31
  • Sir, I really got confused. Sorry for bothering you over and over. What I did was I add a context like **** to Tomcat server.xml. Then I wanted to load it to a **p:graphicImage** as **** where iconFileName is exact image name in that directory. The image is saved there. However, the image is not loaded in p:graphicImage. What am I missing? – nudastack Nov 07 '13 at 14:20

0 Answers0