[英]PDFBOX 1.8.10. Error in generating PDDocument from load() method
我正在使用PDFBOX 1.8.10。
如果我将PDF文件加载到字节数组中,则可以正常工作-
File file = new File(args[0]);
FileInputStream fis = new FileInputStream(file); //Normal PDF File
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum); //no doubt here is 0
}
} catch (IOException ex) {
ex.printStackTrace();
}
byte[] bytes = bos.toByteArray();
CheckIsPDF(bytes);
pdf = PDDocument.load(new ByteArrayInputStream(bytes)); //**No exception here**
但是,如果同一文件存储在数据库中,并且如果我尝试通过上述代码读取它,则会得到以下异常- "java.io.IOException: Error: End-of-File, expected line"
。
这是从数据库读取并填充PDF-
List<byte[]> forms; //this gets populated from database. The data stored in DB is HEX.
for(byte[] file : forms){
try{
int var=file.length;
pdDocument = PDDocument.load(new ByteArrayInputStream(file)); //**Exception**
fieldLists = PDFFormUtils.printFields( pdDocument );
}
catch(Exception e){
e.printStackTrace();
}
}
如评论中所述,问题的原因在于Blob的内容不是PDF。 Blob内容为:
43 3a 5c 4d 42 43 50 4f 53 5c 52 65 6e 74 2e 70 64 66
pdf以“%PDF”开头,因此十六进制为
25 50 44 46
您提到的十六进制序列转换为
C:\MBCPOS\Rent.pdf
这意味着有人将文件名而不是文件内容保存到Blob中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.