chinabugotech/hutool · error · IllegalArgumentException
给定文件为空
Error message
给定文件为空
What it means
BackgroundRemoval.fileTypeValidation throws IllegalArgumentException ('给定文件为空' = 'the given file is empty/missing') when the File passed to it does not exist (input.exists() is false). Despite the word 'empty', the actual condition is non-existence.
Source
Thrown at hutool-core/src/main/java/cn/hutool/core/img/BackgroundRemoval.java:344
return ImgUtil.toHex(Integer.parseInt(strings[0]), Integer.parseInt(strings[1]),
Integer.parseInt(strings[2]));
}
return StrUtil.EMPTY;
}
// -------------------------------------------------------------------------- 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
- Verify input.exists() && input.isFile() before calling the removal method.
- Resolve paths to absolute and log them to catch typos / wrong working directory.
- Ensure the file is present (re-download, recreate) before processing.
Example fix
// before
BackgroundRemoval.removeBg(new File("/tmp/missing.png"), ...);
// after
File f = new File("/tmp/missing.png");
if (!f.isFile()) throw new FileNotFoundException(f.getAbsolutePath());
BackgroundRemoval.removeBg(f, ...); Defensive patterns
Strategy: validation
Validate before calling
void requireExistingImage(File f){ if(f==null || !f.exists() || !f.isFile()) throw new FileNotFoundException(String.valueOf(f)); } Type guard
boolean isReadableFile(File f){ return f != null && f.exists() && f.isFile() && f.canRead(); } Try / catch
try { BackgroundRemoval.removeBg(file, ...); }
catch (IllegalArgumentException e){ if(e.getMessage().contains("给定文件为空")) { /* report missing path */ } else throw e; } Prevention
- Check exists()/isFile() before any file-based image call.
- Log absolute paths to catch working-directory issues.
- Stream files to stable, verified locations before processing.
When it happens
Trigger: Calling a background-removal method with a File whose path does not exist, points to a deleted/moved file, or has a typo in the path.
Common situations: User-uploaded file already cleaned up; path from config that is wrong; file on a network mount that is unavailable; relative path resolved against the wrong working directory.
Related errors
- 图片流是空的
- 文件类型{}不支持
- Destination Dir must be a Directory !
- RGB must be 0~255!
- Image type of file [{}] is not supported!
AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14).
Data as JSON: /api/errors/c6b1b7e28819e7da.
Report an issue: GitHub.