文件MultipartFile上传同时,接收复杂参数

  • Post author:
  • Post category:其他




方案一MultipartFile和dto分开

@PostMapping("/uploadData")
public Result<Object> uploadData(
     @RequestParam("file") MultipartFile file,
     @RequestPart() DataDTO dataDTO) {
    // 处理文件上传逻辑,如保存文件到本地或云存储

    // 处理接收到的 DTO 对象逻辑,并进行业务处理

    return ResponseEntity.ok("File uploaded and DTO object received successfully");
}


SpringBoot文件上传同时,接收复杂参数



方案二MultipartFile放入dto中

dto类

@Data
public class MetaDataDTO {
    /**
     * 主键id
     */
    private Integer id;
    /**
     * 关联数据类型id
     */
    protected Integer metadataTypeId;
    /**
     * 数据名称
     */
    private String dataName;
    /**
     * 描述
     */
    private String description;
    /**
     * 共享级别
     */
    private String sharingLevel;
    /**
     * 数据图片
     */
    MultipartFile dataPic;
}

controller类

/**
 * 新增或修改
 */
@PostMapping("/saveData")
public Result<Object> saveData(@Validated MetaDataDTO metaDataDTO) {
    return dataService.saveData(metaDataDTO);
}

service实现类

@Service
public class DataServiceImpl implements DataService {
	@Override
	public Result<Object> saveData(MetaDataDTO metaDataDTO) {
	   // * * * * * * * * * * * * * * * * * * * * * * * * * *//
	    //                 上传图片                            //
	    // * * * * * * * * * * * * * * * * * * * * * * * * * *//
	    //必须上传图片
	    if (metaDataDTO.getDataPic() == null) {
	        return Result.error(CodeMsg.META_DATA_PIC_NOT_EXIST);
	    }
	    // 检查文件大小
	    if (metaDataDTO.getDataPic().getSize() > 1 * 1024 * 1024) { // 1MB
	        return Result.error(CodeMsg.FILE_SIZE_TOO_LARGE);
	    }
	    // 检查文件类型
	    if (!ImageUtil.isImageFile(metaDataDTO.getDataPic())) {
	        return Result.error(CodeMsg.FILE_FORMAT_NOT_PIC);
	    }
	}
}

判断文件是否是图片工具类

public class ImageUtil {
	/**
     * 判断文件是否为图片
     */
    public static boolean isImageFile(MultipartFile file) {
        if (file != null && !file.isEmpty()) {
            Tika tika = new Tika();
            try {
                String fileType = tika.detect(file.getInputStream());
                if (fileType.startsWith("image/")) {
                    // 是图片类型
                    return true;
                } else {
                    // 非图片类型
                    return false;
                }
            } catch (IOException e) {
                // 处理异常情况
            }
        } else {
            // 未上传文件
            return false;
        }
        return false;
    }
	/**
	 * 图片保存至服务器指定目录,返回访问路径
	 */
    public static String savePhotoAndGetPath(String basePath, String detailPath, MultipartFile file) {
        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            String filename = file.getOriginalFilename();
            //"/data/static/dataSharingStatic/idCard/"
            String directoryPath = basePath + detailPath;
            File dir = new File(directoryPath);
            if (!dir.exists()) {
                boolean mkdirs = dir.mkdirs();
            }
            inputStream = file.getInputStream();
            String filePath = directoryPath + filename;
            fileOutputStream = new FileOutputStream(filePath);
            IOUtils.copy(inputStream, fileOutputStream);
            fileOutputStream.flush();
            return detailPath + filename;
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}



版权声明:本文为weixin_43732943原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。