{"record":{"id":"b52d676641b327ac","repo":"chinabugotech/hutool","slug":"not-a-regular-file","errorCode":null,"errorMessage":"Not a regular file!","messagePattern":"Not a regular file!","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/io/FileTypeUtil.java","lineNumber":210,"sourceCode":"\t}\n\n\t/**\n\t * 根据文件流的头部信息获得文件类型\n\t *\n\t * <pre>\n\t *     1、无法识别类型默认按照扩展名识别\n\t *     2、xls、doc、msi头信息无法区分，按照扩展名区分\n\t *     3、zip可能为jar、war头信息无法区分，按照扩展名区分\n\t * </pre>\n\t *\n\t * @param file    文件 {@link File}\n\t * @param isExact 是否精确匹配，如果为false，使用前64个bytes匹配，如果为true，使用前8192bytes匹配\n\t * @return 类型，文件的扩展名，未找到为{@code null}\n\t * @throws IORuntimeException 读取文件引起的异常\n\t */\n\tpublic static String getType(File file, boolean isExact) throws IORuntimeException {\n\t\tif (false == FileUtil.isFile(file)) {\n\t\t\tthrow new IllegalArgumentException(\"Not a regular file!\");\n\t\t}\n\t\tFileInputStream in = null;\n\t\ttry {\n\t\t\tin = IoUtil.toStream(file);\n\t\t\treturn getType(in, file.getName(), isExact);\n\t\t} finally {\n\t\t\tIoUtil.close(in);\n\t\t}\n\t}\n\n\t/**\n\t * 根据文件流的头部信息获得文件类型\n\t *\n\t * <pre>\n\t *     1、无法识别类型默认按照扩展名识别\n\t *     2、xls、doc、msi头信息无法区分，按照扩展名区分\n\t *     3、zip可能为jar、war头信息无法区分，按照扩展名区分\n\t * </pre>","sourceCodeStart":192,"sourceCodeEnd":228,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/io/FileTypeUtil.java#L192-L228","documentation":"FileTypeUtil.getType(File, boolean) detects a file's type from its magic header bytes. It rejects anything that is not a regular file — directories, missing paths, special device files — with IllegalArgumentException, because reading header bytes from them is meaningless.","triggerScenarios":"Passing a directory, a non-existent File, or a special/device File to FileTypeUtil.getType(File, boolean) or the single-arg getType(File).","commonSituations":"Iterating a directory's entries and calling getType on every child including subdirectories; resolving a path from user input that does not exist yet; pointing at a symlink whose target is a folder.","solutions":["Guard with FileUtil.isFile(file) (exists && not a directory) before calling getType.","Filter directory listings to regular files before type detection.","Confirm the file exists and is readable at the point of call."],"exampleFix":"// before\nString type = FileTypeUtil.getType(file); // -> IllegalArgumentException if dir/missing\n\n// after\nif (FileUtil.isFile(file)) {\n    String type = FileTypeUtil.getType(file);\n}","handlingStrategy":"validation","validationCode":"if (FileUtil.isFile(file)) {\n    String type = FileTypeUtil.getType(file, isExact);\n} else {\n    // skip or report: directory / missing / special file\n}","typeGuard":"static boolean isDetectableFile(File f) {\n    return f != null && f.isFile();\n}","tryCatchPattern":"try {\n    return FileTypeUtil.getType(file);\n} catch (IllegalArgumentException e) {\n    // not a regular file: handle missing/dir case\n}","preventionTips":["Always call FileUtil.isFile() before FileTypeUtil.getType().","When iterating a directory, skip children where isDirectory() is true.","Resolve symlinks and confirm the target is a file for type detection."],"tags":["io","file","validation"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}