title: Uploading multiple files using jsp?
body: <p>How can I fix this code below to upload multiple files. It works good when I upload single file.</p>
  
  <p>Using libraries</p>
  
  <ol>
  <li>commons-fileupload-1.2.2 </li>
  <li>org.apache.commons.io.jar</li>
  </ol>
  
  
  
  <p>index.jsp</p>
  
  <pre><code>&lt;%@page import="java.io.*" %&gt;  
  
      &lt;%@page import="jxl.*"%&gt;
      &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
          pageEncoding="ISO-8859-1"%&gt;
      &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
      &lt;html&gt;
      &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
      &lt;title&gt;File upload&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;Uploading files&lt;/h1&gt;
          &lt;form name="uploadForm" action= "index.jsp" method="POST" enctype="multipart/form-data"&gt;
          &lt;input type="file" name="file" value="" multiple="multiple" width="100"/&gt;
          &lt;input type="submit" name="Submit" value="submit" multiple="multiple"/&gt; 
              &lt;%
  
                  String saveFile= new String();
                  //get content type
                  String contentType=request.getContentType();
                  // make sure first of all not null
                  if((contentType!=null)&amp;&amp;(contentType.indexOf("multipart/form-data")&gt;=0))
                  {
                      // begin to read a files
                      // creating DataInputStream
                      DataInputStream in = new DataInputStream(request.getInputStream());
                      //get content links of the request
                      int formDataLength= request.getContentLength();
                      // equal size
                      byte dataBytes[]= new byte[formDataLength];
                      int totalBytesRead=0;
                      int byteRead;
                      while(totalBytesRead&lt;formDataLength)
                      {
                          byteRead=in.read(dataBytes,totalBytesRead,formDataLength);
                          totalBytesRead+=byteRead;
  
                      }
                      //convert it to string  
                      String file= new  String(dataBytes);
                      saveFile= file.substring(file.indexOf("filename=\"")+10);
                      saveFile= saveFile.substring(0,saveFile.indexOf("\n"));
                      saveFile= saveFile.substring(saveFile.lastIndexOf("\\")+1, saveFile.indexOf("\""));
  
                      int lastIndex= contentType.lastIndexOf("=");
  
                      String boundary=contentType.substring(lastIndex+1, contentType.length());
  
                      int pos;
                      pos=file.indexOf("filename=\"");
                      pos=file.indexOf("\n",pos)+1;
                      pos=file.indexOf("\n",pos)+1;
                      pos=file.indexOf("\n",pos)+1;
  
                      int boundaryLocation=file.indexOf(boundary,pos)-4;
                      // get length of position
                      int startPos=((file.substring(0,pos)).getBytes()).length;
                      int endPos=((file.substring(0,boundaryLocation)).getBytes()).length;
  
                      saveFile="C:/upload/"+saveFile;
                      //creating a file object
                      File ff = new File(saveFile);
                      try{
                          //creating a FileOutputStream object
                          FileOutputStream fileOut= new FileOutputStream(ff);
                          fileOut.write(dataBytes, startPos, endPos-startPos);
                          fileOut.flush();
                          fileOut.close();
                      }
                      catch(Exception e){
                          out.print(e);
                      }       
  
                  }
  
  
              %&gt;
  
  
          &lt;/form&gt;
      &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>