How to know with java whether file is corrupted (readable) or not?

牧云@^-^@ 提交于 2021-01-20 16:59:04

问题


I have web application where person can upload any pdf via FTP. After pdf file get uploaded I perform certain operations over that pdf.

But the problem here is, while uploading the PDF via FTP sometimes connection breaks up in between and the pdf uploaded is not complete (act like corrupted one). When I try to open that document in arobat reader it gives message 'There was an error opening the document. The file is damaged and could not be repaired'.

Now before starting processing over PDF, I want to check whether pdf uploaded is readable means no corrupted.

Do java provide any API for that, or there is any method to check whether file is corrupted or not.


回答1:


We have iText API in Java to work on PDF files.

To check if a PDF file is valid to load and read, use com.itextpdf.text.pdf.PdfReader.
If the file is corrupted, an exception like com.itextpdf.text.exceptions.InvalidPdfException, is thrown.

Sample code snippet:

...  
import com.itextpdf.text.pdf.PdfReader;  
...  
try {  
    PdfReader pdfReader = new PdfReader( pathToUploadedPdfFile );  

    String textFromPdfFilePageOne = PdfTextExtractor.getTextFromPage( pdfReader, 1 ); 
    System.out.println( textFromPdfFilePageOne );
}  
catch ( Exception e ) {  
    // handle exception  
}  

In case of uploaded but corrupted files, you may face the following error:

com.itextpdf.text.exceptions.InvalidPdfException: Rebuild failed:   
  trailer not found.; Original message: PDF startxref not found.  

Note: To produce such an exception, try saving a pdf file from net, but abort it in the middle.
Use it to load through above code snippet and check if it is loaded safe.

You can find detailed examples on iText API at

Use Case Examples of iText PDF | iText.



来源:https://stackoverflow.com/questions/10477327/how-to-know-with-java-whether-file-is-corrupted-readable-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!