title: How to save the image in my project folder using servlets?
body: <pre><code>&lt;body&gt;
      &lt;form method="post" action="FileUploadServlet" enctype="multipart/form-data" name="form1"&gt;
          &lt;input type="file"  name="file" /&gt;
          &lt;input type="submit" value="upload"/&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>This is my index.jsp,when i submit it goes to FileUploadServlet.i.e.,</p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
      if (isMultiPart) {
          ServletFileUpload upload = new ServletFileUpload();
          try {
              FileItemIterator itr = upload.getItemIterator(request);
              while (itr.hasNext()) {
                  FileItemStream item = itr.next();
                  if (item.isFormField()) {
                      String fieldname = item.getFieldName();
                      InputStream is = item.openStream();
                      byte[] b = new byte[is.available()];
                      is.read(b);
                      String value = new String(b);
                      response.getWriter().println(fieldname + ":" + value + "&lt;/br&gt;");
                  } else {
                      String path = getServletContext().getRealPath("/");
                      if (FileUpload.processFile(path, item)) {
                          response.getWriter().println("Upload Success");
                      } else {
                          response.getWriter().println("Upload Failed");
                      }
                  }
              }
          } catch (FileUploadException fue) {
              fue.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>This is my Servlet and  the following code is FileUpload java class</p>
  
  <pre><code>public class FileUpload {
  
  public static boolean processFile(String path, FileItemStream item) {
      try {
          File f = new File(path + File.separator + "images");
          if(!f.exists()) {
              f.mkdir();
          }
          File savedFile=new File(f.getAbsolutePath()+File.separator+item.getName());
          FileOutputStream fos=new FileOutputStream(savedFile);
          InputStream is=item.openStream();
          int x=0;
          byte[]b=new byte[1024];
          while((x=is.read(b))!=-1){
              fos.write(b,0,x);
          }
          fos.flush();
          fos.close();
          return true;
      }catch(Exception e){
          e.printStackTrace();
      }
      return false;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is working fine but the problem is when image file is uploaded that are saving in `C:\Users\XYZ\Documents\NetBeansProjects\ImageUpload\build\web\images) but my requirement is image has to store inside project folder called images i.e.,(C:\Users\xyz\Documents\NetBeansProjects\ImageUpload\web\images),can you guide me where to change the code.</p>
  
  <p>Thanks in advance.</p>