9
  • 这个答案如何确定文件是否为 PDF 文件?建议下载另一个库,但我的要求是我只需要检查文件目录是否为 PDF 类型

  • 为这种用途使用完整的库看起来有点矫枉过正

  • 有什么方法可以知道 Java 文件是 PDF 类型的吗?
4

8 回答 8

19

好吧,根据维基百科PDF 文件以幻数开头:"%PDF" (hex 25 50 44 46)所以也许你应该检查文件中的 InputStream 并检查它。

于 2012-11-08T20:13:32.480 回答
5

SimpleMagic是一个用于解析内容类型的 Java 库:

<!-- pom.xml -->
    <dependency>
        <groupId>com.j256.simplemagic</groupId>
        <artifactId>simplemagic</artifactId>
        <version>1.8</version>
    </dependency>

import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;
import com.j256.simplemagic.ContentType;
// ...

public class SimpleMagicSmokeTest {

    private final static Logger log = LoggerFactory.getLogger(SimpleMagicSmokeTest.class);

    @Test
    public void smokeTestSimpleMagic() throws IOException {
        ContentInfoUtil util = new ContentInfoUtil();
        File possiblePdfFile = new File("/path/to/possiblePdfFile.pdf");
        ContentInfo info = util.findMatch(possiblePdfFile);

        log.info( info.toString() );
        assertEquals( ContentType.PDF, info.getContentType() );
    }
于 2016-09-28T16:55:44.510 回答
2

好吧,一种骇人听闻的解决方案是查看完整的文件名,看看它是否以“.pdf”结尾。以下内容应该有所帮助:

import javax.activation.*;  

public class ShowMimeType  
{  
    public static void main(String[] args) {  
        FileDataSource ds = new FileDataSource(args[0]);  
        String contentType = ds.getContentType();  
        System.out.println("The MIME type of the file " + args[0] + " is: " + contentType);  
    }  
}  
于 2012-11-08T20:11:00.187 回答
2

If checking the file extension is not satisfactory, you coudl try checking the files magic number by reading a few bytes of the file

PDF files start with "%PDF" (hex 25 50 44 46).
于 2012-11-08T20:14:03.310 回答
0

尝试了下面的代码,它工作。

public static boolean isSelectedFilePdf(Uri uri, ContentResolver contentResolver) {
if (uri != null) {
        if (uri.getScheme().equals("content")) {
            String type = contentResolver.getType(uri);
            return type != null && type.startsWith("application/pdf");
        } else {
            String fileName = uri.getLastPathSegment();
            String extension = fileName.substring(fileName.lastIndexOf("."));
            return extension != null && extension.equalsIgnoreCase(".pdf");
        }
    }
}
于 2019-06-04T10:26:17.113 回答
0

Check if a PDF-File is valid (Python)中提到了以下解决方案

在我的项目中,我需要检查某些上传文件的 mime 类型。我只是像这样使用文件命令:

from subprocess import Popen, PIPE
filetype = Popen("/usr/bin/file -b --mime -", shell=True, stdout=PIPE, stdin=PIPE).communicate(file.read(1024))[0].strip()

您当然可能希望将实际命令移动到某个配置文件中,因为命令行选项也因操作系统(例如 mac)而异。

如果您只需要知道它是否是 PDF 并且无论如何都不需要处理它,我认为 file 命令是比 lib 更快的解决方案。当然也可以手动进行,但如果您想检查不同的类型,文件命令可能会为您提供更大的灵活性。

于 2019-06-05T03:31:39.453 回答
0

将更轻的 URLCONnection.guessContentTypeFromStream() 与更重的 AutoDetectParser 相结合,该方法为某些 mimeTypes 返回 null。

if(currentImageType ==null){
                ByteArrayInputStream is = new ByteArrayInputStream(image);
                String mimeType = URLConnection.guessContentTypeFromStream(is);
                if(mimeType == null){
                    AutoDetectParser parser = new AutoDetectParser();
                    Detector detector = parser.getDetector();
                    Metadata md = new Metadata();
                    mimeType = detector.detect(is,md).toString();

                    if (mimeType.contains("pdf")){
                        mimeType ="pdf";
                    }
                    else if(mimeType.contains("tif")||mimeType.contains("tiff")){
                        mimeType = "tif";
                    }
                }
                if(mimeType.contains("png")){
                    mimeType ="png";
                }
                else if( mimeType.contains("jpg")||mimeType.contains("jpeg")){
                    mimeType = "jpg";
                }
                else if (mimeType.contains("pdf")){
                    mimeType ="pdf";
                }
                else if(mimeType.contains("tif")||mimeType.contains("tiff")){
                    mimeType = "tif";
                }

                currentImageType = ImageType.fromValue(mimeType);
            }
于 2016-03-21T20:28:37.097 回答
-1

这可能听起来有点太明显了,但请检查文件名上的扩展名。

如果它对 explorer 足够好,它应该对你足够好

于 2012-11-08T20:10:41.240 回答