本文介绍了解析文档时的Apache Tika和字符限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以请任何人帮我解决一下吗?

Could please anybody help me to sort it out?

可以这样做

   Tika tika = new Tika();
   tika.setMaxStringLength(10*1024*1024);

但是如果你不直接使用Tika,就像这样:

But if you don't use Tika directly, like this:

ContentHandler textHandler = new BodyContentHandler();
Metadata metadata = new Metadata();
Parser parser = new AutoDetectParser();

ParseContext ps = new ParseContext();
for (InputStream is : getInputStreams()) {
    parser.parse(is, textHandler, metadata, ps);
    is.close();
    System.out.println("Title: " + metadata.get("title"));
    System.out.println("Author: " + metadata.get("Author"));
}

没有办法设置它,因为你不与之交互 WriteOutContentHandler 。顺便说一下,它默认设置为 -1 ,这意味着没有限制。但是由此产生的限制是100000个字符。

There is no way to set it up, because you don't interact with the WriteOutContentHandler. Btw it is set to -1 by default which means no restrictions. But the resulting restriction is 100000 characters.

/**
 * The maximum number of characters to write to the character stream.
 * Set to -1 for no limit.
 */
private final int writeLimit;

/**
 * Number of characters written so far.
 */
private int writeCount = 0;

private WriteOutContentHandler(Writer writer, int writeLimit) {
    this.writer = writer;
    this.writeLimit = writeLimit;
}

/**
 * Creates a content handler that writes character events to
 * the given writer.
 *
 * @param writer writer
 */
public WriteOutContentHandler(Writer writer) {
    this(writer, -1);
}


推荐答案

你一定忽略了content handler有一个带有writelimit的构造函数。

You must have overlooked that the content handler has constructor with writelimit.

ContentHandler textHandler = new BodyContentHandler(int writeLimit);

这篇关于解析文档时的Apache Tika和字符限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 07:56
查看更多