{"record":{"id":"9f2816a0e7e6cb1c","repo":"chinabugotech/hutool","slug":"error-9f2816","errorCode":null,"errorMessage":"文件类型{}不支持","messagePattern":"文件类型(.+?)不支持","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/img/BackgroundRemoval.java","lineNumber":350,"sourceCode":"\t// -------------------------------------------------------------------------- private\n\n\t/**\n\t * 文件类型验证\n\t * 根据给定文件类型数据，验证给定文件类型.\n\t *\n\t * @param input      需要进行验证的文件\n\t * @param imagesType 文件包含的类型数组\n\t * @return 返回布尔值 false:给定文件的文件类型在文件数组中  true:给定文件的文件类型 不在给定数组中。\n\t */\n\tprivate static boolean fileTypeValidation(File input, String[] imagesType) {\n\t\tif (!input.exists()) {\n\t\t\tthrow new IllegalArgumentException(\"给定文件为空\");\n\t\t}\n\t\t// 获取图片类型\n\t\tString type = FileTypeUtil.getType(input);\n\t\t// 类型对比\n\t\tif (!ArrayUtil.contains(imagesType, type)) {\n\t\t\tthrow new IllegalArgumentException(StrUtil.format(\"文件类型{}不支持\", type));\n\t\t}\n\t\treturn false;\n\t}\n}\n","sourceCodeStart":332,"sourceCodeEnd":355,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/img/BackgroundRemoval.java#L332-L355","documentation":"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.","triggerScenarios":"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.","commonSituations":"Supported-format allowlists narrower than the user's input; format detection mismatch; converting images but feeding the wrong type to removal.","solutions":["Convert the image to a supported format (PNG/JPG) before calling removal.","Expand the allowed imagesType array to include the detected format if the removal logic actually supports it.","Pre-validate the type with FileTypeUtil.getType and surface a clear error to the user."],"exampleFix":"// before\nBackgroundRemoval.removeBg(bmpFile, ...);\n// after - convert to PNG first\nFile png = ImgUtil.convert(bmpFile, \"png\");\nBackgroundRemoval.removeBg(png, ...);","handlingStrategy":"validation","validationCode":"void requireSupportedType(File f, String[] allowed){ String t = FileTypeUtil.getType(f); if(!ArrayUtil.contains(allowed, t)) throw new IllegalArgumentException(\"unsupported type \"+t); }","typeGuard":"boolean isSupportedType(File f, String[] allowed){ return f!=null && f.exists() && ArrayUtil.contains(allowed, FileTypeUtil.getType(f)); }","tryCatchPattern":"try { BackgroundRemoval.removeBg(f, ...); }\ncatch (IllegalArgumentException e){ if(e.getMessage().contains(\"不支持\")) { /* convert to PNG */ } else throw e; }","preventionTips":["Normalize inputs to PNG/JPG before background removal.","Keep an explicit supported-format list in config.","Pre-check with FileTypeUtil.getType."],"tags":["image","file-type","argument","background-removal"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}