title: How I save and retrieve an image on my server in a java webapp
body: <p>Here with another question on Images ( which seems to be more difficult than I initialy predicted) I'm working on a java Web app with JSF 2.0 ( apache myFaces) and I want this app to be able to upload a picture to a destination on the server it's going to run on. I have a Windows r2 2008 Server running a mySQL Db, but I don't want to store the image in the db, I'd rather store it somewhere in the server and then just save the path as a string in the db. </p>
  
  <p>I was told this is the best way, but I can't seem to find an example on how to save it on the server. I run the app on the Apache tomcat Server as a WAR file. so I don't know if I have to save the file to a path on the server drive (i.e. C:\images) or a special folder in the project itself ( within the java, html files) any help at all is greatly appreciated. I'm totally lost and have been stuck the whole day trying to figure this out.</p>
  
  <p>The code I use to upload the image to the java class is this ( courtesy of CodyS):</p>
  
  <pre><code>InputStream is = uploadedFile.getInputStream();  
  byte[] buffer = new byte[(int) uploadedFile.getSize()];
  is.read(buffer); 
  File f = new File("C:\\temp\\" + this.patient.getPk() + ".jpeg");   
  f.createNewFile();  
  FileOutputStream fos = new FileOutputStream(f);  
  fos.write(buffer);  //This is where I write it to the C Drive
  fos.close();
  is.close(); 
  </code></pre>
  
  <p>instead of writing it to my C drive I'm going to run it on the server, but where should I store the image to later retriev and display in an xhtml file? I hope I'm being clear on what I need, let me know if I am not and I'll try to explain in another way.</p>