controller层如何接收文件格式?

/**
 * 图片上传接口
 */
@Operation(summary = "图片上传接口")
@PostMapping("/image")
RestResp<String> uploadImage(
    @Parameter(description = "上传文件") @RequestParam("file") MultipartFile file) {
    return resourceService.uploadImage(file);
}

service进行处理

@SneakyThrows
@Override
public RestResp<String> uploadImage(MultipartFile file) {
    LocalDateTime now = LocalDateTime.now();
    String savePath =
        SystemConfigConsts.IMAGE_UPLOAD_DIRECTORY
            + now.format(DateTimeFormatter.ofPattern("yyyy")) + File.separator
            + now.format(DateTimeFormatter.ofPattern("MM")) + File.separator
            + now.format(DateTimeFormatter.ofPattern("dd"));
    String oriName = file.getOriginalFilename();
    assert oriName != null;
    String saveFileName = IdWorker.get32UUID() + oriName.substring(oriName.lastIndexOf("."));
    File saveFile = new File(fileUploadPath + savePath, saveFileName);
    if (!saveFile.getParentFile().exists()) {
        boolean isSuccess = saveFile.getParentFile().mkdirs();
        if (!isSuccess) {
            throw new BusinessException(ErrorCodeEnum.USER_UPLOAD_FILE_ERROR);
        }
 
    file.transferTo(saveFile);
    if (Objects.isNull(ImageIO.read(saveFile))) {
        // 上传的文件不是图片
        Files.delete(saveFile.toPath());
        throw new BusinessException(ErrorCodeEnum.USER_UPLOAD_FILE_TYPE_NOT_MATCH);
    }
    return RestResp.ok(savePath + File.separator + saveFileName);
}

说明: 关于@SneakyThrows 注解 Lombok 注解 @SneakyThrows 的作用

配置文件配置上传路径

image.png

image.png

我们看到配置文件是yml结尾的,下文介绍springboot中加载顺序: SpringBoot 配置文件加载顺序

附加SpringBoot项目多个配置文件介绍启动项目时候怎么选择指定配置文件