title: JSP AJAX file uploading
body: <p>I tried to upload a file and display content of the file back to the browser with ajax and jsp. However, it doesn't seem to work very well for me.</p>
  
  <p>Apparently, in JSP page Upload.jsp, when I try to getContentType() from request, <code>request.getcontentType() == null</code> .</p>
  
  <p>Does anyone have experience with this? Thank you much.</p>
  
  <p>Form</p>
  
  <pre><code>&lt;form id="uploadform" name="uploadform" enctype="multipart/form-data" action="Upload.jsp" method="post"&gt;
       &lt;input type="file" name="file" id="listfile" onChange="upload(this.value)"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>This is the Javascript function upload(ifile)</p>
  
  <pre><code>function upload(ifile){
                  if (window.XMLHttpRequest){
                      //IE7 + and other browsers
                      xmlhttp = new XMLHttpRequest();
                  }else{
                      //IE 6, 5
                      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                  }
  
                  if(xmlhttp == null){
                      alert("File Uploading is not available because your browser does not support AJAX");
                      return;
                  }
  
                  //Function to process response form upload.jsp
                  xmlhttp.onreadystatechange = function(){
                      if(xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200){
                          var response = xmlhttp.responseText;
  
                          alert(response);
                      }
                  }
  
                  xmlhttp.open("POST", "Upload.jsp?file="+ifile, true);
                  xmlhttp.send(null);
              }
  </code></pre>
  
  <p>And this is the JSP page Upload.jsp</p>
  
  <pre><code>&lt;%@page import="java.util.StringTokenizer"%&gt;
  &lt;%@page import="java.io.*" %&gt;
  
  &lt;%
  
          response.setContentType("text/html");
          response.setHeader("Cache-control", "no-cache");
  
      String contentType = request.getContentType();
  
      if ((contentType != null) &amp;&amp; (contentType.indexOf("multipart/form-data") &gt;= 0)) {
          DataInputStream in = new DataInputStream(request.getInputStream());
          //get length of Content type data
          int formDataLength = request.getContentLength();
          byte dataBytes[] = new byte[formDataLength];
          int byteRead = 0;
          int totalBytesRead = 0;
          //convert the uploaded file into byte code
          while (totalBytesRead &lt; formDataLength) {
              byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
              totalBytesRead += byteRead;
          }
  
                  //decode byte array using default charset
          String file = new String(dataBytes);
  
                  //Using StringTokenizer to extract genes list
                  StringTokenizer st = new StringTokenizer(file, " ");
  
                  int numtoken = st.countTokens();
  
                  for(int i = 0; i &lt; numtoken-1; i++){
                      st.nextToken();
                      }
  
                  String a = st.nextToken();
  
                  st = new StringTokenizer(a, " \n");
                  numtoken = st.countTokens();
  
  
                  String postlink = "";
  
                  st.nextToken();
                  st.nextToken();
  
                  for(int i = 1; i &lt; numtoken-3; i++){
                      String temp = st.nextToken();
                      char[] c = temp.toCharArray();
                      temp = new String(c, 0, c.length-1);
                      if(!" ".equalsIgnoreCase(temp)){
                          postlink = postlink + temp + ",";
                      }
                  }
  
                  String temp = st.nextToken();
                  postlink = postlink + temp;
  
                  out.println(postlink);
                  out.flush();
                  out.close();
  
           }else if (contentType == null){
              out.println("Not a valid file");
              out.flush();
              }
   %&gt;
  </code></pre>