English 中文(简体)
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

最佳回答

Try Java Mime Magic Library

byte[] data = ...
MagicMatch match = Magic.getMagicMatch(data);
String mimeType = match.getMimeType();
问题回答

I m sure the library posted by @sfussenegger is the best solution, but I do it by hand with the following snippet that I hope it could help you.

DESCONOCIDO("desconocido", new byte[][] {}), PDF("PDF",
            new byte[][] { { 0x25, 0x50, 0x44, 0x46 } }), JPG("JPG",
            new byte[][] { { (byte) 0xff, (byte) 0xd8, (byte) 0xff,
                    (byte) 0xe0 } }), RAR("RAR", new byte[][] { { 0x52,
            0x61, 0x72, 0x21 } }), GIF("GIF", new byte[][] { { 0x47, 0x49,
            0x46, 0x38 } }), PNG("PNG", new byte[][] { { (byte) 0x89, 0x50,
            0x4e, 0x47 } }), ZIP("ZIP", new byte[][] { { 0x50, 0x4b } }), TIFF(
            "TIFF", new byte[][] { { 0x49, 0x49 }, { 0x4D, 0x4D } }), BMP(
            "BMP", new byte[][] { { 0x42, 0x4d } });

Regards.

PD: The best of it is that it doesn t have any dependency. PD2: No warranty about it s correctness! PD3: "desconocido" stands for "unknown" (in spanish)

The activation framework is Sun s answer to this. And you may well have this already in the classpath of your app server





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签