导入图片

 @PostMapping("/uploadExercise")
    public ResponseEntity<String> uploadExercise(@RequestParam MultipartFile file, @RequestParam("exercise_id") int exercise_id) throws IOException {

        String md5;

        try (InputStream inputStream = file.getInputStream()) {
            // 计算文件的 MD5 值
            md5 = SecureUtil.md5(inputStream);
        }

        // 生成唯一标识码
        String uuid = IdUtil.fastSimpleUUID();

        // 文件名
        String fileUUID = uuid + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename());

        // 将文件上传到磁盘
        File uploadExerciseFile = new File(fileUploadExercisePath + fileUUID);
        file.transferTo(uploadExerciseFile);

        // 存储在数据库中
        Exercise saveExerciseFile = new Exercise();
        saveExerciseFile.setExercise_id(exercise_id);
        saveExerciseFile.setExercise_url("http://localhost:8081/api/exercise-show/" + fileUUID);
        saveExerciseFile.setMd5(md5);

        // 判断运动是否被添加
        if (exerciseMapper.exerciseById(exercise_id) == null) {
            System.out.println("无数据");
        } else {
            int url1=exerciseMapper.updateExerciseUrl(saveExerciseFile);
            System.out.println("url=" + url1);
            if(url1 > 0){
                System.out.println("上传成功");
            }else{
                System.out.println("上传失败");
            }
        }
        return ResponseEntity.ok(saveExerciseFile.getExercise_url());
    }

 

posted @ 2024-04-23 14:10  XiangdxDu  阅读(8)  评论(0编辑  收藏  举报