I have a problem when dertermine the content type when streaming files from servlet to the web client. What I am currently doing is set content type based on the file extension, for example for .txt files I choose "text/plain", etc. But this method is not that good cause I need to iterate over possible extensions to set the right content type, also sometimes the extensions does not give hint over its actual content type.

So is there any methods or libraries to auto-detect the content type of a file based on its content, instead of extension?