title: Upload+ read an excel file in a jsp using POI
body: <p>I want to read an excel file in JSP,for this I first uploaded the file in a folder in the :D partition named <strong>uploads</strong> using a web application project,and I tried to <strong>read the excel uploaded file</strong> with an another java projet.<strong>The  both codes are working</strong>.Here it is the code of uploading in a specific folder via web application project(JSP and SERVLET):</p>
  
  <p><strong>Libraries</strong> </p>
  
  <ol>
  <li><strong>commons-fileupload-1.2.2.jar</strong>  </li>
  <li><strong>commons-io-2.1.jar</strong></li>
  </ol>
  
  <p><strong>index.jsp</strong> </p>
  
  <pre><code>&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Upload File&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form action="UploadFile" method="post" enctype="multipart/form-data"&gt;
  Select File : &lt;input type="file" name="filetoupload"&gt;
  &lt;br/&gt;
  &lt;input type="submit" value="Upload File"&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>UploadServlet.java(Servlet)</strong></p>
  
  <pre><code>import java.io.*;
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.util.Date;
  import java.util.Iterator;
  import java.util.List;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import org.apache.commons.fileupload.FileItem;
  import org.apache.commons.fileupload.FileItemFactory;
  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  import org.apache.commons.fileupload.servlet.ServletFileUpload;
  import org.apache.commons.io.FilenameUtils;
  
  public class UploadFile extends HttpServlet{
  
  String saveFile="D:/upload/"; 
  
  protected void processRequest(...)throws ServletException, IOException {
  response.setContentType("text/html;charset=UTF-8");
  PrintWriter out = response.getWriter();
  
  try {
  boolean ismultipart=ServletFileUpload.isMultipartContent(request);
  if(!ismultipart){
  
  }else{
  FileItemFactory factory = new DiskFileItemFactory();
  ServletFileUpload upload = new ServletFileUpload(factory);
  List items = null;
  
  try{
  
  items = upload.parseRequest(request);
  }catch(Exception e){
  }
  Iterator itr = items.iterator();
  while(itr.hasNext()){
  FileItem item = (FileItem)itr.next();
  if(item.isFormField()){
  
  }else{
  String itemname = item.getName();
  if((itemname==null || itemname.equals(""))){
  continue;
  }
  String filename = FilenameUtils.getName(itemname);
  File f = checkExist(filename);
  item.write(f);
  }
  }
  }
  
  }catch(Exception e){
  
  }
  finally {
  out.close();
  }
  }
  
  private File checkExist(String fileName) {
  File f = new File(saveFile+"/"+fileName);
  
  if(f.exists()){
  StringBuffer sb = new StringBuffer(fileName);
  sb.insert(sb.lastIndexOf("."),"-"+new Date().getTime());
  f = new File(saveFile+"/"+sb.toString());
  }
  return f;
  }
  
  @Override
  protected void doGet(...)throws ServletException, IOException {
  processRequest(request, response);
  
  }
  
  @Override
  protected void doPost(...)throws ServletException, IOException {
  processRequest(request, response);
  }
  
  @Override
  public String getServletInfo() {
  return "Short description";
  }
  
  }
  </code></pre>
  
  <p>Then I create a new <strong>JAVA PROJECT (SWING)</strong> and try the code of <strong>reading an EXCEL File via POI</strong>,It worked as well,here its is the code:</p>
  
  <p><strong>Libraries</strong>  </p>
  
  <ol>
  <li><strong>dom4j-1.6.1.jar</strong></li>
  <li><strong>poi-3.10-FINAL-20140208.jar</strong></li>
  <li><strong>poi-ooxml-3.9-20121203.jar</strong></li>
  <li><strong>poi-ooxml-schemas-3.9-20121203.jar</strong></li>
  <li><strong>xmlbeans-2.3.0.jar</strong></li>
  </ol>
  
  <p><strong>JavaApplication.java</strong></p>
  
  <pre><code>import java.io.*;
  import java.util.Iterator;
  import org.apache.poi.ss.usermodel.Cell;
  import org.apache.poi.ss.usermodel.Row;
  import org.apache.poi.xssf.usermodel.XSSFSheet;
  import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  
  public class JavaApplication{
  public static void main(String[] args){
  try{
  FileInputStream file;
  file = new FileInputStream(new File("D:\\upload\\total.xlsx"));
  
  //Create Workbook instance holding reference to .xlsx file
  XSSFWorkbook workbook = new XSSFWorkbook(file);
  
  //Get first/desired sheet from the workbook
  XSSFSheet sheet = workbook.getSheetAt(0);
  
  //Iterate through each rows one by one
  Iterator&lt;Row&gt; rowIterator = sheet.iterator();
  while (rowIterator.hasNext()){
  Row row = rowIterator.next();
  //For each row, iterate through all the columns
  Iterator&lt;Cell&gt; cellIterator = row.cellIterator();
  
  while (cellIterator.hasNext()){
  Cell cell = cellIterator.next();
  
  //Check the cell type and format accordingly
  switch (cell.getCellType()){
  case Cell.CELL_TYPE_NUMERIC:
  System.out.print(cell.getNumericCellValue() + "t");
  break;
  case Cell.CELL_TYPE_STRING:
  System.out.print(cell.getStringCellValue() + "t");
  break;
  }
  }
  System.out.println("");
  }
  file.close();
  } 
  catch (Exception e) 
  {
  e.printStackTrace();
  }
  }
  }
  </code></pre>
  
  <p>The question is <strong>how to mix these two codes</strong> to can UPLOAD the file then print the data from EXCEL to a table in JSP ???? help me i'm stuck for over a month on this procedure</p>