Class ImageValidator

java.lang.Object
cloud.opencode.base.image.validation.ImageValidator

public final class ImageValidator extends Object
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

    Fields
    Modifier and Type
    Field
    Description
    static final long
    Default maximum file size (10 MB) 默认最大文件大小(10 MB)
    static final int
    Default maximum height (8000 pixels) 默认最大高度(8000 像素)
    static final int
    Default maximum width (8000 pixels) 默认最大宽度(8000 像素)
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    checkMagicNumber(byte[] bytes)
    Check magic number 检查魔数
    detectFormat(byte[] bytes)
    Deprecated.
    use detectFormatOptional(byte[]) for null-safe detection
    detectFormatOptional(byte[] bytes)
    Detect format from magic number (null-safe) 从魔数检测格式(空值安全)
    static boolean
    isValidImage(byte[] bytes)
    Check if bytes are a valid image 检查字节数组是否为有效图片
    static boolean
    Check if file is a valid image 检查文件是否为有效图片
    static void
    validate(byte[] bytes)
    Validate image bytes 验证图片字节数组
    static void
    validate(byte[] bytes, long maxFileSize, int maxWidth, int maxHeight)
    Validate image bytes with limits 验证图片字节数组(带限制)
    static void
    validate(Path path)
    Validate image file 验证图片文件
    static void
    validate(Path path, long maxFileSize, int maxWidth, int maxHeight)
    Validate image file with limits 验证图片文件(带限制)
    static boolean
    Validate file extension matches content 验证文件扩展名是否与内容匹配

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_MAX_FILE_SIZE

      public static final long DEFAULT_MAX_FILE_SIZE
      Default maximum file size (10 MB) 默认最大文件大小(10 MB)
      See Also:
    • DEFAULT_MAX_WIDTH

      public static final int DEFAULT_MAX_WIDTH
      Default maximum width (8000 pixels) 默认最大宽度(8000 像素)
      See Also:
    • DEFAULT_MAX_HEIGHT

      public static final int DEFAULT_MAX_HEIGHT
      Default maximum height (8000 pixels) 默认最大高度(8000 像素)
      See Also:
  • Method Details

    • validate

      public static void validate(Path path) throws ImageValidationException
      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

      public static void validate(byte[] bytes) throws ImageValidationException
      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

      public static boolean isValidImage(Path path)
      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(since="1.0.1") public static ImageFormat detectFormat(byte[] bytes)
      Deprecated.
      use detectFormatOptional(byte[]) for null-safe detection
      Detect format from magic number 从魔数检测格式
      Parameters:
      bytes - the image bytes | 图片字节数组
      Returns:
      the detected format or null | 检测到的格式或null
    • detectFormatOptional

      public static Optional<ImageFormat> detectFormatOptional(byte[] bytes)
      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

      public static boolean validateExtensionMatchesContent(Path path)
      Validate file extension matches content 验证文件扩展名是否与内容匹配
      Parameters:
      path - the file path | 文件路径
      Returns:
      true if extension matches content | 如果扩展名与内容匹配返回true