0

Hi I am trying read an excel file in a servlet,I am trying to understand how its done, but I could only see resources that use the location of file hard coded everywhere like "C:\Newfolder\filename.xls", What I need is get the input stream from the reuest object and handle it to read the excel using Apache poi.so Far I did Upto this but its of no use.

my servlet:

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    // TODO Auto-generated method stub
    try{
    DiskFileUpload fu = new DiskFileUpload();
     fu.setSizeMax(1024 * 1024 * 2);
     fu.setSizeThreshold(1024);
     fu.setRepositoryPath(System.getProperty("java.io.tmpdir"));
     List fileList = fu.parseRequest(req);
     InputStream uploadedFileStream = null;
     String uploadedFileName = null; // name of file on user's computer

     for (Iterator i = fileList.iterator(); i.hasNext(); )
     {
       FileItem fi = (FileItem)i.next();
       if (fi.isFormField())
       {
         String key = fi.getFieldName();
         String val = fi.getString();
         System.out.println("Form parameter " + key + "=" + val);
       } else
       {
         if (fi.getSize() < 1)
         {
           throw new Exception("No file was uplaoded");
         }

         uploadedFileName = fi.getName();
         uploadedFileStream = fi.getInputStream();
       }
     }

     HSSFWorkbook hssF = new HSSFWorkbook(uploadedFileStream);
}catch(Exception e)
    {
    e.printStackTrace();
    }

}


}

my jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

   <body>
    <form name="frm" action="ReadExcel.do" method="post"
   enctype="multipart/form-data">
    <div id="header"></div>
    <input type="file" name="fl_upload">
   <input type="submit" value="Upload">
   <input type="reset" value="cancel">
   </form>
    </body>

0 Answers0