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


    方案一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");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    controller类

    /**
     * 新增或修改
     */
    @PostMapping("/saveData")
    public Result<Object> saveData(@Validated MetaDataDTO metaDataDTO) {
        return dataService.saveData(metaDataDTO);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    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);
    	    }
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

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

    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();
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
  • 相关阅读:
    【Leetcode】 738. 单调递增的数字
    Postman接口测试之get请求
    Java:Java与Node.js对比学习必备
    【Zookeeper客户端常用的命令&&Zookeeper的核心功能之事件监听】
    数据库管理-第110期 Oracle Exadata 01(20231016)
    刷题1:数组篇
    33. 搜索旋转排序数组
    轻松导航:教你在Excel中添加超链接功能
    【调制解调】SSB 单边带调幅
    ESP32上实现面向对象的C++ OOP——面向对象的点灯
  • 原文地址:https://blog.csdn.net/weixin_43732943/article/details/133318695