您的 InputStream 既不是 OLE2 流,也不是 OOXML 流 [英] Your InputStream was neither an OLE2 stream, nor an OOXML stream
问题描述
我正在使用 Apache Commons 在谷歌应用引擎中上传一个 .docx 文件,如本链接所述文件上传 servlet.上传时,我还想使用 Apache POI 库提取文本.
如果我将其传递给 POI API:
InputStream stream = item.openStream();
我收到以下异常:
java.lang.IllegalArgumentException:您的 InputStream 既不是 OLE2 流,也不是 OOXML 流公共静态字符串 docx2text(InputStream is) 抛出异常 {返回 ExtractorFactory.createExtractor(is).getText();}
我正在上传有效的 .docx 文档.如果我传递 FileInputStream 对象,POI API 工作正常.
FileInputStream fs=new FileInputStream(new File("C:\\docs\\mydoc.docx"));
我不知道 POI 的内部实现,但我的猜测是他们需要一个可查找的流.servlet(以及一般的网络)返回的流是不可查找的.p>
尝试读取整个内容,然后将其包装在 ByteArrayInputStream
中:
byte[] bytes = getBytes(item.openStream());InputStream stream = new ByteArrayInputStream(bytes);public static byte[] getBytes(InputStream is) 抛出 IOException {ByteArrayOutputStream 缓冲区 = new ByteArrayOutputStream();内里;字节[]数据=新字节[100000];while ((len = is.read(data, 0, data.length)) != -1) {buffer.write(data, 0, len);}缓冲区.flush();返回 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屋!