Since 1.7, Java has .FileTypeDetector, which defines a standard API for determining a file type in implementation specific way. Add a line for every mime type you need and default implementation doesn't provide (there are hundreds of mime types and list grows as time goes by).Įxample entries for png and js files would be: image/png png PNGįor mime.types file format, see more details here:.Create file named 'mime.types' in META-INF folder in your project.It is, however, super simple to add any content type you may need: ![]() In its vanilla implementation this does not do much (i.e. Since 1.6, Java has MimetypesFileTypeMap, as pointed in one of the answers above, and it is the simplest way to determine mime type: new MimetypesFileTypeMap().getContentType( fileName ) I like to use standard facilities provided by JDK first, and if that doesn't work, I'll go and look for something else. I use MimetypesFileTypeMap and add any mime that is not there and I specifically need it, into mime.types file.įirst of all, MIME types list is huge, see here: ContentInfoUtil util = new ContentInfoUtil("/etc/magic") ĬontentInfo info = util.findMatch("/tmp/upload.tmp") ĬontentInfo info = util.findMatch(inputStream) ĬontentInfo info = util.findMatch(contentByteArray) if you want to use a different config file(s), you can load them by hand: With SimpleMagic you can do something like: // create a magic utility using the internal magic fileĬontentInfoUtil util = new ContentInfoUtil() ![]() ![]() ![]() I tried Apache Tika but it is huge with tons of dependencies, URLConnection doesn't use the bytes of the files, and MimetypesFileTypeMap also just looks at files names. It is designed to read and run the Unix file(1) command magic files that are a part of most ~Unix OS configurations. I've published my SimpleMagic Java package which allows content-type (mime-type) determination from files and byte arrays. I was just wondering how most people fetch a mime type from a file in Java?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |