\n
Update\nUsing org.apache.tika (v2.4)
\nTika tika = new Tika();\nString myType = tika.detect(filePath);\n
\nIf the file extension is not present I get the filetype as "application/octet-stream". The image is originally jpeg so I was expecting it to detect it as an image.
\n","author":{"@type":"Person","name":"Ashar"},"upvoteCount":1,"answerCount":1,"acceptedAnswer":null}}Reputation: 794
I read that Files.probeContentType(path) can be used to get the file type of a file without relying on file extensions. However in my case, if I remove the file type, the function returns the file type "null". If I attach the correct extension, then I get the type as "image/jpeg". Can anyone what is going on here or is my understanding of probeContentType not correct? I'm expecting it to return "image/jpeg" irrespective of the fact that it has file extension or not.
This is the case where I deliberately removed the extension from the file "Nature.jpg":
Update Using org.apache.tika (v2.4)
Tika tika = new Tika();
String myType = tika.detect(filePath);
If the file extension is not present I get the filetype as "application/octet-stream". The image is originally jpeg so I was expecting it to detect it as an image.
Upvotes: 1
Views: 3638
Reputation: 1187
While it might not rely on the file extension in all implementations, it might use it in others. According to Oracle - "The implementation of this method is highly platform specific and is not infallible."
"The means by which a file type detector determines the file type is highly implementation specific. A simple implementation might examine the file extension (a convention used in some platforms) and map it to a file type. In other cases, the file type may be stored as a file attribute or the bytes in a file may be examined to guess its file type." - https://docs.oracle.com/javase/8/docs/api/java/nio/file/spi/FileTypeDetector.html
You can try Apache Tika as an alternative, I've had good experiences with it - https://tika.apache.org/
Upvotes: 1