chinabugotech/hutool · error · IllegalArgumentException

Image type is not supported!

Error message

Image type is not supported!

What it means

ImgUtil.read(InputStream) wraps ImageIO.read, which returns null when no reader can decode the stream. Hutool raises IllegalArgumentException (message: 'Image type is not supported!') because the resulting BufferedImage is null.

Source

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

	}

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

		if (null == result) {
			throw new IllegalArgumentException("Image type is not supported!");
		}

		return result;
	}

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

View on GitHub (pinned to 8870454b2a)

Solutions

  1. Buffer the stream into a byte[] and inspect magic bytes / content type before reading.
  2. Ensure the stream is fresh (mark/reset or re-open) and the format is supported.
  3. Add ImageIO reader plugins for advanced formats, or convert upstream to PNG/JPG.

Example fix

// before
BufferedImage img = ImgUtil.read(request.getInputStream()); // unknown bytes
// after
byte[] bytes = IoUtil.readBytes(request.getInputStream());
if (!isSupportedImageMagic(bytes)) throw new IllegalArgumentException("not an image");
BufferedImage img = ImgUtil.read(IoUtil.toStream(bytes));
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

boolean isDecodableImageStream(InputStream in) throws IOException { byte[] h = new byte[12]; in.mark(h.length); int n=IoUtil.read(in,h,0,h.length); in.reset(); return n>=3 && (isJpeg(h)||isPng(h)||isGif(h)||isBmp(h)); }

Try / catch

try { img = ImgUtil.read(in); }
catch (IllegalArgumentException e){ if(e.getMessage().contains("not supported")) { /* re-open stream / convert */ } else throw e; }

Prevention

When it happens

Trigger: Passing an InputStream whose bytes are not a decodable image, or a stream that has already been consumed/positioned past its header, or an unsupported format with no registered reader.

Common situations: Reading from a network/multipart stream whose content type is not a baseline image; reusing a stream after it was read once; compressed/encrypted bytes mistakenly fed as an image.

Related errors


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