Class ImageValidator
java.lang.Object
cloud.opencode.base.image.validation.ImageValidator
Image Validator
图片验证器
Validates images for format, size, and security.
验证图片的格式、大小和安全性。
Usage Examples | 使用示例:
// Validate file
ImageValidator.validate(path);
// Validate with limits
ImageValidator.validate(path, 10_000_000, 4000, 4000);
// Check magic numbers
boolean valid = ImageValidator.checkMagicNumber(bytes);
Features | 主要功能:
- File size and dimension validation - 文件大小和尺寸验证
- Magic number verification for format detection - 用于格式检测的魔数验证
- Configurable maximum file size and dimensions - 可配置的最大文件大小和尺寸
- Since:
- JDK 25, opencode-base-image V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longDefault maximum file size (10 MB) 默认最大文件大小(10 MB)static final intDefault maximum height (8000 pixels) 默认最大高度(8000 像素)static final intDefault maximum width (8000 pixels) 默认最大宽度(8000 像素) -
Method Summary
Modifier and TypeMethodDescriptionstatic booleancheckMagicNumber(byte[] bytes) Check magic number 检查魔数static ImageFormatdetectFormat(byte[] bytes) Deprecated.static Optional<ImageFormat> detectFormatOptional(byte[] bytes) Detect format from magic number (null-safe) 从魔数检测格式(空值安全)static booleanisValidImage(byte[] bytes) Check if bytes are a valid image 检查字节数组是否为有效图片static booleanisValidImage(Path path) Check if file is a valid image 检查文件是否为有效图片static voidvalidate(byte[] bytes) Validate image bytes 验证图片字节数组static voidvalidate(byte[] bytes, long maxFileSize, int maxWidth, int maxHeight) Validate image bytes with limits 验证图片字节数组(带限制)static voidValidate image file 验证图片文件static voidValidate image file with limits 验证图片文件(带限制)static booleanValidate file extension matches content 验证文件扩展名是否与内容匹配
-
Field Details
-
DEFAULT_MAX_FILE_SIZE
public static final long DEFAULT_MAX_FILE_SIZEDefault maximum file size (10 MB) 默认最大文件大小(10 MB)- See Also:
-
DEFAULT_MAX_WIDTH
public static final int DEFAULT_MAX_WIDTHDefault maximum width (8000 pixels) 默认最大宽度(8000 像素)- See Also:
-
DEFAULT_MAX_HEIGHT
public static final int DEFAULT_MAX_HEIGHTDefault maximum height (8000 pixels) 默认最大高度(8000 像素)- See Also:
-
-
Method Details
-
validate
Validate image file 验证图片文件- Parameters:
path- the file path | 文件路径- Throws:
ImageValidationException- if validation fails | 如果验证失败
-
validate
public static void validate(Path path, long maxFileSize, int maxWidth, int maxHeight) throws ImageValidationException Validate image file with limits 验证图片文件(带限制)- Parameters:
path- the file path | 文件路径maxFileSize- maximum file size in bytes | 最大文件大小(字节)maxWidth- maximum width in pixels | 最大宽度(像素)maxHeight- maximum height in pixels | 最大高度(像素)- Throws:
ImageValidationException- if validation fails | 如果验证失败
-
validate
Validate image bytes 验证图片字节数组- Parameters:
bytes- the image bytes | 图片字节数组- Throws:
ImageValidationException- if validation fails | 如果验证失败
-
validate
public static void validate(byte[] bytes, long maxFileSize, int maxWidth, int maxHeight) throws ImageValidationException Validate image bytes with limits 验证图片字节数组(带限制)- Parameters:
bytes- the image bytes | 图片字节数组maxFileSize- maximum file size in bytes | 最大文件大小(字节)maxWidth- maximum width in pixels | 最大宽度(像素)maxHeight- maximum height in pixels | 最大高度(像素)- Throws:
ImageValidationException- if validation fails | 如果验证失败
-
isValidImage
Check if file is a valid image 检查文件是否为有效图片- Parameters:
path- the file path | 文件路径- Returns:
- true if valid image | 如果是有效图片返回true
-
isValidImage
public static boolean isValidImage(byte[] bytes) Check if bytes are a valid image 检查字节数组是否为有效图片- Parameters:
bytes- the image bytes | 图片字节数组- Returns:
- true if valid image | 如果是有效图片返回true
-
checkMagicNumber
public static boolean checkMagicNumber(byte[] bytes) Check magic number 检查魔数- Parameters:
bytes- the image bytes | 图片字节数组- Returns:
- true if magic number matches | 如果魔数匹配返回true
-
detectFormat
Deprecated.usedetectFormatOptional(byte[])for null-safe detectionDetect format from magic number 从魔数检测格式- Parameters:
bytes- the image bytes | 图片字节数组- Returns:
- the detected format or null | 检测到的格式或null
-
detectFormatOptional
Detect format from magic number (null-safe) 从魔数检测格式(空值安全)- Parameters:
bytes- the image bytes | 图片字节数组- Returns:
- an Optional containing the detected format, or empty if unrecognized 包含检测到格式的 Optional,如果无法识别则为空
- Since:
- V1.0.1
-
validateExtensionMatchesContent
Validate file extension matches content 验证文件扩展名是否与内容匹配- Parameters:
path- the file path | 文件路径- Returns:
- true if extension matches content | 如果扩展名与内容匹配返回true
-
detectFormatOptional(byte[])for null-safe detection