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

前端 未结 1 865
醉话见心
醉话见心 2021-02-04 21:22

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 up

相关标签:
1条回答
  • 2021-02-04 21:58

    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.

    0 讨论(0)
提交回复
热议问题