How do I know if a File type is PDF?











up vote
4
down vote

favorite
1













  • This answer How can I determine if a file is a PDF file? recommends to download another library, but my requirement is that I just need to check if a file is directory is of type PDF or not


  • Using complete library for this use looks like overkill


  • Are there any ways to know that a Java File is of type PDF?










share|improve this question




















  • 3




    Why don't you want to use a library? What is the use case of this? Looking at the extension is usually not a good idea, because anyone and any other program can change an extension. Without looking at the file it will be hard to determine if it really is a PDF or not. And for this I recommend you using a library.
    – peshkira
    Nov 8 '12 at 20:14










  • Related/duplicate: stackoverflow.com/questions/1915317/…
    – Tomasz Nurkiewicz
    Nov 8 '12 at 20:14










  • Try having a look at stackoverflow.com/questions/51438/…
    – MadProgrammer
    Nov 8 '12 at 20:18















up vote
4
down vote

favorite
1













  • This answer How can I determine if a file is a PDF file? recommends to download another library, but my requirement is that I just need to check if a file is directory is of type PDF or not


  • Using complete library for this use looks like overkill


  • Are there any ways to know that a Java File is of type PDF?










share|improve this question




















  • 3




    Why don't you want to use a library? What is the use case of this? Looking at the extension is usually not a good idea, because anyone and any other program can change an extension. Without looking at the file it will be hard to determine if it really is a PDF or not. And for this I recommend you using a library.
    – peshkira
    Nov 8 '12 at 20:14










  • Related/duplicate: stackoverflow.com/questions/1915317/…
    – Tomasz Nurkiewicz
    Nov 8 '12 at 20:14










  • Try having a look at stackoverflow.com/questions/51438/…
    – MadProgrammer
    Nov 8 '12 at 20:18













up vote
4
down vote

favorite
1









up vote
4
down vote

favorite
1






1






  • This answer How can I determine if a file is a PDF file? recommends to download another library, but my requirement is that I just need to check if a file is directory is of type PDF or not


  • Using complete library for this use looks like overkill


  • Are there any ways to know that a Java File is of type PDF?










share|improve this question
















  • This answer How can I determine if a file is a PDF file? recommends to download another library, but my requirement is that I just need to check if a file is directory is of type PDF or not


  • Using complete library for this use looks like overkill


  • Are there any ways to know that a Java File is of type PDF?







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 23 '17 at 10:34









Community

11




11










asked Nov 8 '12 at 20:09









daydreamer

30.2k124336562




30.2k124336562








  • 3




    Why don't you want to use a library? What is the use case of this? Looking at the extension is usually not a good idea, because anyone and any other program can change an extension. Without looking at the file it will be hard to determine if it really is a PDF or not. And for this I recommend you using a library.
    – peshkira
    Nov 8 '12 at 20:14










  • Related/duplicate: stackoverflow.com/questions/1915317/…
    – Tomasz Nurkiewicz
    Nov 8 '12 at 20:14










  • Try having a look at stackoverflow.com/questions/51438/…
    – MadProgrammer
    Nov 8 '12 at 20:18














  • 3




    Why don't you want to use a library? What is the use case of this? Looking at the extension is usually not a good idea, because anyone and any other program can change an extension. Without looking at the file it will be hard to determine if it really is a PDF or not. And for this I recommend you using a library.
    – peshkira
    Nov 8 '12 at 20:14










  • Related/duplicate: stackoverflow.com/questions/1915317/…
    – Tomasz Nurkiewicz
    Nov 8 '12 at 20:14










  • Try having a look at stackoverflow.com/questions/51438/…
    – MadProgrammer
    Nov 8 '12 at 20:18








3




3




Why don't you want to use a library? What is the use case of this? Looking at the extension is usually not a good idea, because anyone and any other program can change an extension. Without looking at the file it will be hard to determine if it really is a PDF or not. And for this I recommend you using a library.
– peshkira
Nov 8 '12 at 20:14




Why don't you want to use a library? What is the use case of this? Looking at the extension is usually not a good idea, because anyone and any other program can change an extension. Without looking at the file it will be hard to determine if it really is a PDF or not. And for this I recommend you using a library.
– peshkira
Nov 8 '12 at 20:14












Related/duplicate: stackoverflow.com/questions/1915317/…
– Tomasz Nurkiewicz
Nov 8 '12 at 20:14




