Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HowTo extract MimeType from a byte[] [duplicate]

I've a web page that that can be used to upload files.
Now I need to check if the file type is correct (zip, jpg, pdf,...).

I can use the mimeType that comes with the request but I don't trust the user and let's say I want to be sure that nobody is able to upload a .gif file that was renamed in .jpg
I think that in this case I should inspect the magic number.
This is a java library I've found that seems to achieve what I need 'extract the mimetype from the magic number'.
Is this a correct solution or what do you suggest?

UPDATE: I've found the mime-util project and it seems very good and up-to-date! (maybe better then Java Mime Magic Library?)
Here is a list of utility projects that can help you to extract mime-types

like image 538
mickthompson Avatar asked Dec 16 '09 15:12

mickthompson


People also ask

Can a file have multiple MIME types?

Multiple MIME types can use one extension. For example, if your organization uses multiple versions of a program, you can define a MIME type for each version; however, file names of all versions use the same extension.

What is MIME type in C#?

MIME stands for "Multipurpose Internet Mail Extensions." It is a standard way of classifying file types on the Internet. By specifying a MIME type, application can easily identify the type of file and can extract more information and attributes about a file.

What is MIME type to download any type of file?

The recommended MIME type for that is application/octet-stream : The "octet-stream" subtype is used to indicate that a body contains arbitrary binary data.


Video Answer


1 Answers

Try Java Mime Magic Library

byte[] data = ... MagicMatch match = Magic.getMagicMatch(data); String mimeType = match.getMimeType(); 
like image 153
sfussenegger Avatar answered Oct 14 '22 02:10

sfussenegger