title: Servlet interaction with filesystem: storing files and generating URLs for clientside access
body: <p><strong>SUMMARY</strong></p>
  
  <ol>
  <li>I need to store both uploaded and server-generated images, with portable and
  predictable paths so my server code is aware of where these images exist.</li>
  <li>I need to generate URLs to these images that can be sent to the client. These URLs will be used in HTML image elements.</li>
  </ol>
  
  <p><strong>PROBLEM</strong></p>
  
  <p>My web application allows the user to upload an image, using gwtupload(Apache Commons). This image is stored on the server and a URL returned to the client so the image is shown. gwtupload provides a clientside method to get this URL for the uploaded image. This works in deployment; the following aspects do not:</p>
  
  <ul>
  <li>In certain cases, the uploaded image must be cropped. This results in
  a new image being generated by a servlet method. I want to store this
  cropped image and return(to the client) an access URL.</li>
  <li>Then, this    image is used by another method to generate another
  image. This    method must know the location on the file system of
  the previously    uploaded(and/or cropped) image. A URL for the new
  image must then be    returned to client.</li>
  </ul>
  
  <p>I have implementation working perfectly in GWT development mode. However, as I expected, after deployment to my own Tomcat server, the remote services fail due to my confusion regarding the file system. I do not know the correct way of storing these images in a predictable place on the server filesystem, nor do I know how to generate access URLs(for files residing outwith the WAR, as these images will.)</p>
  
  <p>All these images are only needed for the current session, so the locations can be temporary directories. I have spent two days experimenting and trawling the web for a solution to no avail.</p>
  
  <p>I will post abridged code below. This is my attempt to simply use the working directory and relative pathnames. Using the Eclipse debugger attached to my servlet container, I could see the results of String dataDir = context.getRealPath("foo") indicating a temp folder within the servlet: but when I navigated there using explorer, NONE of the files had been written to the disk. I am very confused.</p>
  
  <pre><code>public String[] generatePreview(String xml) {
  
      PreviewManager manager = new PreviewManager();
      String url;
  
      try{       
          preview = manager.generatePreview(xml);
      }
      catch (Exception e){e.printStackTrace();}
  
      //Create the preview directory
      File folder = new File("previews");
      if (!folder.exists()) folder.mkdir();
  
      //The file to be written to
      File output = new File(folder, "front.jpg");
  
      ServletContext context = getServletContext();
      String dataDir = context.getRealPath("previews");
  
      try {
           ImageIO.write(image, "jpg", output);
      } catch (IOException e) {
           e.printStackTrace();
      }
  
      url = "previews/" + output.getName();
  
      return url;
  }
  
  @Override
  public String cropBackground(int[] coord_pair, String relativePath) {
  
      File backgroundsFolder = new File("backgrounds");
      if (!backgroundsFolder.exists()) backgroundsFolder.mkdir();
  
      ServletContext context = getServletContext();
      String dataDir = context.getRealPath("backgrounds");
  
      File current = new File(relativePath);
      String croppedName = "cropped_" + relativePath.replace("./backgrounds/", "");
  
      int x = coord_pair[0];
      int y = coord_pair[1];
      int width = coord_pair[2];
      int height = coord_pair[3];
      String croppedPath = null;
  
      try {
          croppedPath = imageCropper.createCroppedImage(current, "backgrounds",      croppedName, x, y, width, height);
      }
      catch (IOException e){
          e.printStackTrace();
      }
  
      current.delete();
      return "backgrounds/" + croppedPath;
  </code></pre>
  
  <p>I am aware that my current 'return' statements would never work in deployment: I need to generate the URLs properly and return as strings. I'm sorry about the question length but I wanted to make my problem clear. </p>