title: java.io.FileNotFoundException: (No such file or directory) - Working with servlets
body: <p>I'm working with hibernate and tomcat. What I did is create a form which has a file input. In the servlet I want to cast that file to a byte[]. But when I want to create de File, there's no directory: it only saves the image's name, not the path. Here is the error:</p>
  
  <blockquote>
    <p>java.io.FileNotFoundException: oldi.png (No such file or directory)
      at java.io.FileInputStream.open(Native Method)  at
    java.io.FileInputStream.(FileInputStream.java:138)  at
    control.servlets.AddGenericItem.doPost(AddGenericItem.java:46)    at
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)  at
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)  at
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
      at
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
      at
    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
      at
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
      at
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      at
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
      at
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
      at
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      at
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
      at
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
      at
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
      at
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:724)</p>
  </blockquote>
  
  <p>The HTML:</p>
  
  <pre><code>&lt;tr&gt;
      &lt;td&gt;
          &lt;font class="text_title"&gt;Image: &lt;/font&gt;
      &lt;/td&gt;
      &lt;td&gt;
          &lt;input name="image" type="file"&gt;
      &lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
      &lt;td&gt;
          &lt;input type="submit" value="submit" name="ctl00$MainContent$RegisterUserWizard$CreateUserStepContainer$CreateButton"
                 id="ctl00_MainContent_RegisterUserWizard_CreateUserStepContainer_CreateButton"
                 style="border-width:0px;"&gt;
      &lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>Servlet:</p>
  
  <pre><code>public class AddGenericItem extends HttpServlet {
      @Override
      protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
              throws ServletException, IOException {
  
  //        HttpSession session = httpServletRequest.getSession();
          DatabaseManager databaseManager = new DatabaseManager();
  
          String name = httpServletRequest.getParameter("name");
          String description = httpServletRequest.getParameter("description");
          float price = Float.parseFloat(httpServletRequest.getParameter("price"));
  
          File image = new File(httpServletRequest.getParameter("image"));
          byte[] imageArray = new byte[(int) image.length()];
          try {
              FileInputStream fileInputStream = new FileInputStream(image);
              fileInputStream.read(imageArray);
              fileInputStream.close();
          } catch (Exception e) {
              e.printStackTrace();
          }
          Admin admin = (Admin) httpServletRequest.getSession().getAttribute("user");
          Brand brand = admin.getBrand();
  
          GenericItem genericItem = new GenericItem(name, description, imageArray, price,brand);
  
          GenericItemDAO genericItemDAO = databaseManager.getGenericItemDAO();
  
          genericItemDAO.beginTransaction();
          genericItemDAO.save(genericItem);
  
          try {
              genericItemDAO.commitTransaction();
          } catch (DatabaseAccessFailException e) {
              e.getStackTrace();
          } finally {
              httpServletResponse.sendRedirect("/adminPages/items.jsp");
          }
      }
  
      @Override
      protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
              throws ServletException, IOException {
  
      }
  
      @Override
      protected void doDelete(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
              throws ServletException, IOException {
  
      }
  
  }
  </code></pre>