chinabugotech/hutool · error · IllegalArgumentException

文件类型{}不支持

Error message

文件类型{}不支持

What it means

After confirming the file exists, fileTypeValidation detects the image's type (via FileTypeUtil.getType) and checks it against the allowed imagesType array. If the detected type is not in the allowed set, it throws IllegalArgumentException ('文件类型{}不支持' = 'file type {} is not supported'), embedding the detected type.

Source

Thrown at hutool-core/src/main/java/cn/hutool/core/img/BackgroundRemoval.java:350

	// -------------------------------------------------------------------------- private

	/**
	 * 文件类型验证
	 * 根据给定文件类型数据,验证给定文件类型.
	 *
	 * @param input      需要进行验证的文件
	 * @param imagesType 文件包含的类型数组
	 * @return 返回布尔值 false:给定文件的文件类型在文件数组中  true:给定文件的文件类型 不在给定数组中。
	 */
	private static boolean fileTypeValidation(File input, String[] imagesType) {
		if (!input.exists()) {
			throw new IllegalArgumentException("给定文件为空");
		}
		// 获取图片类型
		String type = FileTypeUtil.getType(input);
		// 类型对比
		if (!ArrayUtil.contains(imagesType, type)) {
			throw new IllegalArgumentException(StrUtil.format("文件类型{}不支持", type));
		}
		return false;
	}
}

View on GitHub (pinned to 8870454b2a)

Solutions

  1. Convert the image to a supported format (PNG/JPG) before calling removal.
  2. Expand the allowed imagesType array to include the detected format if the removal logic actually supports it.
  3. Pre-validate the type with FileTypeUtil.getType and surface a clear error to the user.

Example fix

// before
BackgroundRemoval.removeBg(bmpFile, ...);
// after - convert to PNG first
File png = ImgUtil.convert(bmpFile, "png");
BackgroundRemoval.removeBg(png, ...);
Defensive patterns

Strategy: validation

Validate before calling

void requireSupportedType(File f, String[] allowed){ String t = FileTypeUtil.getType(f); if(!ArrayUtil.contains(allowed, t)) throw new IllegalArgumentException("unsupported type "+t); }

Type guard

boolean isSupportedType(File f, String[] allowed){ return f!=null && f.exists() && ArrayUtil.contains(allowed, FileTypeUtil.getType(f)); }

Try / catch

try { BackgroundRemoval.removeBg(f, ...); }
catch (IllegalArgumentException e){ if(e.getMessage().contains("不支持")) { /* convert to PNG */ } else throw e; }

Prevention

When it happens

Trigger: Passing a BMP, GIF, TIFF, WEBP, or HEIC image to a background-removal API that only accepts JPG/PNG; a file whose extension does not match its actual bytes; a corrupted or mis-detected file.

Common situations: Supported-format allowlists narrower than the user's input; format detection mismatch; converting images but feeding the wrong type to removal.

Related errors


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