您的InputStream既不是OLE2流,也不是OOXML流 [英] Your InputStream was neither an OLE2 stream, nor an OOXML stream

查看:2417
本文介绍了您的InputStream既不是OLE2流,也不是OOXML流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apache Commons在google应用引擎中上传.docx文件,如链接中所述
文件上传servlet 。在上传时,我也想通过使用Apache POI库来提取文本。



如果我将此传递给POI API:

  InputStream stream = item.openStream(); 

我得到以下异常:

 java.lang.IllegalArgumentException:您的InputStream既不是OLE2流也不是OOXML流
$ b public static String docx2text(InputStream is)throws Exception {
返回ExtractorFactory.createExtractor(is).getText();
}

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

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


解决方案

我不知道POI内部实现,但是我的猜测会是他们需要一个可寻求的流。通过servlet返回的流(和一般的网络连接)是不可搜索的。尝试读取整个内容,然后将其包装在 ByteArrayInputStream code>:

  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]; ((len = is.read(data,0,data.length))!= -1){
buffer.write(data,0,len);
while
}

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


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.

If I pass this to the 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.

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

解决方案

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

这篇关于您的InputStream既不是OLE2流,也不是OOXML流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