Hello.
I'm trying to write code that reads an Excel spreadsheet and reads
a custom property called "Mime type". I'm using a jar file from
the February 7 nightly build.
I'm getting a Java ArrayIndexOutOfBounds exception from the POI
library, and I don't know why. This code sometimes works fine
but other times I get the exception. The code is straightforward,
taken from the examples given on Jakarta POI site.
Can anybody help?
Thanks.
Carmen
package apps.mimetypedetermination.excel;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Map;
import org.apache.poi.hpsf.*;
import org.apache.poi.poifs.eventfilesystem.*;
/**
* Determines the Mime type of an Excel spreadsheet
*/
public class ExcelMimeType
{
/**
* Determine Mime type of an Excel spreadsheet to XML using POI
* @param data the input data file
* @param options
*/
public static final String DEFAULT_EXCEL_MIME_TYPE =
"application/vnd.ms-excel";
private static String mime = "";
public InputStream GetMimeType(InputStream inputStream, String options) {
mime = "";
POIFSReader POIFr = new POIFSReader();
try {
POIFr.registerListener(new MyPOIFSReaderListener());
POIFr.read(inputStream);
} catch (IOException ex)
{
return null;
}
String mimetypeString;
if (mime == "")
mimetypeString = DEFAULT_EXCEL_MIME_TYPE;
else
mimetypeString = mime;
return new ByteArrayInputStream(mimetypeString.getBytes());
}
static class MyPOIFSReaderListener implements POIFSReaderListener
{
public void processPOIFSReaderEvent(POIFSReaderEvent event)
{
PropertySet ps = null;
try
{
ps = PropertySetFactory.create(event.getStream());
}
catch (NoPropertySetStreamException ex)
{
return;
}
catch (Exception ex)
{
throw new RuntimeException
("Property set stream \"" +
event.getPath() + event.getName() + "\": " + ex);
}
if (ps.isDocumentSummaryInformation())
{
try
{
DocumentSummaryInformation docsi = new
DocumentSummaryInformation(ps);
Map propmap = docsi.getCustomProperties();
if (propmap != null)
{
String mimeString = (String) propmap.get("Mime type");
if (mimeString != null) mime = mimeString;
}
}
catch (UnexpectedPropertySetTypeException ex)
{
throw new RuntimeException
("Unexpected property set type \"" +
event.getPath() + event.getName() + "\": " + ex);
}
}
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
Mailing List: http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project: http://jakarta.apache.org/poi/