How can the MIME type of a file be known on the basis of its contents?
I don't want the detection to be based on file's extension as new extensions evolve everyday.
I don't think there is a reliable way. Some files, like JPG images, have a header one could read. Others, like plain text files, do not.
The only programmatic way I know of is ServletContext.getMimeType, but that relies on the container configuration, which in turn relies on web.xml, which contains a map of file extensions to mime types.