Java 您的 InputStream 既不是 OLE2 流,也不是 OOXML 流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23246850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:30:03  来源:igfitidea点击:

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

javagoogle-app-engineapache-poi

提问by user1493834

I am using Apache Commons to upload a .docx file in google app engine as explained in this link File upload servlet. While uploading, I also want to extract text by using Apache POI libraries.

我正在使用 Apache Commons 在谷歌应用引擎中上传 .docx 文件,如此链接 文件上传 servlet 中所述。上传时,我还想使用 Apache POI 库提取文本。

If I pass this to the POI API:

如果我将其传递给 POI API:

 InputStream stream = item.openStream();

I get the below exception:

我得到以下异常:

java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream

public static String docx2text(InputStream is) throws Exception {
    return ExtractorFactory.createExtractor(is).getText();
}

I am uploading a valid .docx document. The POI API works fine if I pass a FileInputStream object.

我正在上传有效的 .docx 文档。如果我传递 FileInputStream 对象,POI API 工作正常。

FileInputStream fs=new FileInputStream(new File("C:\docs\mydoc.docx"));

采纳答案by Peter Knego

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.

我不知道 POI 的内部实现,但我的猜测是他们需要一个可查找的流。servlet(以及一般的网络)返回的流是不可查找的。

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

尝试阅读全部内容,然后将其包装在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();
}

回答by 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());
    }