title: Tomcat web app can't write file on network
body: <p>I've this web application on Tomcat 7.0.42 on Windows that is instructed to copy a file on a locally mapped network directory, for example <code>N:\some\directory\file.txt</code>, but it's not able to do so.</p>
  
  <p>To define the output file, I'm using the URI syntax <code>file:///n:/some/directory/file.txt</code>, but both <code>Files.copy</code> and <code>FileUtils.copyFile</code> throw an <code>IOException</code> with some not-so-useful error messages:</p>
  
  <pre><code>URI desturi = new URI(srcpath);
  File dest = new File(desturi);
  Files.copy(source.toPath(), dest.toPath());
  // error message: "c:\local\dir\file.txt -&gt; n:\some\directory\file.txt"
  
  FileUtils.copyFile(source, dest);
  // error message: "Destination 'N:\some\directory' directory cannot be created"
  </code></pre>
  
  <p>Some additional info:</p>
  
  <ul>
  <li>Of course I <em>can</em> read and write on that directory</li>
  <li>An executable .jar can copy the file with no problem</li>
  <li>If the destination is in a local drive, everything is fine</li>
  <li>I don't think the security manager is loaded, but how can I check it?</li>
  <li>Tomcat is started with the same user I use to login (e.g. <code>user.name</code> is the same as the <code>USERNAME</code> environment variable)</li>
  </ul>
  
  <p>I'm out of ideas...</p>
  
  <p><strong>Update</strong> Snippets of the stack traces. For <code>Files.copy</code>:</p>
  
  <pre><code>java.nio.file.NoSuchFileException: c:\local\dir\file.txt -&gt; n:\some\directory\file.txt
      at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
      at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
      at sun.nio.fs.WindowsFileCopy.copy(Unknown Source)
      at sun.nio.fs.WindowsFileSystemProvider.copy(Unknown Source)
      at java.nio.file.Files.copy(Unknown Source)
      at it.augea.print.server.MyClass.copyFileToPath(MyClass.java:886)
      at it.augea.print.server.MyServlet.doPost(MyServlet.java:126)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  </code></pre>
  
  <p>For <code>FileUtils.copyFile</code>:</p>
  
  <pre><code>java.io.IOException: Destination 'N:\some\directory' directory cannot be created
      at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1015)
      at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:968)
      at it.augea.print.server.MyClass.copyFileToPath(MyClass.java:886)
      at it.augea.print.server.MyServlet.doPost(MyServlet.java:126)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  </code></pre>
  
  <p>Apache Commons.io is v2.2, by the way. Looking at the source code, these are the involved lines:</p>
  
  <pre><code>File parentFile = destFile.getParentFile();
  if (parentFile != null)
      if (!parentFile.mkdirs() &amp;&amp; !parentFile.isDirectory())
          throw new IOException("Destination '" + parentFile + "' directory cannot be created");
  </code></pre>