Java method for reading mimeType of picture

1、 Problem description

During project development, we often encounter a kind of file upload problem, that is, what format is the image obtained. In many cases, many people use the suffix to judge, as shown below.

However, this method is quite unreliable. We can try to upload zip files, RMVB files, CSS, JS modified suffix names JPG or PNG, or upload them to the server, which causes dirty data on our server. In addition, some picture files are modified to the wrong extension, and some browsers may not be able to display this picture.

2、 Solution

In the computer system, all media files have [identifiers], and zip and pictures themselves belong to media files. Therefore, we can judge whether the pictures are legal by encoding and decoding.

1. Judgment marking method

2. Core method

3. Test code

Normal test

output

image/jpeg

Modify extension test

① Modify oschina JPEG is oschina png

② Copy oschina Png delete extension

output

image/jpeg image/jpeg

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>