title: PrimeFaces uploadFile: how can I save the uploaded image file to some directory in web app and retrieve it back
body: <p>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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>my file upload listener:</p>
  
  <pre><code>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)) &gt; 0)
          out.write(buf, 0, len);
      is.close();
      out.close();
  }
  </code></pre>
  
  <p>my type backing bean that is saved to DB:</p>
  
  <pre><code>public class Type {
      private Integer typeId;
      private String typeName;
      private byte[] icon;
      private String iconFileName;
  
      // getters &amp; setters
      ...
  }
  </code></pre>
  
  <p>xhtml file uploading:</p>
  
  <pre><code>&lt;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}"&gt;
  &lt;/p:fileUpload&gt;
  </code></pre>
  
  <p>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.</p>