title: java.io.FileNotFoundException(Access denied) Error?But Files are getting Stored
body: <p>I am getting an error HTTP Status 500 - java.io.FileNotFoundException:
  C:\sam\IMAGES (Access is denied) while saving an image in the file 'IMAGES'.I tried all the things like changing the permission of the file and tried to save it in different folder.The strange thing is that my images are actually getting stored in the file which i mentioned above.But why still its shows the error?? how to resolve this error?Tomcat is under the ownership of user.</p>
  
  <pre><code>My Code:
  MySampleUpload.jsp
  &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form action="FileUploadServlet" method="post"enctype="multipart/form-data"&gt;
      &lt;div class="btn btn-success btn-file"&gt;
      &lt;i class="fa fa-cloud-upload"&gt;&lt;/i&gt;
               Browse
      &lt;input type="file" name="file" /&gt;
      &lt;/div&gt;
      &lt;button type="submit" value="submit" name='submit'&gt;submit&lt;/button&gt;`
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  
  FileUploadServlet.java
  
  
  
  import java.io.File;
  import java.io.IOException;
  
  import javax.imageio.stream.FileImageOutputStream;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.MultipartConfig;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.Part;
  import java.io.RandomAccessFile;
  
  /**
   * Servlet implementation class FileUploadServlet
   */
  @WebServlet("/FileUploadServlet")
  @MultipartConfig(fileSizeThreshold=1024*1024*10,    // 10 MB 
  maxFileSize=1024*1024*50,       // 50 MB
  maxRequestSize=1024*1024*100)       // 100 MB
  public class FileUploadServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;
  
      /**
       * @see HttpServlet#HttpServlet()
       */
      private static final String UPLOAD_DIR="IMAGES";
      public FileUploadServlet() {
          super();
          // TODO Auto-generated constructor stub
      }
  
  
      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
          response.getWriter().append("Served at: ").append(request.getContextPath());
      }
  
      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
          //gets absolute path of the web application
          //String applicationPath=request.getServletContext().getRealPath("");
          //construct path of the directory to save the uploaded file
          String UploadFilePath="C:/sam"+File.separator+UPLOAD_DIR;
          //creates the file directory if it doesn't exist
          File FileSaveDir=new File(UploadFilePath);
  
      if(!FileSaveDir.exists())
          {
              FileSaveDir.mkdirs();
          }
  
          System.out.println("Upload file Diiirectory"+FileSaveDir.getAbsolutePath());
          String fileName=null;
          //get all parts from request and write it to the file on the server
          for(Part part:request.getParts())
          {
              fileName=getFileName(part);
  
              part.write(UploadFilePath + File.separator + fileName);
  
          }
          System.out.println("File Uploaded successfully");
          request.setAttribute("message",fileName+"file uploaded successfully");
  
          getServletContext().getRequestDispatcher("/response.jsp").forward(request, response);
      }
      private String getFileName(Part part)
      {
      String ContentDisp=part.getHeader("Content-Disposition");
      System.out.println("content-disposition-header"+ContentDisp);
      String[] tokens = ContentDisp.split(";");
      for (String token : tokens) {
          if (token.trim().startsWith("filename")) {
              return token.substring(token.indexOf("=") + 2, token.length()-1);
  
          }
      } return "";
      }}
  </code></pre>