title: uploading file using ajax and servlet
body: <p>i'm getting message "File not found". please help me.
  here id my code</p>
  
  <p><strong>index.jsp</strong></p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Ajax File Upload&lt;/title&gt;
   &lt;script type="text/javascript" src="jquery-1.4.2.min.js"&gt;&lt;/script&gt;
  
          &lt;script type="text/javascript"&gt;
              $(document).ready(function(){
                  $("#login_frm").submit(function(){
  
                      //remove previous class and add new "myinfo" class
                      $("#msgbox").removeClass().addClass('myinfo').text('Validating Your Form ').fadeIn(1000);
  
  
                      this.timer = setTimeout(function () {
                          $.ajax({
                              url: 'uploads',
                              enctype: 'multipart/form-data',
                              data: 'filename='+ $('#file').val(),
                              type: 'post',                            
                              success: function(msg){
                                   $("#msgbox").removeClass().addClass('myinfo').text(msg).fadeIn(1000);
                              }
  
                          });
                      }, 200);
                      return false;
                  });
  
              });
  
          &lt;/script&gt;
   &lt;link href="style.css" rel="stylesheet" type="text/css" /&gt;
   &lt;link href="login_style.css" rel="stylesheet" type="text/css" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form name="login_frm"  enctype="multipart/form-data" id="login_frm" action="" method="post"&gt;
             &lt;div id="login_box"&gt;
                  &lt;div id="login_header"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Citizen Login &lt;/div&gt;
                  &lt;div id="form_val" style="background-color:black; height:80px;"&gt;
                      &lt;div class="label"&gt;Upload Pic :&lt;/div&gt;
                      &lt;div class="control"&gt;&lt;input type="file" name="file" id="file"/&gt;&lt;/div&gt;
                      &lt;div id="msgbox"&gt;&lt;/div&gt;
                  &lt;/div&gt;
                    &lt;div id="login_footer"&gt;
                            &lt;label&gt;
                          &lt;input type="submit" name="upload" id="upload" value="Upload" class="send_button" /&gt;
                      &lt;/label&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>now, this is my servlet </p>
  
  <p><strong>uploads.java</strong></p>
  
  <pre><code>package fileupload;
  import java.io.IOException;
  import java.io.PrintWriter;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import java.util.List;
  import java.util.Iterator;
  import java.io.File;
  import java.util.Random;
  import org.apache.commons.fileupload.servlet.ServletFileUpload;
  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  import org.apache.commons.fileupload.*;
  import java.util.regex.*;
  
  
  public class uploads extends HttpServlet {
  
      protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
          response.setContentType("text/html;charset=UTF-8");
          PrintWriter out = response.getWriter();
          try {
  boolean isMultipart = ServletFileUpload.isMultipartContent(request);
  if (!isMultipart) {
  out.println("File Not Uploaded");
  } else {
  FileItemFactory factory = new DiskFileItemFactory();
  ServletFileUpload upload = new ServletFileUpload(factory);
  List items = null;
  try {
  items = upload.parseRequest(request);
  //out.println("items: "+items);
  } catch (FileUploadException e) {
  e.printStackTrace();
  }
  FileItem file = (FileItem)items.get(0);
  //out.print(file);
  Iterator itr = items.iterator();
  int noFile=0;
  while (itr.hasNext()) {
  
  FileItem item = (FileItem) itr.next();
  if (item.isFormField()){
  
  String name = item.getFieldName();
  String value = item.getString();
  } else {
  try {
  
  String itemName = item.getName();
  Random generator = new Random();
  int r = Math.abs(generator.nextInt());
  String reg = "[.*]";
  String replacingtext = "";
  Pattern pattern = Pattern.compile(reg);
  Matcher matcher = pattern.matcher(itemName);
  StringBuffer buffer = new StringBuffer();
  while (matcher.find()) {
  matcher.appendReplacement(buffer, replacingtext);
  }
  int IndexOf = itemName.indexOf(".");
  String domainName = itemName.substring(IndexOf);
  String finalimage = buffer.toString()+"_"+r+domainName;
  File savedFile = new File("C:/tmp/"+"images\\"+finalimage);
  item.write(savedFile);
  } catch (Exception e) {
  e.printStackTrace();
  }
  }
  }
  out.print(noFile+" File(s) Uploaded !!");
  }
          } finally { 
              out.close();
          }
      } 
  
      // &lt;editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."&gt;
  
      @Override
      protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
          processRequest(request, response);
      } 
  
      @Override
      protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
          processRequest(request, response);
      }
  
      /** 
       * Returns a short description of the servlet.
       * @return a String containing servlet description
       */
      @Override
      public String getServletInfo() {
          return "Short description";
      }// &lt;/editor-fold&gt;
  
  }
  </code></pre>
  
  <p>same code is running perfectly without ajax.</p>