HuTool the judgment uploaded file type

File Type judgment -FileTypeUtil

Origin

When uploading files, sometimes we need to determine the file type. But it can not simply be judged by the extension (such as by preventing malicious scripts uploaded to the server), so we need to determine common file types on the server through the head several bits to read the file.

use

The use of very simple tools, by calling FileTypeUtil.getTypecan be determined, this method also provides many overloaded methods, for reading different files and streams.

File file = FileUtil.file("d:/test.jpg"); String type = FileTypeUtil.getType(file); //输出 jpg则说明确实为jpg文件 Console.log(type);


Principles and limitations

This class is judged by the first N byte read the file stream file types, in a class by us Map form common file types do mappings that are on the network to collect from. In other words, we can only identify a limited number of file types. But these types already covered by the common image, audio, video, Office document type, can cope with most usage scenarios.

For some text file formats and we can not determine its type header byte, for example JSON, on the nature of this type of file is a text file, we should read its text, through its syntax to determine the type.

Guess you like

Origin www.cnblogs.com/mr-wuxiansheng/p/11225829.html