• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2662
  • Last Modified:

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())

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
sridhar_karu
Asked:
sridhar_karu
1 Solution
 
rrzCommented:
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
 
sridhar_karuAuthor Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now