Apache Tika blank text content when parsing doc, docx, pdf files

Viewed 214

here is the dependency of my pom.xml:

<dependency>
    <groupId>org.apache.tika</groupId>
    <artifactId>tika-core</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.tika</groupId>
    <artifactId>tika-parsers-standard-package</artifactId>
    <version>2.1.0</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>5.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.1.0</version>
</dependency>

My method to parse document:

public String parseToPlainText(InputStream inputStream) {
    BodyContentHandler handler = new BodyContentHandler(Integer.MAX_VALUE);
    AutoDetectParser parser = new AutoDetectParser();
    Metadata metadata = new Metadata();
    ParseContext parseContext = new ParseContext();
    parseContext.set(Parser.class, parser);

    String content = "";
    try {
        parser.parse(inputStream, handler, metadata, parseContext);
        content = handler.toString();
        System.out.println(parser.getDetector().detect(inputStream, metadata).toString());
    } catch (IOException | SAXException | TikaException e) {
        logger.error(e.getMessage(), e);
    } finally {
        try {
            inputStream.close();
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }

    return content;
}

The content is always blank. I tried to follow this guide: https://cwiki.apache.org/confluence/display/TIKA/Troubleshooting+Tika

Mimetypes seem to be supported so I don't understand what the problem could be.

Note: this line prints nothing if the InputStream is of type FileInputStream:

System.out.println(parser.getDetector().detect(inputStream, metadata).toString());

But I get the application/x-tika-ooxml mimetype if I pass a BufferedInputStream:

public static void main(String[] args) throws Exception {
    InputStream is = new FileInputStream("src/main/java/resources/file-sample_100kB.docx");
    System.out.println(parseToPlainText(new BufferedInputStream(is)));

    System.exit(0);
}
0 Answers
Related