Related/duplicate: stackoverflow.com/questions/1915317/…
– Tomasz Nurkiewicz
Nov 8 '12 at 20:14












Try having a look at stackoverflow.com/questions/51438/…
– MadProgrammer
Nov 8 '12 at 20:18




Try having a look at stackoverflow.com/questions/51438/…
– MadProgrammer
Nov 8 '12 at 20:18












6 Answers
6






active

oldest

votes

















up vote
11
down vote



accepted










Well, according to wikipedia PDF files start with magic numbers: "%PDF" (hex 25 50 44 46) so maybe you should check the InputStream from the file and check that.






share|improve this answer





















  • Just opened a PDF in notepad++, and it indeed does. +1
    – Sam I am
    Nov 8 '12 at 20:16










  • Yeah, I had a use case similar and Wikipedia was very helpful
    – ElderMael
    Nov 8 '12 at 20:17










  • but what if you make a text file, and just begin it with %PDF-1.4, just to screw with op
    – Sam I am
    Nov 8 '12 at 20:17






  • 2




    @SamIam - Sounds like another argument in favor of using a library.
    – jahroy
    Nov 8 '12 at 20:21






  • 2




    Exactly, because of this kind of things I will use a library, such as apache Tika, PRONOM DROID, JHove or any other identification tool, because they not only look at the signature but also at the whole format and the trailing bytes and give you specific info as mime, format and version.
    – peshkira
    Nov 8 '12 at 20:22


















up vote
2
down vote













Well, kind of a hackish solution would be to look at the full file name and see if it ends in ".pdf". The following should help:



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);
}
}





