首页 > 代码库 > tika提取pdf信息异常

tika提取pdf信息异常

org.apache.tika.sax.WriteOutContentHandler$WriteLimitReachedException: Your document contained more than 100000 characters, and so your requested limit has been reached. To receive the full text of the document, increase your limit. (Text up to the limit is however available).at org.apache.tika.sax.WriteOutContentHandler.characters(WriteOutContentHandler.java:141)at org.apache.tika.sax.ContentHandlerDecorator.characters(ContentHandlerDecorator.java:146)at org.apache.tika.sax.xpath.MatchingContentHandler.characters(MatchingContentHandler.java:85)at org.apache.tika.sax.ContentHandlerDecorator.characters(ContentHandlerDecorator.java:146)at org.apache.tika.sax.ContentHandlerDecorator.characters(ContentHandlerDecorator.java:146)at org.apache.tika.sax.SafeContentHandler.access$001(SafeContentHandler.java:46)at org.apache.tika.sax.SafeContentHandler$1.write(SafeContentHandler.java:82)at org.apache.tika.sax.SafeContentHandler.filter(SafeContentHandler.java:140)at org.apache.tika.sax.SafeContentHandler.characters(SafeContentHandler.java:287)at org.apache.tika.sax.XHTMLContentHandler.characters(XHTMLContentHandler.java:278)at org.apache.tika.sax.XHTMLContentHandler.characters(XHTMLContentHandler.java:305)at org.apache.tika.parser.pdf.PDF2XHTML.writeString(PDF2XHTML.java:398)at org.apache.pdfbox.util.PDFTextStripper.writeString(PDFTextStripper.java:866)at org.apache.pdfbox.util.PDFTextStripper.writeLine(PDFTextStripper.java:1896)at org.apache.pdfbox.util.PDFTextStripper.writePage(PDFTextStripper.java:744)at org.apache.pdfbox.util.PDFTextStripper.processPage(PDFTextStripper.java:461)at org.apache.pdfbox.util.PDFTextStripper.processPages(PDFTextStripper.java:385)at org.apache.pdfbox.util.PDFTextStripper.writeText(PDFTextStripper.java:344)at org.apache.tika.parser.pdf.PDF2XHTML.process(PDF2XHTML.java:130)at org.apache.tika.parser.pdf.PDFParser.parse(PDFParser.java:159)

在使用apache tika提取pdf信息时,报以上错误。根据错误信息提示,可能读取超过请求限制(10万字)。

我的代码如下:

		Parser parser = new PDFParser();		//parser.		BodyContentHandler handler = new BodyContentHandler();		Metadata metadata = http://www.mamicode.com/new Metadata();"1.pdf"));			parser.parse(stream, handler, metadata, new ParseContext());						 for (String name : metadata.names()) {                 System.out.println(name + ":\t" + metadata.get(name));             }		} catch (IOException e) {			// TODO Auto-generated catch block			e.printStackTrace();		} catch (SAXException e) {			// TODO Auto-generated catch block			e.printStackTrace();		} catch (TikaException e) {			// TODO Auto-generated catch block			e.printStackTrace();		} finally {			try {				stream.close();			} catch (IOException e) {				// TODO Auto-generated catch block				e.printStackTrace();			}		}

  对读取字数限制,可能在某个构造函数里我没有传入最大限制,而使用了默认的十万字。检查一下上面的代码,我注意到了

BodyContentHandler的构造函数:
org.apache.tika.sax.BodyContentHandler.BodyContentHandler(int writeLimit)

  看样子有关系。修改一下构造函数的数字为:10*1024*1024(这个数字有pdf文档大小决定)。

重新调试程序,即可获得pdf的元数据信息如下:

  

dc:subject:	meta:save-date:	2014-07-22T21:02:38Zsubject:	PostgreSQL 9.3 DocumentationAuthor:	The PostgreSQL Global Development Groupdcterms:created:	2014-07-22T20:55:33Zdate:	2014-07-22T21:02:38Zcreator:	The PostgreSQL Global Development GroupCreation-Date:	2014-07-22T20:55:33Ztitle:	PostgreSQL 9.3 Documentationtrapped:	Falsemeta:author:	The PostgreSQL Global Development Groupcreated:	Wed Jul 23 04:55:33 CST 2014meta:keyword:	cp:subject:	PostgreSQL 9.3 Documentationdc:format:	application/pdf; version=1.4PTEX.Fullbanner:	This is pdfTeX, Version 3.1415926-2.4-1.40.13 (TeX Live 2012/Debian) kpathsea version 6.1.0xmp:CreatorTool:	LaTeX with hyperref packageKeywords:	dc:title:	PostgreSQL 9.3 DocumentationLast-Save-Date:	2014-07-22T21:02:38Zmeta:creation-date:	2014-07-22T20:55:33Zdcterms:modified:	2014-07-22T21:02:38Zdc:creator:	The PostgreSQL Global Development Grouppdf:PDFVersion:	1.4Last-Modified:	2014-07-22T21:02:38Zmodified:	2014-07-22T21:02:38ZxmpTPg:NPages:	2861pdf:encrypted:	falseproducer:	pdfTeX-1.40.13; modified using iText® 5.1.3 ©2000-2011 1T3XT BVBAContent-Type:	application/pdf

  

tika提取pdf信息异常