Search code examples
javajarmime-typesapache-tika

Why Apache Tika detect mimetype of a jar file as application/zip instead of application/java-archive?


I am trying to detect mime-type for a jar file code is working fine, but the issue is Apache Tika returns application/zip instead of application/java-archive for a java Jar file

I Uploaded Jar file from html

for (FileItem item : fileItems)
        {  
            String mimeType;
            try { 
                mimeType = tika.detect(item.getInputStream());
                System.out.println(mimeType); // returns application/zip
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

So my question is how to get application/java-archive mime-type for a jar file.

Is there any way to configure Apache Tika or I am doing something wrong


Solution

  • Thanks to @Gagravarr I able to fix this issue

    So what my code is look like

    public static TikaConfig getTikaConfig() {
            TikaConfig tikaConfig = null;
            try {
                return new TikaConfig();
            } catch (TikaException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return tikaConfig;
        }
    public static final TikaConfig tikaConfig = getTikaConfig();
    MediaType mediaType = tikaConfig.getDetector()
                                            .detect(TikaInputStream
                                                        .get(item.getInputStream()), new Metadata());
                    MimeType mimeType = tikaConfig.getMimeRepository().forName(mediaType.toString());
                    String extension =  mimeType.getExtension().split("\\.")[1];
    System.out.println("File extention is :"+extension);
    

    Most important gralde config

    compile group: 'org.apache.tika', name: 'tika-core', version: '1.24.1'
    runtime group: 'org.apache.tika', name: 'tika-parsers' version: '1.24.1'