use of org.apache.poi.poifs.filesystem.FileMagic in project hutool by looly.
the class ExcelFileUtil method getFileMagic.
/**
* {@link java.io.PushbackInputStream}
* PushbackInputStream的markSupported()为false,并不支持mark和reset
* 如果强转成PushbackInputStream在调用FileMagic.valueOf(inputStream)时会报错
* {@link FileMagic}
* 报错内容:getFileMagic() only operates on streams which support mark(int)
* 此处修改成 final InputStream in = FileMagic.prepareToCheckMagic(in)
*
* @param in {@link InputStream}
* @author kefan.qu
*/
private static FileMagic getFileMagic(InputStream in) {
FileMagic magic;
in = FileMagic.prepareToCheckMagic(in);
try {
magic = FileMagic.valueOf(in);
} catch (IOException e) {
throw new IORuntimeException(e);
}
return magic;
}
use of org.apache.poi.poifs.filesystem.FileMagic in project yyl_example by Relucent.
the class WordExample method toString.
public static String toString(byte[] content) throws IOException {
try (InputStream input = new ByteArrayInputStream(content)) {
FileMagic magic = ofMagic(content);
// DOC:OLE2
if (FileMagic.OLE2.equals(magic)) {
try (WordExtractor extractor = new WordExtractor(input)) {
return extractor.getText();
}
}
// DOCX:OOXML
OPCPackage opcPackage = OPCPackage.open(input);
try (POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage)) {
return extractor.getText();
}
} catch (XmlException | OpenXML4JException e) {
throw new IOException(e);
}
}
Aggregations