{"record":{"id":"4547d6df86978ece","repo":"apache/shenyu","slug":"file-exists-but-is-a-directory","errorCode":null,"errorMessage":"File '' exists but is a directory","messagePattern":"File '' exists but is a directory","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/HttpUtils.java","lineNumber":738,"sourceCode":"            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];\n\n            while (EOF != (n = input.read(buffer))) {\n                output.write(buffer, 0, n);\n            }\n            return output.toByteArray();\n        }\n\n        /**\n         * file to bytes.\n         *\n         * @param file file\n         * @return byte\n         * @throws IOException IOException\n         */\n        public static byte[] toBytes(final File file) throws IOException {\n            if (file.exists()) {\n                if (file.isDirectory()) {\n                    throw new IOException(\"File '\" + file + \"' exists but is a directory\");\n                }\n                if (!file.canRead()) {\n                    throw new IOException(\"File '\" + file + \"' cannot be read\");\n                }\n            } else {\n                throw new FileNotFoundException(\"File '\" + file + \"' does not exist\");\n            }\n            InputStream input = null;\n            try {\n                input = Files.newInputStream(file.toPath());\n                return toBytes(input);\n            } finally {\n                try {\n                    if (Objects.nonNull(input)) {\n                        input.close();\n                    }\n                } catch (IOException ioe) {\n                    LOG.error(\"toBytes error\", ioe);","sourceCodeStart":720,"sourceCodeEnd":756,"githubUrl":"https://github.com/apache/shenyu/blob/567142e07261b3e615ae8850b30f4421f455cc5d/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/HttpUtils.java#L720-L756","documentation":"HttpUtils.toBytes(File) loads a file's full contents into a byte array. Before opening the file it rejects paths that exist but are directories, because a directory cannot be read as an input stream. The error message includes the offending path.","triggerScenarios":"Calling HttpUtils.toBytes with a File object whose path points at an existing directory rather than a regular file — typically a misconfigured upload path or a placeholder directory where a file was expected.","commonSituations":"Uploading a secret/certificate in shenyu-admin where the configured file path points to a directory (e.g. '/etc/ssl/certs' instead of a specific .pem), or a config value set to a folder name by mistake.","solutions":["Point the file path configuration at the actual regular file, not its parent directory.","Check with Files.isRegularFile(path) before calling toBytes.","If the path may be a directory, resolve to the intended child file explicitly.","Handle IOException and report a clear message distinguishing 'is a directory' from 'not found'."],"exampleFix":"// before\nbyte[] bytes = HttpUtils.toBytes(new File(\"/etc/shenyu/certs\"));\n// after\nbyte[] bytes = HttpUtils.toBytes(new File(\"/etc/shenyu/certs/server.pem\"));","handlingStrategy":"validation","validationCode":"Path path = Paths.get(configuredPath);\nif (!Files.isRegularFile(path)) {\n    if (Files.isDirectory(path)) throw new IllegalArgumentException(\"Expected a file, got directory: \" + path);\n    throw new IllegalArgumentException(\"File does not exist: \" + path);\n}","typeGuard":null,"tryCatchPattern":"try {\n    byte[] bytes = HttpUtils.toBytes(file);\n} catch (IOException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"is a directory\")) {\n        throw new IllegalArgumentException(\"Configured path is a directory, expected a file: \" + file, e);\n    }\n    throw e;\n}","preventionTips":["Point config values at exact file paths, never directories.","Use Files.isRegularFile() as a pre-check before reading.","Resolve symlinks (toRealPath()) so a symlinked directory does not sneak through.","Fail fast at startup if a configured file path is a directory."],"tags":["io","file","directory"],"backgroundTag":"path-is-not-a-directory","analyzedSha":"567142e07261b3e615ae8850b30f4421f455cc5d","analyzedAt":"2026-09-12T10:08:21.293Z","contentChangedAt":"2026-09-12T10:08:21.293Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}