从 Base64 编码的字符串中检索 MIME 类型

2022-01-21 00:00:00 mime base64 java

假设一个用 Base64 字符串编码并提供给我的文件(例如 myfile.jpeg).我无法知道文件类型是什么.我想将字符串解码为文件(本例中为图像).我如何知道文件的类型(例如 jpeg)?

Let' say a file (e.g. myfile.jpeg) encoded in Base64 String and given to me. There is no way I know what the file type was. I'd like to decode the string into a file (an image in this example). How do I know the type of the file (e.g jpeg)?

推荐答案

一般来说,base 64 编码的字符串绝对可以包含任何数据,因此无法知道它的文件类型.

In general, a base 64-encoded string could contain absolutely any data, so there is no way to know its file type.

要确定它是否是 JPEG 图像的实例,您需要对其进行 base64 解码,然后执行诸如检查其 幻数,这有助于告诉您文件不是.您仍然需要做更多的工作来确定它是否是有效的 JPEG 图像.

To determine if it is an instance of a JPEG image, you'd need to base64-decode it, and then do something like checking its magic number, which is useful in telling you what the file isn't. You'd still need to do more work to determine if it is a valid JPEG image.

相关文章