Your InputStream was neither an OLE2 stream, nor an OOXML stream

Deadly 提交于 2019-12-05 10:02:07

I don't know POI internal implementation, but my guess would be that they need a seekable stream. The streams returned by servlets (and networking in general) aren't seekable.

Try reading the whole contents and then wrapping it in ByteArrayInputStream:

byte[] bytes = getBytes(item.openStream());
InputStream stream = new ByteArrayInputStream(bytes);

public static byte[] getBytes(InputStream is) throws IOException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    int len;
    byte[] data = new byte[100000];
    while ((len = is.read(data, 0, data.length)) != -1) {
    buffer.write(data, 0, len);
    }

    buffer.flush();
    return buffer.toByteArray();
}
user1493834

The issue is solved ..

    while (iterator.hasNext()) {  //Apache commons file upload code
      FileItemStream item = iterator.next();
      InputStream stream = item.openStream();
      ByteArrayInputStream bs=new ByteArrayInputStream(IOUtils.toByteArray(stream));
      POITextExtractor extractor = ExtractorFactory.createExtractor(bs); 
      System.out.println(extractor.getText());
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!