title: File uploading even after Exception (FileNotFoundException (Access is Denied))
body: <p>I have created a servlet that uploads a file, selected by user from a form.</p>
  
  <p>HTML Form snippet is:</p>
  
  <pre><code>    &lt;form method="POST" enctype="multipart/form-data" action="UploadFile"&gt;
  &lt;div class="form-group"&gt;
      &lt;label for="inputModuleName"&gt;Module Name&lt;/label&gt;
      &lt;input type="text" class="form-control" id="inputModuleName" placeholder="Module Name"&gt;
  &lt;/div&gt;
  &lt;div class="form-group"&gt;
      &lt;label for="exampleInputFile"&gt;File input&lt;/label&gt;
      &lt;input type="file" id="exampleInputFile" name="fileName"&gt;
      &lt;p class="help-block"&gt;Only .docx &amp; .pdf files&lt;/p&gt;
  &lt;/div&gt;
      &lt;div class="form-group"&gt;
      &lt;label for="inputComment"&gt;Comments&lt;/label&gt;
      &lt;textarea class="form-control" rows="4" placeholder="Write here..."&gt;&lt;/textarea&gt;
  &lt;/div&gt;
  &lt;button name="uploadButton" class="btn btn-default"&gt;Upload&lt;/button&gt;
  </code></pre>
  
  <p>
  Note: Only file type input is relevant here as it is a test page. No other inputs fields are processed as per now.</p>
  
  <p>and Servlet code is :</p>
  
  <pre><code>    import java.io.*;
      import java.sql.*;
  
      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.annotation.MultipartConfig;
      import javax.servlet.*;
      import javax.servlet.http.*;
  
      @MultipartConfig(fileSizeThreshold=1024*1024*2, maxFileSize=1024*1024*5)
      public class UploadFile extends HttpServlet {
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
      PrintWriter out = response.getWriter();
  
      String appPath = request.getServletContext().getRealPath("/");
      String saveDir = getServletContext().getInitParameter("uploaddir");
      String savePath = appPath + saveDir;
  
      File fileSaveDir = new File(savePath);
  
      try{
          for(Part part : request.getParts()){
              String fileName = extractFileName(part);
              part.write(savePath + File.separator + fileName);
          }
          out.println("File uploaded!!!");
      }catch(Exception e){
          e.printStackTrace();
      out.println("Error: Can't Upload");
      }
  }
  
  private String extractFileName(Part part) {
      String contentDisp = part.getHeader("content-disposition");
      String[] items = contentDisp.split(";");
      for (String s : items) {
          if (s.trim().startsWith("filename")) {
              return s.substring(s.indexOf("=") + 2, s.length()-1);
          }
      }
      return "";
  }
  }
  </code></pre>
  
  <p>When I submit the form it always throws an Exception and I get the following output in the console but file gets uploaded to the folder:</p>
  
  <pre><code>java.io.IOException: java.io.FileNotFoundException: C:\tomcat\webapps\proto1\filesupload (Access is denied)
      at org.apache.catalina.core.ApplicationPart.write(ApplicationPart.java:121)
      at UploadFile.doPost(UploadFile.java:30)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
      at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2466)
      at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2455)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Thread.java:745)
      Caused by: java.io.FileNotFoundException: C:\tomcat\webapps\proto1\filesupload (Access is denied)
      at java.io.FileOutputStream.open(Native Method)
      at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:213)
      at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:162)
      at org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.write(DiskFileItem.java:377)
      at org.apache.catalina.core.ApplicationPart.write(ApplicationPart.java:119)
      ... 24 more
  </code></pre>
  
  <p>How to resolve this issue ?</p>
  
  <p>Some points to consider: </p>
  
  <ul>
  <li>I am using Windows 8.1 OS with Apache Tomcat 7 server.</li>
  <li><code>saveFileDir.canRead()</code> and <code>saveFileDir.canWrite()</code> both return <code>true</code>.</li>
  <li>edited permission from security settings of folder to 'full modify'.</li>
  </ul>