我正在使用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"
。
这是从DB读取并填充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();
}
}
答案 0 :(得分:1)
正如评论中所讨论的,问题的原因是blob的内容不是PDF。 blob内容是:
43 3a 5c 4d 42 43 50 4f 53 5c 52 65 6e 74 2e 70 64 66
pdf以&#34;%PDF&#34;开头,因此在十六进制中,这将是
25 50 44 46
您提到的十六进制序列转换为
C:\MBCPOS\Rent.pdf
表示有人将文件名而不是文件内容保存到blob中。