34

In a web project, users upload their files, but when I receive them on the server, they are being stored as .tmp files rather than their original file extension (this is my preferred behavior as well).

However, this is causing a problem with Files.probeContentType(). While locally for me, on my Linux dev machine, Files.probeContentType() works correctly and does determine the right mime type, when I upload my project to the production server (amazon beanstalk), it doesn't seem to correctly determine the mime type.

From reading the javadocs, it seems that the implementation of Files.probeContentType() are different, and I think that on production server, it is reading the file extension and so, is unable to determine the content type.

What's a good, and quick alternative to Files.probeContentType() which will accept a File argument and return a string like image/png as the resulting mime type?

Ali
  • 261,656
  • 265
  • 575
  • 769
  • Can you store them as `filename.extension.tmp`? – PM 77-1 Oct 31 '13 at 16:34
  • 2
    @PM77-1 No, that's not what I want anyway. A user could just rename their .exe file to a .png and it would be accepted by the server. I'd rather it actually reads the file to determine the correct mime type. – Ali Oct 31 '13 at 16:35

3 Answers3

47

Take a look at the Apache Tika. It can easily determine a mime type:

 Tika tika = new Tika();
 File file = ...
 String mimeType = tika.detect(file);

Here's a minimal required maven dependency:

 <dependency>
    <groupId>org.apache.tika</groupId>
    <artifactId>tika-core</artifactId>
    <version>1.12</version>
 </dependency>
Jk1
  • 11,233
  • 9
  • 54
  • 64
  • Do you know which artifact for maven I need to add to my project from their maven page? http://mvnrepository.com/artifact/org.apache.tika . they have many ambigious ones like `tika-parsers` and just `tika`', I'm not sure which one I need for this purpose. – Ali Oct 31 '13 at 16:43
  • Sure, see updated answer. tika-core was completely enough for my project when I was facing the similar issue – Jk1 Oct 31 '13 at 16:47
  • Very helpful thanks ! Before adding the dependency, make sure to get the latest version by checking here : http://mvnrepository.com/artifact/org.apache.tika/tika-core – c4k Jan 04 '15 at 16:58
  • I needed to include tika-parsers to handle detecting mimetypes of jpgs and tiffs. – EdgeCaseBerg Nov 10 '15 at 20:18
  • @EdgeCaseBerg This works for me with Tika 1.12, core only (no tika-parsers): `t.detect("file.jpeg")`, `t.detect("file.jpg")`, `t.detect("file.tiff")` (results in `image/jpeg image/jpeg image/tiff`). – KajMagnus May 03 '16 at 04:41
  • This just works. It event returns `text/x-log` for an empty file ending with .log extension. – Halil Feb 28 '17 at 11:08
12

This answer suggests using:

InputStream is = new BufferedInputStream(new FileInputStream(file));
mimeType = URLConnection.guessContentTypeFromStream(is);
//...close stream
Community
  • 1
  • 1
summerbulb
  • 5,709
  • 8
  • 37
  • 83
-3

This also works

String mimeType = URLConnection.guessContentTypeFromName(new File(path).getName());

Ben
  • 3,989
  • 9
  • 48
  • 84
Janakiram
  • 1
  • 1
  • 1
    This will resolve type from file name instead of trying to read metadata inside the file, the opposite of what OP asked – Ben Apr 14 '20 at 09:35