gpt4 book ai didi

java - 解析二进制文件时出错... (moSTLy PDF)

转载 作者:太空宇宙 更新时间:2023-11-04 08:31:04 25 4
gpt4 key购买 nike

我正在尝试使用 Apache Tika 通过使用 ByteArrayInputStream 处理二进制文件来解析 pdf 文件...并且开始出现某些 pdf 文件的错误,并且对于某些 pdf 文件来说解析得非常好.. 之前我能够使用 Tika 解析相同的 pdf 文件,但现在当我尝试使用 ByteArrayInputStream 时,我开始收到错误..我认为 ByteArray 存在一些问题这是我收到的错误..

org.apache.tika.exception.TikaException: Unexpected RuntimeException from org.apache.tika.parser.pdf.PDFParser@652489c0

这是我的代码...

if (page.isBinary()) {
handleBinary(page, curURL);
}
<小时/>
public int handleBinary(Page page, WebURL curURL) {
try {
binaryParser.parse(page.getBinaryData());
page.setText(binaryParser.getText());
handleMetaData(page, binaryParser.getMetaData());


//System.out.println(" pdf url " +page.getWebURL().getURL());
//System.out.println("Text" +page.getText());
} catch (Exception e) {
// TODO: handle exception
}
return PROCESS_OK;
}
<小时/>
        public class BinaryParser {

private String text;
private Map<String, String> metaData;

private Tika tika;

public BinaryParser() {
tika = new Tika();
}

public void parse(byte[] data) {
InputStream is = null;
try {
is = new ByteArrayInputStream(data);
text = null;
Metadata md = new Metadata();
metaData = new HashMap<String, String>();
text = tika.parseToString(is, md).trim();
processMetaData(md);
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(is);
}
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}


private void processMetaData(Metadata md){
if ((getMetaData() == null) || (!getMetaData().isEmpty())) {
setMetaData(new HashMap<String, String>());
}
for (String name : md.names()){
getMetaData().put(name.toLowerCase(), md.get(name));
}
}

public Map<String, String> getMetaData() {
return metaData;
}

public void setMetaData(Map<String, String> metaData) {
this.metaData = metaData;
}

}
<小时/>
    public class Page {

private WebURL url;

private String html;

// Data for textual content
private String text;

private String title;

private String keywords;
private String authors;
private String description;
private String contentType;
private String contentEncoding;

private byte[] binaryData;

private List<WebURL> urls;

private ByteBuffer bBuf;

private final static String defaultEncoding = Configurations
.getStringProperty("crawler.default_encoding", "UTF-8");

public boolean load(final InputStream in, final int totalsize,
final boolean isBinary) {
if (totalsize > 0) {
this.bBuf = ByteBuffer.allocate(totalsize + 1024);
} else {
this.bBuf = ByteBuffer.allocate(PageFetcher.MAX_DOWNLOAD_SIZE);
}
final byte[] b = new byte[1024];
int len;
double finished = 0;
try {
while ((len = in.read(b)) != -1) {
if (finished + b.length > this.bBuf.capacity()) {
break;
}
this.bBuf.put(b, 0, len);
finished += len;
}
} catch (final BufferOverflowException boe) {
System.out.println("Page size exceeds maximum allowed.");
return false;
} catch (final Exception e) {
System.err.println(e.getMessage());
return false;
}

this.bBuf.flip();
if (isBinary) {
binaryData = new byte[bBuf.limit()];
bBuf.get(binaryData);
} else {
this.html = "";
this.html += Charset.forName(defaultEncoding).decode(this.bBuf);
this.bBuf.clear();
if (this.html.length() == 0) {
return false;
}
}
return true;
}
public boolean isBinary() {
return binaryData != null;
}

public byte[] getBinaryData() {
return binaryData;
}

任何建议我做错了什么......!!

更新:-升级到 pdfbox 1.6.0 版本后,我开始在某些 pdf 中收到此错误...

Parsing Error, Skipping Object
java.io.IOException: expected='endstream' actual='' org.apache.pdfbox.io.PushBackInputStream@70dbdc4b
at org.apache.pdfbox.pdfparser.BaseParser.parseCOSStream(BaseParser.java:439)
at org.apache.pdfbox.pdfparser.PDFParser.parseObject(PDFParser.java:552)
at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:184)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1088)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1053)

对于某些 pdf,此错误...

 Did not found XRef object at specified startxref position 0
Invalid dictionary, found: '' but expected: '/'
WARN [Crawler 2] Did not found XRef object at specified startxref position 0

最佳答案

这是 PDFBox 版本 1.4.0 的一个已知错误。只需更新到 PDFBox 1.5.0+

检查这个release notes :

[PDFBOX-578] PDPageNode.getCount 中的 NPE NullPointerException

还有这个JIRA ticket .

关于java - 解析二进制文件时出错... (moSTLy PDF),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7489206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com