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,595 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now