Java method to determine whether it is a picture:
1. Determine whether it is a picture by judging the file suffix name
String extension = ""; int i = fileName.lastIndexOf('.'); if (i > 0) { extension = fileName.substring(i+1); } //... if("jpg".equals(extension)){ //your code }
2. Determine whether it is a picture by judging the file header.
If the suffix is unknown or the suffix has been modified, the file header can still be used to determine what file type the file is. We can use a text editing tool such as UltraEdit to open the file (in hexadecimal mode), and then see what characters are in the file header. The following are the file header characters for common image types (hexadecimal)
JPEG (jpg),文件头:FFD8FF PNG (png),文件头:89504E47 GIF (gif),文件头:47494638 TIFF (tif),文件头:49492A00 Windows Bitmap (bmp),文件头:424D
3. Judge by MimetypesFileTypeMap
public class ImageCheck { private MimetypesFileTypeMap mtftp; public ImageCheck(){ mtftp = new MimetypesFileTypeMap(); /* 不添加下面的类型会造成误判 mtftp.addMimeTypes("image png tif jpg jpeg bmp"); } public boolean isImage(File file){ String mimetype= mtftp.getContentType(file); String type = mimetype.split("/")[0]; return type.equals("image"); } }
4. Judge by ImageIO
try { // 通过ImageReader来解码这个file并返回一个BufferedImage对象 // 如果找不到合适的ImageReader则会返回null,我们可以认为这不是图片文件 // 或者在解析过程中报错,也返回false Image image = ImageIO.read(file); return image != null; } catch(IOException ex) { return false; }
Note: The applicable image format for this method is bmp/gif/jpg /png
For more java knowledge, please pay attention to java basic tutorial.
The above is the detailed content of Java determines whether it is a picture. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
