Solved

I am trying to upload excel file in jsp. When I try to create the POIFSFileSystem using following call POIFSFileSystem fs = new POIFSFileSystem(request.getInputStream())

Posted on 2008-11-01
2
2,614 Views
Last Modified: 2013-11-24
I am trying to upload excel file in jsp. When I try to create the POIFSFileSystem using following call POIFSFileSystem fs = new POIFSFileSystem(request.getInputStream())

request type is multipart
I am trying to upload excel file in jsp. When I try to create the POIFSFileSystem using following call
 
POIFSFileSystem fs = new POIFSFileSystem(uploadedFileStream);
 
I get following error message
 
 
 
Hi,
 
I trying to upload an excel sheet and read it using POI. 
My html code is:
<form name="frmNTest" enctype="MULTIPART/FORM-DATA" method="post" action="test.jsp">
<input type=hidden name=hfrmID value="frmTest">
<input type=hidden name=hfrmActionID value="uploadFiletoServer">
<table width="98%" border=0>
<tr>
<td>Select File</td>
<td><input type="file" name="updfilename">
</td>
</tr>
 
<tr>
<td colspan="2"><div align="center">
<input type="submit" name=Submit value="Upload">
</div></td>
</tr>
</table>
**************************
My server side code is: (test.jsp)
<%
UploadFile up = new UploadFile();
up.doUpload(request,response);
%>
********************************
import java.io.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletInputStream;
import org.apache.poi.hssf.util.*;
import org.apache.poi.hssf.eventmodel.*;
import org.apache.poi.hssf.eventusermodel.*;
import org.apache.poi.hssf.dev.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.*;
import java.io.FileInputStream;
import java.io.InputStream;
public class UploadFile {
 
public void doUpload(HttpServletRequest request, HttpServletResponse response) throws IOException
{
try
{ 
ServletInputStream is = request.getInputStream();
InputStream ins = (InputStream)is;
POIFSFileSystem fs = new POIFSFileSystem(ins);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(1);
HSSFCell cell = row.getCell((short)0);
String c1 = cell.getStringCellValue();
}
catch(IOException io)
{
LCMLogger.log("io error = " + io.toString());
}
}
}
*******************
I am getting the following exception:
java.io.IOException: Invalid header signature; read 3255307777713450285, expected -2226271756974174256

Open in new window

0
Comment
Question by:sridhar_karu
2 Comments
 
LVL 27

Accepted Solution

by:
rrz earned 50 total points
ID: 22859383
I don't use POI. But I think I can help some. I looked at the API at
http://poi.apache.org/apidocs/overview-summary.html   
I didn't see anything in there that you could use to upload a file.  I think you need to use  
http://commons.apache.org/fileupload/   
Here is some sample code
http://www.experts-exchange.com/Programming/Languages/Java/J2EE/JSP/Q_23504709.html 
For your question here, you could do a test with test.jsp having code like the following.

<%@ page import="java.util.*,
                 java.io.*,
                 org.apache.commons.fileupload.*,
                 org.apache.commons.fileupload.servlet.*,
                 org.apache.commons.fileupload.disk.*,
                 org.apache.commons.fileupload.util.*,
                 org.apache.poi.hssf.util.*,
                 org.apache.poi.hssf.eventmodel.*,
                 org.apache.poi.hssf.eventusermodel.*,
                 org.apache.poi.hssf.dev.*,
                 org.apache.poi.hssf.usermodel.*,
                 org.apache.poi.poifs.filesystem.*"%>
<%
  boolean isMultipart = ServletFileUpload.isMultipartContent(request);
  if(isMultipart){
                  ServletFileUpload upload = new ServletFileUpload();
                  FileItemIterator iter = upload.getItemIterator(request);
                  FileItemStream item = null;
                  String name = "";
                  InputStream stream = null;
                  while (iter.hasNext()){
                                         item = iter.next();
                                         name = item.getFieldName();
                                         stream = item.openStream();
                    if(item.isFormField()){out.println("Form field " + name + " with value " 
                                               + Streams.asString(stream) + "<br/>");}
                    else {
                          name = item.getName();
                          if(name != null && !"".equals(name)){
                             String fileName = new File(item.getName()).getName();
                             out.println("Client file " + item.getName() + " <br/>with file name "
                                                        + fileName + " uploaded.<br/>");
                             
                             POIFSFileSystem fs = new POIFSFileSystem(stream);
                             HSSFWorkbook wb = new HSSFWorkbook(fs);
                             HSSFSheet sheet = wb.getSheetAt(0);
                             HSSFRow row = sheet.getRow(1);
                             HSSFCell cell = row.getCell((short)0);
                             String c1 = cell.getStringCellValue();
                             out.println(c1);
                          }
                       }
                  }
  }
%>

Open in new window

0
 

Author Comment

by:sridhar_karu
ID: 23024547
Hi ,

Thanks.

I have not  properly formatted  the question.Any way I found the solution.Finally I conclusion  that whenever we send the multipart stream ,we need to  parse that  stream and reconstruct into binary stream and send that  stream to POI.

Thanks,
sridhar.

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Modeling a class in java 5 53
HashMap Vs TreeMap 12 60
numbers ascending pyramid 101 191
How  Can  I  resolve   this  Error "java.sql.SQLException: Invalid column index" 4 37
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question