share|improve this answer




























    up vote
    2
    down vote













    SimpleMagic is a Java library for resolving content types:



    <!-- 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() );
    }





    share|improve this answer




























      up vote
      1
      down vote













      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).





      share|improve this answer




























        up vote
        0
        down vote













        This might sound a little bit too obvious, but check the extension on the filename.



        If it's good enough for explorer, it should be good enough for you






        share|improve this answer



















        • 2




          an extension does not say anything about a format.
          – peshkira
          Nov 8 '12 at 20:11










        • @peshkira well, it's supposed to. Only rarely you can't trust it.
          – John Dvorak
          Nov 8 '12 at 20:14






        • 1




          on what grounds do you base your comment. How can you say it is rarely? This depends on the use case. You say it is rarely, because you probably don't do it or don't encounter it, but this doesn't mean it does not happen in a real world scenario.
          – peshkira
          Nov 8 '12 at 20:17






        • 2




          I would say it is a bad idea to base design decisions on the way Microsoft Explorer does things.... I think most would agree that Windows is not perfect (and far from it).
          – jahroy
          Nov 8 '12 at 20:19




















        up vote
        0
        down vote













        Combines lighter URLCOnnection.guessContentTypeFromStream() which returns null for some mimeTypes, with heavier 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);
        }





        share|improve this answer





















          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13296939%2fhow-do-i-know-if-a-file-type-is-pdf%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          6 Answers
          6






          active

          oldest

          votes








          6 Answers
          6






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          11
          down vote



          accepted










          Well, according to wikipedia PDF files start with magic numbers: "%PDF" (hex 25 50 44 46) so maybe you should check the InputStream from the file and check that.






          share|improve this answer





















          • Just opened a PDF in notepad++, and it indeed does. +1
            – Sam I am
            Nov 8 '12 at 20:16










          • Yeah, I had a use case similar and Wikipedia was very helpful
            – ElderMael
            Nov 8 '12 at 20:17










          • but what if you make a text file, and just begin it with %PDF-1.4, just to screw with op
            – Sam I am
            Nov 8 '12 at 20:17






          • 2




            @SamIam - Sounds like another argument in favor of using a library.
            – jahroy
            Nov 8 '12 at 20:21






          • 2




            Exactly, because of this kind of things I will use a library, such as apache Tika, PRONOM DROID, JHove or any other identification tool, because they not only look at the signature but also at the whole format and the trailing bytes and give you specific info as mime, format and version.
            – peshkira
            Nov 8 '12 at 20:22















          up vote
          11
          down vote



          accepted










          Well, according to wikipedia PDF files start with magic numbers: "%PDF" (hex 25 50 44 46) so maybe you should check the InputStream from the file and check that.






          share|improve this answer





















          • Just opened a PDF in notepad++, and it indeed does. +1
            – Sam I am
            Nov 8 '12 at 20:16










          • Yeah, I had a use case similar and Wikipedia was very helpful
            – ElderMael
            Nov 8 '12 at 20:17










          • but what if you make a text file, and just begin it with %PDF-1.4, just to screw with op
            – Sam I am
            Nov 8 '12 at 20:17






          • 2




            @SamIam - Sounds like another argument in favor of using a library.
            – jahroy
            Nov 8 '12 at 20:21






          • 2




            Exactly, because of this kind of things I will use a library, such as apache Tika, PRONOM DROID, JHove or any other identification tool, because they not only look at the signature but also at the whole format and the trailing bytes and give you specific info as mime, format and version.
            – peshkira
            Nov 8 '12 at 20:22













          up vote
          11
          down vote



          accepted







          up vote
          11
          down vote



          accepted






          Well, according to wikipedia PDF files start with magic numbers: "%PDF" (hex 25 50 44 46) so maybe you should check the InputStream from the file and check that.






          share|improve this answer












          Well, according to wikipedia PDF files start with magic numbers: "%PDF" (hex 25 50 44 46) so maybe you should check the InputStream from the file and check that.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 '12 at 20:13









          ElderMael

          5,47942450




          5,47942450












          • Just opened a PDF in notepad++, and it indeed does. +1
            – Sam I am
            Nov 8 '12 at 20:16










          • Yeah, I had a use case similar and Wikipedia was very helpful
            – ElderMael
            Nov 8 '12 at 20:17










          • but what if you make a text file, and just begin it with %PDF-1.4, just to screw with op
            – Sam I am
            Nov 8 '12 at 20:17






          • 2




            @SamIam - Sounds like another argument in favor of using a library.
            – jahroy
            Nov 8 '12 at 20:21






          • 2




            Exactly, because of this kind of things I will use a library, such as apache Tika, PRONOM DROID, JHove or any other identification tool, because they not only look at the signature but also at the whole format and the trailing bytes and give you specific info as mime, format and version.
            – peshkira
            Nov 8 '12 at 20:22


















          • Just opened a PDF in notepad++, and it indeed does. +1
            – Sam I am
            Nov 8 '12 at 20:16










          • Yeah, I had a use case similar and Wikipedia was very helpful
            – ElderMael
            Nov 8 '12 at 20:17










          • but what if you make a text file, and just begin it with %PDF-1.4, just to screw with op
            – Sam I am
            Nov 8 '12 at 20:17






          • 2




            @SamIam - Sounds like another argument in favor of using a library.
            – jahroy
            Nov 8 '12 at 20:21






          • 2




            Exactly, because of this kind of things I will use a library, such as apache Tika, PRONOM DROID, JHove or any other identification tool, because they not only look at the signature but also at the whole format and the trailing bytes and give you specific info as mime, format and version.
            – peshkira
            Nov 8 '12 at 20:22
















          Just opened a PDF in notepad++, and it indeed does. +1
          – Sam I am
          Nov 8 '12 at 20:16




          Just opened a PDF in notepad++, and it indeed does. +1
          – Sam I am
          Nov 8 '12 at 20:16












          Yeah, I had a use case similar and Wikipedia was very helpful
          – ElderMael
          Nov 8 '12 at 20:17




          Yeah, I had a use case similar and Wikipedia was very helpful
          – ElderMael
          Nov 8 '12 at 20:17












          but what if you make a text file, and just begin it with %PDF-1.4, just to screw with op
          – Sam I am
          Nov 8 '12 at 20:17




          but what if you make a text file, and just begin it with %PDF-1.4, just to screw with op
          – Sam I am
          Nov 8 '12 at 20:17




          2




          2




          @SamIam - Sounds like another argument in favor of using a library.
          – jahroy
          Nov 8 '12 at 20:21




          @SamIam - Sounds like another argument in favor of using a library.
          – jahroy
          Nov 8 '12 at 20:21




          2




          2




          Exactly, because of this kind of things I will use a library, such as apache Tika, PRONOM DROID, JHove or any other identification tool, because they not only look at the signature but also at the whole format and the trailing bytes and give you specific info as mime, format and version.
          – peshkira
          Nov 8 '12 at 20:22




          Exactly, because of this kind of things I will use a library, such as apache Tika, PRONOM DROID, JHove or any other identification tool, because they not only look at the signature but also at the whole format and the trailing bytes and give you specific info as mime, format and version.
          – peshkira
          Nov 8 '12 at 20:22












          up vote
          2
          down vote













          Well, kind of a hackish solution would be to look at the full file name and see if it ends in ".pdf". The following should help:



          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);
          }
          }





          share|improve this answer

























            up vote
            2
            down vote













            Well, kind of a hackish solution would be to look at the full file name and see if it ends in ".pdf". The following should help:



            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);
            }
            }





            share|improve this answer























              up vote
              2
              down vote










              up vote
              2
              down vote









              Well, kind of a hackish solution would be to look at the full file name and see if it ends in ".pdf". The following should help:



              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);
              }
              }





              share|improve this answer












              Well, kind of a hackish solution would be to look at the full file name and see if it ends in ".pdf". The following should help:



              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);
              }
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 8 '12 at 20:11









              awolfe91

              1,5071710




              1,5071710






















                  up vote
                  2
                  down vote













                  SimpleMagic is a Java library for resolving content types:



                  <!-- 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() );
                  }





                  share|improve this answer

























                    up vote
                    2
                    down vote













                    SimpleMagic is a Java library for resolving content types:



                    <!-- 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() );
                    }





                    share|improve this answer























                      up vote
                      2
                      down vote










                      up vote
                      2
                      down vote









                      SimpleMagic is a Java library for resolving content types:



                      <!-- 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() );
                      }





                      share|improve this answer












                      SimpleMagic is a Java library for resolving content types:



                      <!-- 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() );
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Sep 28 '16 at 16:55









                      Abdull

                      14.3k1486135




                      14.3k1486135






















                          up vote
                          1
                          down vote













                          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).





                          share|improve this answer

























                            up vote
                            1
                            down vote













                            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).





                            share|improve this answer























                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              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).





                              share|improve this answer












                              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).






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 8 '12 at 20:14









                              case1352

                              1,10511221




                              1,10511221






















                                  up vote
                                  0
                                  down vote













                                  This might sound a little bit too obvious, but check the extension on the filename.



                                  If it's good enough for explorer, it should be good enough for you






                                  share|improve this answer



















                                  • 2




                                    an extension does not say anything about a format.
                                    – peshkira
                                    Nov 8 '12 at 20:11










                                  • @peshkira well, it's supposed to. Only rarely you can't trust it.
                                    – John Dvorak
                                    Nov 8 '12 at 20:14






                                  • 1




                                    on what grounds do you base your comment. How can you say it is rarely? This depends on the use case. You say it is rarely, because you probably don't do it or don't encounter it, but this doesn't mean it does not happen in a real world scenario.
                                    – peshkira
                                    Nov 8 '12 at 20:17






                                  • 2




                                    I would say it is a bad idea to base design decisions on the way Microsoft Explorer does things.... I think most would agree that Windows is not perfect (and far from it).
                                    – jahroy
                                    Nov 8 '12 at 20:19

















                                  up vote
                                  0
                                  down vote













                                  This might sound a little bit too obvious, but check the extension on the filename.



                                  If it's good enough for explorer, it should be good enough for you






                                  share|improve this answer



















                                  • 2




                                    an extension does not say anything about a format.
                                    – peshkira
                                    Nov 8 '12 at 20:11










                                  • @peshkira well, it's supposed to. Only rarely you can't trust it.
                                    – John Dvorak
                                    Nov 8 '12 at 20:14






                                  • 1




                                    on what grounds do you base your comment. How can you say it is rarely? This depends on the use case. You say it is rarely, because you probably don't do it or don't encounter it, but this doesn't mean it does not happen in a real world scenario.
                                    – peshkira
                                    Nov 8 '12 at 20:17






                                  • 2




                                    I would say it is a bad idea to base design decisions on the way Microsoft Explorer does things.... I think most would agree that Windows is not perfect (and far from it).
                                    – jahroy
                                    Nov 8 '12 at 20:19















                                  up vote
                                  0
                                  down vote










                                  up vote
                                  0
                                  down vote









                                  This might sound a little bit too obvious, but check the extension on the filename.



                                  If it's good enough for explorer, it should be good enough for you






                                  share|improve this answer














                                  This might sound a little bit too obvious, but check the extension on the filename.



                                  If it's good enough for explorer, it should be good enough for you







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Nov 8 '12 at 20:19









                                  Jacob Schoen

                                  9,833156695




                                  9,833156695










                                  answered Nov 8 '12 at 20:10









                                  Sam I am

                                  26.6k105791




                                  26.6k105791








                                  • 2




                                    an extension does not say anything about a format.
                                    – peshkira
                                    Nov 8 '12 at 20:11










                                  • @peshkira well, it's supposed to. Only rarely you can't trust it.
                                    – John Dvorak
                                    Nov 8 '12 at 20:14






                                  • 1




                                    on what grounds do you base your comment. How can you say it is rarely? This depends on the use case. You say it is rarely, because you probably don't do it or don't encounter it, but this doesn't mean it does not happen in a real world scenario.
                                    – peshkira
                                    Nov 8 '12 at 20:17






                                  • 2




                                    I would say it is a bad idea to base design decisions on the way Microsoft Explorer does things.... I think most would agree that Windows is not perfect (and far from it).
                                    – jahroy
                                    Nov 8 '12 at 20:19
















                                  • 2




                                    an extension does not say anything about a format.
                                    – peshkira
                                    Nov 8 '12 at 20:11










                                  • @peshkira well, it's supposed to. Only rarely you can't trust it.
                                    – John Dvorak
                                    Nov 8 '12 at 20:14






                                  • 1




                                    on what grounds do you base your comment. How can you say it is rarely? This depends on the use case. You say it is rarely, because you probably don't do it or don't encounter it, but this doesn't mean it does not happen in a real world scenario.
                                    – peshkira
                                    Nov 8 '12 at 20:17






                                  • 2




                                    I would say it is a bad idea to base design decisions on the way Microsoft Explorer does things.... I think most would agree that Windows is not perfect (and far from it).
                                    – jahroy
                                    Nov 8 '12 at 20:19










                                  2




                                  2




                                  an extension does not say anything about a format.
                                  – peshkira
                                  Nov 8 '12 at 20:11




                                  an extension does not say anything about a format.
                                  – peshkira
                                  Nov 8 '12 at 20:11












                                  @peshkira well, it's supposed to. Only rarely you can't trust it.
                                  – John Dvorak
                                  Nov 8 '12 at 20:14




                                  @peshkira well, it's supposed to. Only rarely you can't trust it.
                                  – John Dvorak
                                  Nov 8 '12 at 20:14




                                  1




                                  1




                                  on what grounds do you base your comment. How can you say it is rarely? This depends on the use case. You say it is rarely, because you probably don't do it or don't encounter it, but this doesn't mean it does not happen in a real world scenario.
                                  – peshkira
                                  Nov 8 '12 at 20:17




                                  on what grounds do you base your comment. How can you say it is rarely? This depends on the use case. You say it is rarely, because you probably don't do it or don't encounter it, but this doesn't mean it does not happen in a real world scenario.
                                  – peshkira
                                  Nov 8 '12 at 20:17




                                  2




                                  2




                                  I would say it is a bad idea to base design decisions on the way Microsoft Explorer does things.... I think most would agree that Windows is not perfect (and far from it).
                                  – jahroy
                                  Nov 8 '12 at 20:19






                                  I would say it is a bad idea to base design decisions on the way Microsoft Explorer does things.... I think most would agree that Windows is not perfect (and far from it).
                                  – jahroy
                                  Nov 8 '12 at 20:19












                                  up vote
                                  0
                                  down vote













                                  Combines lighter URLCOnnection.guessContentTypeFromStream() which returns null for some mimeTypes, with heavier 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);
                                  }





                                  share|improve this answer

























                                    up vote
                                    0
                                    down vote













                                    Combines lighter URLCOnnection.guessContentTypeFromStream() which returns null for some mimeTypes, with heavier 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);
                                    }





                                    share|improve this answer























                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      Combines lighter URLCOnnection.guessContentTypeFromStream() which returns null for some mimeTypes, with heavier 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);
                                      }





                                      share|improve this answer












                                      Combines lighter URLCOnnection.guessContentTypeFromStream() which returns null for some mimeTypes, with heavier 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);
                                      }






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Mar 21 '16 at 20:28









                                      Akin Okegbile

                                      457720




                                      457720






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13296939%2fhow-do-i-know-if-a-file-type-is-pdf%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Catalogne

                                          Violoncelliste

                                          Héron pourpré