• Post Reply Bookmark Topic Watch Topic
  • New Topic

Detecting MIME types of a file

 
Mahendra Kariya
Greenhorn
Posts: 26
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!