title: uploadFile.getInputstream() throws java.nio.file.NoSuchFileException
body: <p>I use a JSF 2.2 and a Primefaces 5.3. I'd like to save a uploaded file on a disk. Below you can see how I try to do this. When I push a <code>Send</code> button I get a <code>java.nio.file.NoSuchFileException</code> exception (at the end of my post I also put a full stacktrace):</p>
  
  <blockquote>
    <p>ERROR [stderr] (default task-24) java.nio.file.NoSuchFileException: [PATH]\Wildfly_10\WILDFLY_HOME\standalone\tmp\MasterProject.war\undertow1357918758070690245upload</p>
  </blockquote>
  
  <p>When I trace the stacktrace, I see that the exception is caused by this line (I think so):</p>
  
  <p><code>try(InputStream input = uploadFile.getInputstream()){</code></p>
  
  <p>It's strange for me. I can get the name of the uploaded file (by <code>uploadFile.getFileName()</code>) but I can't get the size of the file (by <code>uploadFile.getSize()</code>) and I can't get the <code>InputStream</code> (by <code>uploadFile.getInputstream()</code>). In both cases I get the <code>NoSuchFileException</code> exception.</p>
  
  <p>Additional information: I've to upload the large files (i.e. 200 MB or more). Of course I get the same exception when I upload the small file.</p>
  
  <p>Have you got any idea why have I got this exception and how can I fix this issue?</p>
  
  <p>This's part of the page where I upload the file:</p>
  
  <pre><code>&lt;h:form&gt;
      &lt;p:growl id="messages" showDetail="true" /&gt;
      &lt;p:panelGrid id="panel" columns="2" styleClass="ui-noborder" columnClasses="rightalign,leftalign"&gt;
  
          &lt;p:outputLabel for="file" value="File:" /&gt;
          &lt;p:fileUpload id="file" fileLimit="1"
                      fileUploadListener="#{dataController.handleFileUpload}" 
                      mode="advanced" dragDropSupport="true" sizeLimit="1000000000" 
                      uploadLabel="Upload" cancelLabel="Delete" allowTypes="/(\.|\/)(csv|binetflow)$/" /&gt;                                             
          &lt;p:commandButton id="buttonSend" value="Send" 
                          action="#{dataController.send()}" update="messages"/&gt;                       
  
      &lt;/p:panelGrid&gt;                                              
  &lt;/h:form&gt;
  </code></pre>
  
  <p>This's the CDI bean which is the controller for the above page:</p>
  
  <pre><code>@Named
  @ViewScoped
  public class DataController implements Serializable {
  
      private static final long serialVersionUID = 1383572529241805730L;
  
      public void handleFileUpload(FileUploadEvent event){
  
          uploadFile=event.getFile(); 
  
          FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", event.getFile().getFileName() + " is uploaded"));
      }
  
      public void send(){
  
          try(InputStream input = uploadFile.getInputstream()){
  
              Path folder=Paths.get("F:/Files");
              String filename = FilenameUtils.getBaseName(uploadFile.getFileName()); 
              String extension = FilenameUtils.getExtension(uploadFile.getFileName());
              Path file = Files.createTempFile(folder, filename + "-", "." + extension);
              Files.copy(input, file, StandardCopyOption.REPLACE_EXISTING);
  
              FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "Uploaded file successfully saved in " + file));          
  
          } catch (IOException e1) {
              FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR", null));
              e1.printStackTrace();
          }   
      }
  
      private UploadedFile uploadFile;
  }
  </code></pre>
  
  <p>This's the full stacktrace:</p>
  
  <pre><code>10:40:45,822 ERROR [stderr] (default task-24) java.nio.file.NoSuchFileException: [PATH]\Wildfly_10\WILDFLY_HOME\standalone\tmp\MasterProject.war\undertow1357918758070690245upload
  
  10:40:45,823 ERROR [stderr] (default task-24)   at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
  
  10:40:45,823 ERROR [stderr] (default task-24)   at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
  
  10:40:45,823 ERROR [stderr] (default task-24)   at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
  
  10:40:45,823 ERROR [stderr] (default task-24)   at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230)
  
  10:40:45,823 ERROR [stderr] (default task-24)   at java.nio.file.Files.newByteChannel(Files.java:361)
  
  10:40:45,824 ERROR [stderr] (default task-24)   at java.nio.file.Files.newByteChannel(Files.java:407)
  
  10:40:45,824 ERROR [stderr] (default task-24)   at java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)
  
  10:40:45,824 ERROR [stderr] (default task-24)   at java.nio.file.Files.newInputStream(Files.java:152)
  
  10:40:45,824 ERROR [stderr] (default task-24)   at io.undertow.servlet.spec.PartImpl.getInputStream(PartImpl.java:63)
  
  10:40:45,824 ERROR [stderr] (default task-24)   at org.primefaces.model.NativeUploadedFile.getInputstream(NativeUploadedFile.java:45)
  
  10:40:45,824 ERROR [stderr] (default task-24)   at com.system.controller.DataController.send(DataController.java:163)
  
  10:40:45,824 ERROR [stderr] (default task-24)   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  
  10:40:45,825 ERROR [stderr] (default task-24)   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  
  10:40:45,825 ERROR [stderr] (default task-24)   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  
  10:40:45,825 ERROR [stderr] (default task-24)   at java.lang.reflect.Method.invoke(Method.java:498)
  
  10:40:45,825 ERROR [stderr] (default task-24)   at javax.el.ELUtil.invokeMethod(ELUtil.java:308)
  
  10:40:45,825 ERROR [stderr] (default task-24)   at javax.el.BeanELResolver.invoke(BeanELResolver.java:415)
  
  10:40:45,825 ERROR [stderr] (default task-24)   at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
  
  10:40:45,825 ERROR [stderr] (default task-24)   at com.sun.el.parser.AstValue.invoke(AstValue.java:285)
  
  10:40:45,825 ERROR [stderr] (default task-24)   at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
  
  10:40:45,826 ERROR [stderr] (default task-24)   at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
  
  10:40:45,826 ERROR [stderr] (default task-24)   at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
  
  10:40:45,826 ERROR [stderr] (default task-24)   at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
  
  10:40:45,826 ERROR [stderr] (default task-24)   at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
  
  10:40:45,826 ERROR [stderr] (default task-24)   at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
  
  10:40:45,826 ERROR [stderr] (default task-24)   at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
  
  10:40:45,826 ERROR [stderr] (default task-24)   at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
  
  10:40:45,827 ERROR [stderr] (default task-24)   at javax.faces.component.UICommand.broadcast(UICommand.java:315)
  
  10:40:45,827 ERROR [stderr] (default task-24)   at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
  
  10:40:45,827 ERROR [stderr] (default task-24)   at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
  
  10:40:45,827 ERROR [stderr] (default task-24)   at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
  
  10:40:45,827 ERROR [stderr] (default task-24)   at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
  
  10:40:45,827 ERROR [stderr] (default task-24)   at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
  
  10:40:45,828 ERROR [stderr] (default task-24)   at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
  
  10:40:45,828 ERROR [stderr] (default task-24)   at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
  
  10:40:45,828 ERROR [stderr] (default task-24)   at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
  
  10:40:45,828 ERROR [stderr] (default task-24)   at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
  
  10:40:45,829 ERROR [stderr] (default task-24)   at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
  
  10:40:45,829 ERROR [stderr] (default task-24)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  
  10:40:45,829 ERROR [stderr] (default task-24)   at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
  
  10:40:45,829 ERROR [stderr] (default task-24)   at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
  
  10:40:45,829 ERROR [stderr] (default task-24)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  
  10:40:45,830 ERROR [stderr] (default task-24)   at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
  
  10:40:45,830 ERROR [stderr] (default task-24)   at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
  
  10:40:45,830 ERROR [stderr] (default task-24)   at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
  
  10:40:45,830 ERROR [stderr] (default task-24)   at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
  
  10:40:45,831 ERROR [stderr] (default task-24)   at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
  
  10:40:45,831 ERROR [stderr] (default task-24)   at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
  
  10:40:45,831 ERROR [stderr] (default task-24)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  
  10:40:45,831 ERROR [stderr] (default task-24)   at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
  
  10:40:45,831 ERROR [stderr] (default task-24)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  
  10:40:45,832 ERROR [stderr] (default task-24)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  
  10:40:45,832 ERROR [stderr] (default task-24)   at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:284)
  
  10:40:45,832 ERROR [stderr] (default task-24)   at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:263)
  
  10:40:45,832 ERROR [stderr] (default task-24)   at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
  
  10:40:45,833 ERROR [stderr] (default task-24)   at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:174)
  
  10:40:45,833 ERROR [stderr] (default task-24)   at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
  
  10:40:45,833 ERROR [stderr] (default task-24)   at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:793)
  
  10:40:45,833 ERROR [stderr] (default task-24)   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  
  10:40:45,833 ERROR [stderr] (default task-24)   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  
  10:40:45,834 ERROR [stderr] (default task-24)   at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <h1>Update:</h1>
  
  <p>I noticed something new. If I move the contents of the <code>send()</code> method to the <code>handleFileUpload()</code> method (the code below) - the file is saved on the disk:</p>
  
  <pre><code>@Named
  @ViewScoped
  public class DataController implements Serializable {
  
      private static final long serialVersionUID = 1383572529241805730L;
  
      public void handleFileUpload(FileUploadEvent event){
  
          uploadFile=event.getFile(); 
  
          try(InputStream input = uploadFile.getInputstream()){
  
              Path folder=Paths.get("F:/Files");
              String filename = FilenameUtils.getBaseName(uploadFile.getFileName()); 
              String extension = FilenameUtils.getExtension(uploadFile.getFileName());
              Path file = Files.createTempFile(folder, filename + "-", "." + extension);
              Files.copy(input, file, StandardCopyOption.REPLACE_EXISTING);
  
              FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "Uploaded file successfully saved in " + file));          
  
          } catch (IOException e1) {
              FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR", null));
              e1.printStackTrace();
          }
  
          FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", event.getFile().getFileName() + " is uploaded"));
      }
  
      public void send(){
  
      }
  
      private UploadedFile uploadFile;
  }
  </code></pre>
  
  <p>But I'd like to save the uploaded file on the disk if the user push the <code>Send</code> button, not earlier (i.e. not when the user push the <code>Upload</code> button) so it's necessary to save the file in <code>send()</code> method. </p>
  
  <p>I also noticed that when I use a simple mode of the <code>&lt;p:fileUpload&gt;</code> component (the modified form below), everything work (i.e. the file is saved when the user push the button). I had to create getter and setter for the <code>uploadFile</code> object in the <code>DataController</code> bean to use <code>#{dataController.uploadFile}</code>. I had to add the <code>enctype="multipart/form-data"</code> attribute to the <code>&lt;h:form&gt;</code> component either (without this attribute it isn't work).</p>
  
  <pre><code>&lt;h:form enctype="multipart/form-data"&gt;
      &lt;p:growl id="messages" showDetail="true" /&gt;
      &lt;p:panelGrid id="panel" columns="2" styleClass="ui-noborder" columnClasses="rightalign,leftalign"&gt;
  
          &lt;p:outputLabel for="file" value="File:" /&gt;
          &lt;p:fileUpload id="file" value="#{dataController.uploadFile}" mode="simple" skinSimple="true" sizeLimit="1000000000" allowTypes="/(\.|\/)(csv|binetflow)$/"/&gt;
          &lt;p:commandButton value="Submit" ajax="false" actionListener="#{dataController.send()}" disabled="false" /&gt;                                                                                                                                          
  
      &lt;/p:panelGrid&gt;                                              
  &lt;/h:form&gt;
  </code></pre>
  
  <p>The problem of this solution is: I need the advanced mode of the <code>&lt;p:fileUpload&gt;</code> component. </p>
  
  <p>I don't understand why I can't access to the uploaded file in the <code>send()</code> method when I use the advanced mode. I use the <code>@ViewScoped</code> scope so it should work.</p>