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

ファイル拡張子のチェックが不十分な場合は、ファイルの数バイトを読み取ってファイルのマジックナンバーをチェックしてみてください。

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

一部のmimeTypeに対してnullを返す軽いURLCOnnection.guessContentTypeFromStream()と、重いAutoDetectParserを組み合わせます。

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 に答える
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

次の解決策は、PDFファイルが有効かどうかの確認(Python)で言及されています

プロジェクトで、私の場合、アップロードされたファイルのmimeタイプを確認する必要があります。私は単に次のようなfileコマンドを使用します:

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よりも高速なソリューションだと思います。もちろん手動で行うことも可能ですが、さまざまなタイプをチェックする場合は、fileコマンドを使用すると柔軟性が向上する可能性があります。

于 2019-06-05T03:31:39.453 に答える
-1

これは少し明白すぎるように聞こえるかもしれませんが、ファイル名の拡張子を確認してください。

探検家にとって十分であれば、あなたにとっても十分なはずです

于 2012-11-08T20:10:41.240 に答える