简体   繁体   English

验证以检查上传的文件是 pdf

[英]Validate to check uploaded file is pdf

How to validate if the file uploaded is PDF only?如何验证上传的文件是否仅为 PDF? not only by extension(.pdf) but also with the content.If someone change the extension of any other file to pdf file then it should fail while uploading.不仅通过扩展名(.pdf)而且还有内容。如果有人将任何其他文件的扩展名更改为 pdf 文件,那么它在上传时应该会失败。

You can use Apache Tika for this, available here.为此,您可以使用 Apache Tika,可在此处获得。 http://tika.apache.org/ http://tika.apache.org/

You can also find a practical example here: https://dzone.com/articles/determining-file-types-java您还可以在此处找到一个实际示例: https : //dzone.com/articles/determining-file-types-java

There are many way to validate PDF file.有很多方法可以验证 PDF 文件。 I used itext for check pdf is corrupted or not.我使用 itext 来检查 pdf 是否已损坏。

try {
        PdfReader pdfReader = new PdfReader(file);

        PdfTextExtractor.getTextFromPage(pdfReader, 1);

        LOGGER.info("pdfFileValidator ==> Exit");
        return true;
    } catch (InvalidPdfException e) {
        e.printStackTrace();
        LOGGER.error("pdfFileValidator ==> Exit. Error ==> " + e.getMessage());
        return false;
    }

If file is not PDF or file is corrupted than it will throw InvalidPDFException .如果文件不是 PDF 或文件已损坏,则会抛出InvalidPDFException For above example you need itext library.对于上面的示例,您需要 itext 库。

There are many validation libraries that you can use in order to validate if a file is PDF compliant.您可以使用许多验证库来验证文件是否符合 PDF。 For instance, you can use - veradpf or pdfbox .例如,您可以使用-veradpfpdfbox Of course you can use any other library that would do the work for you.当然,您可以使用任何其他可以为您完成工作的库。 As it was already mentioned, tika is another library that can read file metadata and tell you what the file is.正如已经提到的, tika是另一个可以读取文件元数据并告诉您文件是什么的库。

As an example (a bare one), you can do something with pdfbox .作为一个例子(一个简单的例子),你可以用pdfbox做一些事情。 Also keep in mind that this will validate if the file is PDF/A compliant.另请记住,这验证文件是否符合 PDF/A。

boolean validateImpl(File file) {

    PreflightDocument document = new PreflightParser(file).getPreflightDocument();

    try {
        document.validate();
        ValidationResult validationResult = document.getResult();

        if (validationResult.isValid()) {
            return true;
        }

    } catch (Exception e) {
       // Error validating
    }
    return false;
}

or with Tika, you can do something like或者使用 Tika,你可以做类似的事情

public ContentType tikaDetect(File file) {

    Tika tika = new Tika();

    String detectedType = tika.detect(file);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM