chinabugotech/hutool · error · IllegalArgumentException

Image type of file [{}] is not supported!

Error message

Image type of file [{}] is not supported!

What it means

ImgUtil.read(File) delegates to javax.imageio.ImageIO.read, which returns null when no registered ImageReader can decode the file (unknown/unsupported format or corrupted content). Hutool converts that null into IllegalArgumentException naming the file.

Source

Thrown at hutool-core/src/main/java/cn/hutool/core/img/ImgUtil.java:1951

	}

	/**
	 * 从文件中读取图片
	 *
	 * @param imageFile 图片文件
	 * @return 图片
	 * @since 3.2.2
	 */
	public static BufferedImage read(File imageFile) {
		BufferedImage result;
		try {
			result = ImageIO.read(imageFile);
		} catch (IOException e) {
			throw new IORuntimeException(e);
		}

		if (null == result) {
			throw new IllegalArgumentException("Image type of file [" + imageFile.getName() + "] is not supported!");
		}

		return result;
	}

	/**
	 * 从URL中获取或读取图片对象
	 *
	 * @param url URL
	 * @return {@link Image}
	 * @since 5.5.8
	 */
	public static Image getImage(URL url) {
		return Toolkit.getDefaultToolkit().getImage(url);
	}

	/**
	 * 从{@link Resource}中读取图片

View on GitHub (pinned to 8870454b2a)

Solutions

  1. Verify the file is a supported format (PNG/JPG/GIF/BMP) using FileTypeUtil before reading.
  2. Add the required ImageIO reader plugin (e.g. webp-imageio, TwelveMonkeys ImageIO) for advanced formats.
  3. Re-download/re-encode the source if it is corrupt.

Example fix

// before
BufferedImage img = ImgUtil.read(new File("logo.svg"));
// after - use a supported raster format, or add an SVG/extra reader
BufferedImage img = ImgUtil.read(new File("logo.png"));
Defensive patterns

Strategy: validation

Validate before calling

void requireReadableImage(File f){ String[] ok = {"jpg","png","gif","bmp"}; if(!ArrayUtil.contains(ok, FileTypeUtil.getType(f))) throw new IllegalArgumentException("not a decodable image: "+f); }

Type guard

boolean isDecodableImageFile(File f){ return f!=null && f.exists() && ArrayUtil.contains(new String[]{"jpg","png","gif","bmp"}, FileTypeUtil.getType(f)); }

Try / catch

try { img = ImgUtil.read(file); }
catch (IllegalArgumentException e){ if(e.getMessage().contains("not supported")) { /* convert / add reader */ } else throw e; }

Prevention

When it happens

Trigger: Reading a file that is not a decodable image (e.g. SVG, HEIC, AVIF, a text file, a truncated/corrupted JPG/PNG). ImageIO has no reader for that format on the classpath.

Common situations: Unsupported modern formats (WEBP/AVIF/HEIC need extra readers); files with wrong extension; partially downloaded/corrupt images; minimal JRE without the image plugins.

Related errors


AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14). Data as JSON: /api/errors/ef1f97d49c505dab. Report an issue: GitHub.