{"record":{"id":"29f0f3f3aabed386","repo":"jeecgboot/JeecgBoot","slug":"the-file-name-can-not-null","errorCode":null,"errorMessage":"The file name can not null","messagePattern":"The file name can not null","errorType":"validation","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/FileDownloadUtils.java","lineNumber":50,"sourceCode":"public class FileDownloadUtils {\n\n    /**\n     * 单文件下载\n     *\n     * @param response\n     * @param storePath 下载文件储存地址\n     * @param fileName  文件名称\n     * @author: chenrui\n     * @date: 2019/5/24 17:10\n     */\n    public static void downloadFile(HttpServletResponse response, String storePath, String fileName) {\n        response.setCharacterEncoding(\"UTF-8\");\n        File file = new File(storePath);\n        if (!file.exists()) {\n            throw new NullPointerException(\"Specified file not found\");\n        }\n        if (fileName == null || fileName.isEmpty()) {\n            throw new NullPointerException(\"The file name can not null\");\n        }\n        // 配置文件下载\n        response.setHeader(\"content-type\", \"application/octet-stream\");\n        response.setContentType(\"application/octet-stream\");\n        // 下载文件能正常显示中文\n        try {\n            response.setHeader(\"Content-Disposition\", \"attachment;filename=\" + URLEncoder.encode(fileName, \"UTF-8\"));\n            response.setHeader(\"Access-Control-Expose-Headers\", \"Content-Disposition\");\n        } catch (UnsupportedEncodingException e) {\n            log.error(e.getMessage(), e);\n        }\n        // 实现文件下载\n        byte[] buffer = new byte[1024];\n        try (FileInputStream fis = new FileInputStream(file);\n             BufferedInputStream bis = new BufferedInputStream(fis);) {\n            OutputStream os = response.getOutputStream();\n            int i = bis.read(buffer);\n            while (i != -1) {","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/jeecgboot/JeecgBoot/blob/96fb33f5ec68516da0b0147da06b2eb0419e063a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/FileDownloadUtils.java#L32-L68","documentation":"In downloadFile, after the file-exists check, if the fileName parameter is null or empty the method throws NullPointerException('The file name can not null'). The fileName is used to set the Content-Disposition header (URL-encoded). A null/empty name would break the header, so the guard rejects it early. Like error 18 it uses NPE for a validation condition rather than IllegalArgumentException.","triggerScenarios":"A download request where the display fileName is not supplied — e.g. the caller passes only storePath and omits fileName, or the DB record's fileName field is null/blank, or a frontend download link omits the name query param.","commonSituations":"Download endpoint called with only a path param and no name param; DB record missing the original filename column value; a refactor that drops the fileName argument from the call site.","solutions":["Always pass a non-empty fileName to downloadFile; default to the file's actual name if unknown.","In the controller, validate fileName is non-null/non-empty and derive it from the File if missing.","Ensure the DB record storing the display name is populated on upload."],"exampleFix":"// before — fileName omitted/nullable\ndownloadFile(response, storePath, record.getFileName()); // null -> throws\n\n// after — default to the physical file name\nString name = StringUtils.hasText(record.getFileName())\n  ? record.getFileName()\n  : new File(storePath).getName();\ndownloadFile(response, storePath, name);","handlingStrategy":"validation","validationCode":"// Guarantee a non-empty fileName before downloading\nString name = (fileName == null || fileName.isBlank())\n  ? new File(storePath).getName()\n  : fileName;\nFileDownloadUtils.downloadFile(response, storePath, name);","typeGuard":"public static boolean hasValidFileName(String fileName) {\n  return fileName != null && !fileName.trim().isEmpty();\n}","tryCatchPattern":"try {\n  FileDownloadUtils.downloadFile(response, storePath, fileName);\n} catch (NullPointerException e) {\n  if (e.getMessage().contains(\"file name\")) {\n    response.sendError(400, \"File name required\");\n  }\n}","preventionTips":["Always pass a non-empty fileName; default to the physical file name.","Validate fileName in the controller and derive it if missing.","Populate the display-name DB column on upload."],"tags":["backend","file-download","validation","io","jeecg"],"backgroundTag":null,"analyzedSha":"96fb33f5ec68516da0b0147da06b2eb0419e063a","analyzedAt":"2026-08-14T00:04:16.786Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}