11 Replies Latest reply: Nov 23, 2007 3:20 AM by 800282 RSS

    Retrieve File Type / Mime Type

    807603
      Hi all,

      I hava some files that are stores on a database as binaries files but without files extensions. And i don't know why but the original file name (with extension) is not stored.
      I try to retrieve the extension of file using MIME but without success for the moment. Could you tell me if there any tools in JAVA of course that i can use to retrieve it ?

      Thanks in advance for your help and answer.

      Regards,

      Denez
        • 1. Re: Retrieve File Type / Mime Type
          807603
          There are one or two utilities arround for guessing file type, basically they just have a library of common file types with heuristic test pattern to decide what a file is likely to be. A lot of binary files, for example, have a "magic number" in the first four bytes allocated by whoever dreamed up the format. I'd search outside java if I were you, for unix or Windows utilities.
          • 2. Re: Retrieve File Type / Mime Type
            rahul_akkina
            What happens when you try with ??
            URLConnection.guessContentTypeFromStream(InputStream is)
            or make use of activation/mime-util libraries in guessing the mimetype...checkout the below link.

            http://www.rgagnon.com/javadetails/java-0487.html

            REGARDS,
            RaHuL
            • 3. Re: Retrieve File Type / Mime Type
              800282
              denez wrote:
              Hi all,

              I hava some files that are stores on a database as binaries files but without files extensions. And i don't know why but the original file name (with extension) is not stored.
              I try to retrieve the extension of file using MIME but without success for the moment. Could you tell me if there any tools in JAVA of course that i can use to retrieve it ?
              Try DROID:
              http://droid.sourceforge.net/wiki/index.php/Introduction
              • 4. Re: Retrieve File Type / Mime Type
                807603
                First, thanks for your reply.

                I try the two method as you mentionned.
                Here is my code :

                try {
                File aFile = new File("File") ;
                InputStream in = new FileInputStream(aFile) ;
                System.out.println("1. " + URLConnection.guessContentTypeFromStream(in)) ;
                System.out.println("Mime Type of " + aFile.getName() + " is " + new MimetypesFileTypeMap()).getContentType(aFile));
                } catch (Exception e) {
                e.printStackTrace() ;
                }

                output is :
                1. null
                Mime Type of File is application/octet-stream

                And exactly the same result if i put again the extension '.zip'
                Any other ideas ?

                Thanks in advance,
                Regards,

                Denez
                • 5. Re: Retrieve File Type / Mime Type
                  800282
                  denez wrote:
                  ...
                  output is :
                  1. null
                  Mime Type of File is application/octet-stream

                  And exactly the same result if i put again the extension '.zip'
                  Are they valid formats? Just adding a ZIP extension after a file that is just an arbitrary number of bytes won't make it a valid ZIP.
                  But I think guessContentTypeFromStream(...) just doesn't do a thorough job as DROID does.
                  \\
                  \\
                  Any other ideas ?
                  Yes: DROID.
                  • 6. Re: Retrieve File Type / Mime Type
                    807603
                    Sorry probably not so clear regarding the .ZIP extension.

                    I made a File.ZIp containing 3 diffrents files at the beginning.
                    I just remove the .ZIP extension to test my program.
                    if i rename it to .ZIP and try to open it with Winrar or Winzip there is no problem at all.

                    Regarding DROID, why not, but is it possible to use it in my program with an import ? there is no information regarding this in the documentation.
                    Have you already try it ?

                    Thanks in advance.
                    Regards,

                    Denez
                    • 7. Re: Retrieve File Type / Mime Type
                      800282
                      denez wrote:
                      ...
                      Regarding DROID, why not, but is it possible to use it in my program with an import ? there is no information regarding this in the documentation.
                      I find it hard to believe that you did some thorough research. I only posted the link to DROID a short while back, yet you claim that there is no information about what you are looking for.
                      Ah well, if you can't find it, it's not there I guess.
                      \\
                      \\
                      Have you already try it ?
                      ...
                      Yes, I tried it and found that the documentation is sufficient to get started.
                      • 8. Re: Retrieve File Type / Mime Type
                        807603
                        Sorry but i didn't find in the documentation any sample regarding how to use it if you import the droid.jar in the classpath.
                        It's only metionned how to use it with the jar executable and in command line.

                        Or probably i have not the correct documentation.
                        Regards,

                        Denez
                        • 9. Re: Retrieve File Type / Mime Type
                          JoachimSauer
                          denez wrote:
                          Or probably i have not the correct documentation.
                          Regards,
                          Did you click on the link that says JavaDoc?
                          • 10. Re: Retrieve File Type / Mime Type
                            807603
                            of course, but where is a entry point ?
                            it's only list the contains of the javadoc.
                            But not give some sample.

                            Regards,
                            Denez
                            • 11. Re: Retrieve File Type / Mime Type
                              807603
                              I would like to apologies.
                              After a long time of research in the javadoc I find how DROID works!
                              No it's ok i have adapted a part of code to obtain information that i want.
                              For your information it works with excel, word, zip for example.
                              I try with a text/plain file but it doesn"t work at all.

                              here is my code :

                              try {
                              AnalysisController analysisControl = new AnalysisController();
                              analysisControl.readConfiguration("DROID_config.xml") ;
                              analysisControl.readSigFile() ;
                              IdentificationFile identificationFile = new IdentificationFile("C:\\File");
                              ByteReader byteReader = null;
                              byteReader = newByteReader(identificationFile);

                              analysisControl.getSigFile().runFileIdentification(byteReader) ;

                              // display list of hits
                              for (int ih = 0; ih < byteReader.getNumHits(); ih++) {
                              String specificityDisplay = byteReader.getHit(ih).isSpecific()?
                              "specific" : "generic";
                              System.out.println(" " + byteReader.getHit(ih).getHitTypeVerbose() + " "
                              + specificityDisplay + " hit for " +
                              byteReader.getHit(ih).getFileFormat().getName() + " [PUID: " +          
                                          byteReader.getHit(ih).getFileFormat().getPUID() + "]" + " [MIME: "
                                          + byteReader.getHit(ih).getFileFormat().getMimeType() + "]" );
                              if (byteReader.getHit(ih).getHitWarning().length() > 0) {
                              System.out.println(" WARNING: " +
                              byteReader.getHit(ih).getHitWarning());
                              }
                              }
                              } catch (Exception e) {
                                   Sstem.out.println("exception : " + e.getMessage()) ;
                              }

                              With a text/plain file "byteReader.getNumHits()" == 0.
                              So it's a condition to say that is a text/plain file.

                              Could you tell me if i'm wrong or not ?
                              thanks in advance,

                              Regards,

                              Denez