elunez/eladmin · warning · IllegalArgumentException

文件名不能为空

Error message

文件名不能为空

What it means

IllegalArgumentException thrown in S3StorageServiceImpl.upload when file.getOriginalFilename() is blank — the multipart part carried no filename (or whitespace). Unlike the BadRequestExceptions around it, this is an IAE, so it escapes as a different exception type and may map to a 500 rather than the usual 400 handler depending on the global handler's coverage.

Source

Thrown at eladmin-tools/src/main/java/me/zhengjie/service/impl/S3StorageServiceImpl.java:124

        }
    }

    @Override
    public S3Storage upload(MultipartFile file) {
        String bucketName = amzS3Config.getDefaultBucket();
        // 检查存储桶是否存在
        if (!bucketExists(bucketName)) {
            log.warn("存储桶 {} 不存在,尝试创建...", bucketName);
            if (createBucket(bucketName)){
                log.info("存储桶 {} 创建成功。", bucketName);
            } else {
                throw new BadRequestException("存储桶创建失败,请检查配置或权限。");
            }
        }
        // 获取文件名
        String originalName = file.getOriginalFilename();
        if (StringUtils.isBlank(originalName)) {
            throw new IllegalArgumentException("文件名不能为空");
        }
        // 生成存储路径和文件名
        String folder = DateUtil.format(new Date(), amzS3Config.getTimeformat());
        String fileName = IdUtil.simpleUUID() + "." + FileUtil.getExtensionName(originalName);
        String filePath = folder + "/" + fileName;
        // 构建上传请求
        PutObjectRequest putObjectRequest = PutObjectRequest.builder()
                .bucket(amzS3Config.getDefaultBucket())
                .key(filePath)
                .build();
        // 创建 S3Storage 实例
        S3Storage s3Storage = new S3Storage();
        try {
            // 上传文件到 S3
            s3Client.putObject(putObjectRequest, RequestBody.fromInputStream(file.getInputStream(), file.getSize()));
            // 设置 S3Storage 属性
            s3Storage.setFileMimeType(FileUtil.getMimeType(originalName));
            s3Storage.setFileName(originalName);

View on GitHub (pinned to 55fbf70595)

Solutions

  1. Ensure the client sends a proper filename: curl -F 'file=@photo.jpg', requests files={'file': ('photo.jpg', fh)}).
  2. On the server, reject earlier at the controller with a 400 @RequestParam validation or a blank-filename check.
  3. If streaming anonymous content is a real use case, generate a synthetic name instead of throwing.
  4. Add IllegalArgumentException to the global exception handler mapping so it returns a clean 400 message.

Example fix

// before
String originalName = file.getOriginalFilename();
if (StringUtils.isBlank(originalName)) {
    throw new IllegalArgumentException("文件名不能为空");
}

// after: consistent 400 semantics
String originalName = file.getOriginalFilename();
if (StringUtils.isBlank(originalName)) {
    throw new BadRequestException("上传文件名不能为空");
}
Defensive patterns

Strategy: validation

Validate before calling

// controller-level guard before the service call
String name = file.getOriginalFilename();
if (name == null || name.trim().isEmpty()) {
    return ResponseEntity.badRequest().body("上传必须携带文件名");
}

Try / catch

try { s3StorageService.upload(file); } catch (IllegalArgumentException e) { /* 400-class: fix the client to send filename= in the multipart part */ }

Prevention

When it happens

Trigger: Uploading a multipart request where the file part has no filename attribute (curl -F 'file=@-' style stream, some programmatic clients), or a filename of empty string / spaces; the later IdUtil.simpleUUID() + '.' + extension logic depends on a parseable filename.

Common situations: Custom scripts or API clients (requests/httpx) sending files without an explicit filename; proxy/gateway stripping the filename; frontend FormData appended with an empty filename; some mobile HTTP libraries defaulting to no filename.

Related errors


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