{"record":{"id":"17a1fb65df880fa2","repo":"elunez/eladmin","slug":"maxsize-mb","errorCode":null,"errorMessage":"文件超出规定大小:{maxSize}MB","messagePattern":"文件超出规定大小:(.+?)MB","errorType":"exception","errorClass":"BadRequestException","httpStatus":400,"severity":"error","filePath":"eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java","lineNumber":291,"sourceCode":"        String image = \"bmp dib pcp dif wmf gif jpg tif eps psd cdr iff tga pcd mpt png jpeg\";\n        if (image.contains(type)) {\n            return IMAGE;\n        } else if (documents.contains(type)) {\n            return TXT;\n        } else if (music.contains(type)) {\n            return MUSIC;\n        } else if (video.contains(type)) {\n            return VIDEO;\n        } else {\n            return OTHER;\n        }\n    }\n\n    public static void checkSize(long maxSize, long size) {\n        // 1M\n        int len = 1024 * 1024;\n        if (size > (maxSize * len)) {\n            throw new BadRequestException(\"文件超出规定大小:\" + maxSize + \"MB\");\n        }\n    }\n\n    /**\n     * 判断两个文件是否相同\n     */\n    public static boolean check(File file1, File file2) {\n        String img1Md5 = getMd5(file1);\n        String img2Md5 = getMd5(file2);\n        if(img1Md5 != null){\n            return img1Md5.equals(img2Md5);\n        }\n        return false;\n    }\n\n    /**\n     * 判断两个文件是否相同\n     */","sourceCodeStart":273,"sourceCodeEnd":309,"githubUrl":"https://github.com/elunez/eladmin/blob/55fbf705956949697dbd68bf9003776609d3d029/eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java#L273-L309","documentation":"FileUtil.checkSize throws BadRequestException when an uploaded file's byte size exceeds maxSize MB (maxSize * 1024 * 1024). It is a pre-upload guard used by local-storage and upload services to reject oversized files before they are written. The message interpolates the configured maxSize so the client knows the ceiling.","triggerScenarios":"Uploading a file larger than the maxSize passed by the caller, e.g. LocalStorageService.add(...) calling FileUtil.checkSize(properties.getMaxSize(), file.getSize()) when spring.servlet.max-size (in MB) is smaller than the file; avatar or document uploads above the configured limit.","commonSituations":"Default maxSize (often 5MB in eladmin.yml) too small for user uploads; nginx client_max_body_size larger than the app limit so the request reaches Spring and then fails; clients not validating file size before POST; unit confusion (maxSize is in MB, size in bytes).","solutions":["Increase the configured max size in eladmin.yml (e.g. spring.servlet.max-size: 50) and restart, if large uploads are intended.","On the client, check file.size against the limit before uploading and show a friendly message.","Keep nginx client_max_body_size >= the application limit so requests are rejected consistently by the app, not truncated by the proxy.","Compress or split the file (images: convert to WebP/JPEG with lower quality) to fit under the limit."],"exampleFix":"// before: upload rejected for a 30MB file with maxSize=5\nFileUtil.checkSize(maxSize, file.getSize());\n\n// after: raise the limit in eladmin.yml\n# spring:\n#   servlet:\n#     max-size: 50   # MB\n// or guard client-side\n// if (file.size > maxSizeMB * 1024 * 1024) alert('文件超出 ' + maxSizeMB + 'MB');","handlingStrategy":"validation","validationCode":"// Client: validate before upload\nconst MAX_MB = 5; // must mirror spring.servlet.max-size\nif (file.size > MAX_MB * 1024 * 1024) {\n  alert(`文件超出规定大小:${MAX_MB}MB`);\n  return;\n}\nupload(file);","typeGuard":"function isWithinSize(file, maxMb) {\n  return typeof file.size === 'number' && file.size <= maxMb * 1024 * 1024;\n}","tryCatchPattern":"try {\n    storageService.add(name, file); // internally calls FileUtil.checkSize\n} catch (BadRequestException e) {\n    if (e.getMessage().startsWith(\"文件超出规定大小\")) return showFriendlySizeError(file);\n    throw e;\n}","preventionTips":["Surface the configured maxSize in the UI so users know the ceiling before choosing a file.","Compress images/videos client-side before upload.","Keep nginx client_max_body_size >= the Spring limit to avoid proxy-level truncation.","Never disable the check for 'big files' — raise the configured limit instead."],"tags":["file-upload","validation","eladmin","size-limit"],"backgroundTag":null,"analyzedSha":"55fbf705956949697dbd68bf9003776609d3d029","analyzedAt":"2026-08-14T11:56:12.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}