• java 阿里云上传照片


    获取对象

       @Resource
        private ALiYunConfig aLiYunConfig;
    
    • 1
    • 2

    代码配置类

    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    /**
     * 描述:
     *
     * @author zhaofeng
     * @date 2023-09-05
     */
    @Data
    @ConfigurationProperties(prefix = "aliyun")
    @Component
    public class ALiYunConfig {
        /**
         * 阿里云keyId
         */
        private String accessKey ;
        /**
         * 阿里云secret
         */
        private String accessSecret;
        /**
         * 阿里云secret
         */
        private String endpoint;
        /**
         * 阿里云oss上传节点
         */
        private String ossEndpoint;
        /**
         * 阿里云oss Bucket名称
         */
        private String ossBucketName;
    
    
    }
    
    
    • 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

    yml配置 注意这些参数都是事先配置好的(也就是注册阿里云购买过的获取的参数)

    在这里插入图片描述
    代码controller层

        /**
         * 用户上传图片接口
         *
         * @param file
         */
        @ApiOperation("用户上传图片接口")
        @PostMapping("uploadPicture")
        @ExcludeLogin
        public String uploadPicture(@RequestParam(name = "file") MultipartFile file) {
            return iPlatformPictureService.uploadPicture(file);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    代码service层

       @Resource
        private ALiYunConfig aLiYunConfig;
    
        /**
         * 图片上传到阿里云
         *
         * @param file
         * @return
         */
        @Override
        public String uploadPicture(MultipartFile file) {
            //源文件名称
            String fileName = file.getOriginalFilename();
            //获取后缀
            String suffixName = fileName.contains(".") ? fileName.substring(fileName.lastIndexOf(".") + 1) : null;
            //校验图片格式
            if (StringUtils.isBlank(suffixName)) {
                throw new BusinessException(PlatformResultCode.PICTURE_ERROR);
            }
            List suffix = Arrays.asList("PNG", "JPG", "JPEG");
            if (!suffix.contains(suffixName.toUpperCase())) {
                throw new BusinessException(PlatformResultCode.PICTURE_ERROR);
            }
            //校验图片大小  校验图片大小
            long size = file.getSize();
            int sizeKb = (int) ((size / 1024) + 1);
            //从缓存中获取图片大小的配置
            int configValue = platformConfigExport.getConfigInteger(PlatformConfigEnum.PICTURE_SIZE);
            if (sizeKb > configValue) {
                throw new BusinessException(PlatformResultCode.PICTURE_MAX, configValue / 1024);
            }
            //拼接文件夹以及文件名称
            String today = DateUtils.today();
            String name = today + "/" + UUIDUtils.getUUID() + "." + suffixName;
            // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
            String endpoint = aLiYunConfig.getOssEndpoint();
            // 填写Bucket名称,例如examplebucket。
            String bucketName = aLiYunConfig.getOssBucketName();
            OSS ossClient = new OSSClientBuilder().build(endpoint, aLiYunConfig.getAccessKey(), aLiYunConfig.getAccessSecret());
            try {
                // 创建PutObjectRequest对象。
                PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, name, file.getInputStream());
                // 上传文件
                PutObjectResult result = ossClient.putObject(putObjectRequest);
            } catch (Exception e) {
                log.error("PlatformPictureServiceImpl.uploadPicture; 用户上传图片失败,大小:{}Kb,", sizeKb, e);
                throw new BusinessException(PlatformResultCode.UPLOAD_PICTURE_ERROR);
            } finally {
                if (ossClient != null) {
                    ossClient.shutdown();
                }
            }
            //返回路径,该路径在浏览器访问可以下载
            return "https://" + aLiYunConfig.getOssBucketName() + "." + endpoint + File.separator + name;
        }
    
    • 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
  • 相关阅读:
    XTU-OJ 1146-矩阵乘法
    问题与分类
    通过stream流实现分页、模糊搜索、按列过滤功能
    Redis中zSet类型的操作
    Python模拟试卷2023(1)
    R之广义线性模型
    矩阵分析与应用+张贤达
    [Maven高级]->近万字文章带你深入了解Maven
    CSAPP学习导航2015
    隆云通土壤二氧化碳传感器
  • 原文地址:https://blog.csdn.net/taiguolaotu/article/details/133135625