'Use Apache tika get .mp4 file contentType,got "video/quicktime"
I use this function to get files contentType ,when use the .jpg file receive "image/jpg". use .mp4 receive "video/quicktime"
public static String detectMediaType(String fileName,byte[] data) {
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
AutoDetectParser parser = new AutoDetectParser();
Detector detector = parser.getDetector();
Metadata md = new Metadata();
md.add(Metadata.RESOURCE_NAME_KEY, fileName);
try{
MediaType mediaType = detector.detect(inputStream, md);
return mediaType.toString();
}catch (IOException ex){
throw new RuntimeException("Failed to detect mediaType for file: " + fileName,ex);
}
}
Solution 1:[1]
This is expected because you are using tika detector functionality which for mp4 or for other QuickTime formats returns generic 'video/quicktime'. If you need to get exact type like 'video/mp4' you should additionally use tika parser, for example:
if (mediaType.equals(MediaType.video("quicktime"))) {
Parser parser = new AutoDetectParser();
ContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
ParseContext context = new ParseContext();
parser.parse(inputStream, handler, metadata, context);
}
Solution 2:[2]
I had the same problem using Tikas DefaultDetector.
The solution, as indicated here: https://tika.apache.org/2.3.0/detection.html (under "Container Aware Detection"), is to use TikeInputStream, instead of a standard InputStream.
detector.detect(TikaInputStream.get(inputStream), metadata);
Don't forget to close the streams
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | zygimantus |
| Solution 2 | uhon |
