是否可以从其字节数组内容中找到图像文件扩展名? [重复]

时间:2020-12-01 15:45:10

This question already has an answer here:

这个问题在这里已有答案:

Having an image file already stored into a database as a byte array, is it possible to find the image file extension from the byte array contents?

将图像文件作为字节数组存储到数据库中,是否可以从字节数组内容中找到图像文件扩展名?

I can get the byte array from the database. I need to show the image file into the screen.

我可以从数据库中获取字节数组。我需要将图像文件显示在屏幕上。

Before displaying it, I want to have the image file extension, such as .png,.jpg or .jpeg, etc..

在显示之前,我想要有图像文件扩展名,例如.png,.jpg或.jpeg等。

2 个解决方案

#1


5  

Most file formats have a "Magic Number" at the beginning in the form of a couple of bytes that indicates what file it is.

大多数文件格式在开头都有一个“幻数”,以几个字节的形式表示它是什么文件。

GIF starts with "GIF89a" or "GIF87a", i.e. bytes 47,49,46,38,39,61 or 47,49,46,38,37,61 (both hexadecimal)

GIF以“GIF89a”或“GIF87a”开头,即字节47,49,46,38,39,61或47,49,46,38,37,61(均为十六进制)

JPEG starts with FF,D8 and ends with FF,D9

JPEG以FF,D8开头,以FF,D9结束

PNG begins with 89,50,4E,47,0D,0A,1A,0A

PNG以89,50,4E,47,0D,0A,1A,0A开始

See more about Magic Numbers on Wikipedia

在Wikipedia上查看有关Magic Numbers的更多信息

For other formats you can google the magic number. Some don't have any though.

对于其他格式,您可以谷歌魔术数字。有些人没有。

#2


1  

The Java Mime Magic Library could also help here (it is a Java library for determining the MIME type of files or streams).

Java Mime Magic Library也可以在这里提供帮助(它是一个用于确定文件或流的MIME类型的Java库)。

https://github.com/arimus/jmimemagic

https://github.com/arimus/jmimemagic

http://sourceforge.net/projects/jmimemagic/

http://sourceforge.net/projects/jmimemagic/

#1


5  

Most file formats have a "Magic Number" at the beginning in the form of a couple of bytes that indicates what file it is.

大多数文件格式在开头都有一个“幻数”,以几个字节的形式表示它是什么文件。

GIF starts with "GIF89a" or "GIF87a", i.e. bytes 47,49,46,38,39,61 or 47,49,46,38,37,61 (both hexadecimal)

GIF以“GIF89a”或“GIF87a”开头,即字节47,49,46,38,39,61或47,49,46,38,37,61(均为十六进制)

JPEG starts with FF,D8 and ends with FF,D9

JPEG以FF,D8开头,以FF,D9结束

PNG begins with 89,50,4E,47,0D,0A,1A,0A

PNG以89,50,4E,47,0D,0A,1A,0A开始

See more about Magic Numbers on Wikipedia

在Wikipedia上查看有关Magic Numbers的更多信息

For other formats you can google the magic number. Some don't have any though.

对于其他格式,您可以谷歌魔术数字。有些人没有。

#2


1  

The Java Mime Magic Library could also help here (it is a Java library for determining the MIME type of files or streams).

Java Mime Magic Library也可以在这里提供帮助(它是一个用于确定文件或流的MIME类型的Java库)。

https://github.com/arimus/jmimemagic

https://github.com/arimus/jmimemagic

http://sourceforge.net/projects/jmimemagic/

http://sourceforge.net/projects/jmimemagic/