Java - How to get the mime type of a file from its stream?

Is it possible to get the content type / mime type of a file from a file read as a stream from its stored location.

I'm a bit new to the file and stuff, I've searched all day but couldn't get what I need.

Please help me with this. thank you in advance

Edit: - Edit:

Guys are grateful for your comments.

My java version is 6 and I cannot trust the filename to get its mime type.

+3


source to share


4 answers


Use this:

Files.probeContentType(path)

      



By the way, your question is a duplicate of this SO post . Please read this carefully before asking your next question.

+5


source


The MIME type of the file is not part of the file itself. Rather, it is usually configured and provided by the server from which the file is accessed; for example via an HTTP header. Some binaries have so-called "magic numbers" that can be used to some extent to identify the contents of the file. See here for the general directory. For example, you can look at the first few bytes of a file stream and check a directory to determine the type. However, there is no guarantee that this method will be accurate.



Like others have suggested, you can use Files#probeContentType()

, but that is the filename. If you only have access to the stream, then you're out of luck. In addition, he also suffers from the same mistakes.

+3


source


From Java Docs: http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#probeContentType(java.nio.file.Path)

Use

probeContentType ()

+1


source


Maybe this link will help:

mime file type

+1


source







All Articles