Java 如何从字节中提取 MimeType[]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1915317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HowTo extract MimeType from a byte[]
提问by mickthompson
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.
Thisis 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?
我有一个可用于上传文件的网页。
现在我需要检查文件类型是否正确(zip、jpg、pdf 等)。
我可以使用请求附带的 mimeType 但我不信任用户,假设我想确保没有人能够上传以 .jpg 重命名的 .gif 文件
我认为在这种情况下我应该检查幻数。
这是我发现的一个 java 库,它似乎实现了我需要的“从幻数中提取 mimetype”。
这是正确的解决方案还是您有什么建议?
UPDATE:I've found the mime-util projectand it seems very good and up-to-date! (maybe better then Java Mime Magic Library?)
Here is a listof utility projects that can help you to extract mime-types
更新:我找到了mime-util 项目,它看起来非常好并且是最新的!(也许比 Java Mime Magic Library 更好?)
以下是可以帮助您提取 mime 类型的实用程序项目列表
采纳答案by sfussenegger
byte[] data = ...
MagicMatch match = Magic.getMagicMatch(data);
String mimeType = match.getMimeType();
回答by James B
The activation framework is Sun's answer to this. And you may well have this already in the classpath of your app server
激活框架是Sun 对此的回答。你很可能已经在你的应用服务器的类路径中
回答by ATorras
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.
我确信@sfussenegger 发布的库是最好的解决方案,但我使用以下代码片段手工完成,希望对您有所帮助。
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)
PD:最好的是它没有任何依赖性。PD2:不保证其正确性!PD3:“desconocido”代表“未知”(西班牙语)