title: How to read file using paths relative to context root in Jersey?
body: <p>I am learning REST using Jersey. I was successful in creating the cliche Hello World application. Now (just for learning purposes) I am trying to return an image file through a byte stream. However, I get an <code>IOException</code> that the file cannot be read. Following is my Resource class :</p>
  
  <pre><code>@Path("/image/{file}")
  public class ImageResource
  {
     @Context
     private UriInfo uri;
  
     @GET
     @Produces("image/jpg")
     public Response getFullImage(@PathParam("file") String fileName)
     {
        Response response = null;
        String contextRoot = getUri().getBaseUri().toString();
  
        try
        {
           BufferedImage image = ImageIO.read(new File(contextRoot + "/images/" + fileName));
           ByteArrayOutputStream baos = new ByteArrayOutputStream();
           ImageIO.write(image, "jpg", baos);
           byte[] imgBytes = baos.toByteArray();
  
           response = Response.ok(new ByteArrayInputStream(imgBytes)).build();
        }
        catch (IOException e)
        {
           System.err.println("Error reading image file..." + e.getMessage());
           e.printStackTrace();
        }
  
        return response;
     }
  
     private UriInfo getUri() {
        return uri;
     }
  
  }
  </code></pre>
  
  <p>The image is stored under <code>WebContent/images/image001.jpg</code> and I am using the URL : <code>http://localhost:8080/REST/image/image001.jpg</code> to hit the resource.</p>
  
  <p>Could someone please let me know the reason for the exception ???</p>