Correct use of Apache Tika MediaType

后端 未结 1 1425
醉梦人生
醉梦人生 2021-01-17 05:07

I want to use APache Tika\'s MediaType class to compare mediaTypes.

I first use Tika to detect the MediaType. Then I want to start an action according to the MediaTy

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-17 05:42

    What you'll need to do is walk the types hierarchy, until you either find what you want, or run out of things to check. That can be done with recursion, or could be done with a loop

    The key method you need is MediaTypeRegistry.getSupertype(MediaType)

    Your code would want to be something like:

    // Define your media type constants here
    MediaType FOO = MediaType.parse("application/foo");
    
    // Work out the file's type
    MediaType type = detector.detect(stream, metadata);
    
    // Is it one we want in the tree?
    while (type != null && !type.equals(MediaType.OCTET_STREAM)) {
       if (type.equals(MediaType.Application_XML)) {
           doThingForXML();
       } else if (type.equals(MediaType.APPLICATION_ZIP)) { 
           doThingForZip();
       } else if (type.equals(FOO)) {
           doThingForFoo();
       } else {
           // Check parent
           type = registry.getSuperType(type);
       }
    }
    

    0 讨论(0)
提交回复
热议问题