elunez/eladmin · error · BadRequestException

文件超出规定大小:{maxSize}MB

Error message

文件超出规定大小:{maxSize}MB

What it means

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.

Source

Thrown at eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java:291

        String image = "bmp dib pcp dif wmf gif jpg tif eps psd cdr iff tga pcd mpt png jpeg";
        if (image.contains(type)) {
            return IMAGE;
        } else if (documents.contains(type)) {
            return TXT;
        } else if (music.contains(type)) {
            return MUSIC;
        } else if (video.contains(type)) {
            return VIDEO;
        } else {
            return OTHER;
        }
    }

    public static void checkSize(long maxSize, long size) {
        // 1M
        int len = 1024 * 1024;
        if (size > (maxSize * len)) {
            throw new BadRequestException("文件超出规定大小:" + maxSize + "MB");
        }
    }

    /**
     * 判断两个文件是否相同
     */
    public static boolean check(File file1, File file2) {
        String img1Md5 = getMd5(file1);
        String img2Md5 = getMd5(file2);
        if(img1Md5 != null){
            return img1Md5.equals(img2Md5);
        }
        return false;
    }

    /**
     * 判断两个文件是否相同
     */

View on GitHub (pinned to 55fbf70595)

Solutions

  1. Increase the configured max size in eladmin.yml (e.g. spring.servlet.max-size: 50) and restart, if large uploads are intended.
  2. On the client, check file.size against the limit before uploading and show a friendly message.
  3. Keep nginx client_max_body_size >= the application limit so requests are rejected consistently by the app, not truncated by the proxy.
  4. Compress or split the file (images: convert to WebP/JPEG with lower quality) to fit under the limit.

Example fix

// before: upload rejected for a 30MB file with maxSize=5
FileUtil.checkSize(maxSize, file.getSize());

// after: raise the limit in eladmin.yml
# spring:
#   servlet:
#     max-size: 50   # MB
// or guard client-side
// if (file.size > maxSizeMB * 1024 * 1024) alert('文件超出 ' + maxSizeMB + 'MB');
Defensive patterns

Strategy: validation

Validate before calling

// Client: validate before upload
const MAX_MB = 5; // must mirror spring.servlet.max-size
if (file.size > MAX_MB * 1024 * 1024) {
  alert(`文件超出规定大小:${MAX_MB}MB`);
  return;
}
upload(file);

Type guard

function isWithinSize(file, maxMb) {
  return typeof file.size === 'number' && file.size <= maxMb * 1024 * 1024;
}

Try / catch

try {
    storageService.add(name, file); // internally calls FileUtil.checkSize
} catch (BadRequestException e) {
    if (e.getMessage().startsWith("文件超出规定大小")) return showFriendlySizeError(file);
    throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Related errors


AI-assisted analysis of elunez/eladmin@55fbf70595 (2026-08-14). Data as JSON: /api/errors/17a1fb65df880fa2. Report an issue: GitHub.