java 阿里云上传照片

获取对象

复制代码
   @Resource
    private ALiYunConfig aLiYunConfig;

代码配置类

复制代码
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;


}

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


代码controller层

复制代码
    /**
     * 用户上传图片接口
     *
     * @param file
     */
    @ApiOperation("用户上传图片接口")
    @PostMapping("uploadPicture")
    @ExcludeLogin
    public String uploadPicture(@RequestParam(name = "file") MultipartFile file) {
        return iPlatformPictureService.uploadPicture(file);
    }

代码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<String> 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;
    }
相关推荐
草履虫建模20 小时前
力扣算法 1768. 交替合并字符串
java·开发语言·算法·leetcode·职场和发展·idea·基础
naruto_lnq1 天前
分布式系统安全通信
开发语言·c++·算法
qq_297574671 天前
【实战教程】SpringBoot 实现多文件批量下载并打包为 ZIP 压缩包
java·spring boot·后端
老毛肚1 天前
MyBatis插件原理及Spring集成
java·spring·mybatis
学嵌入式的小杨同学1 天前
【Linux 封神之路】信号编程全解析:从信号基础到 MP3 播放器实战(含核心 API 与避坑指南)
java·linux·c语言·开发语言·vscode·vim·ux
lang201509281 天前
JSR-340 :高性能Web开发新标准
java·前端·servlet
Re.不晚1 天前
Java入门17——异常
java·开发语言
缘空如是1 天前
基础工具包之JSON 工厂类
java·json·json切换
精彩极了吧1 天前
C语言基本语法-自定义类型:结构体&联合体&枚举
c语言·开发语言·枚举·结构体·内存对齐·位段·联合
追逐梦想的张小年1 天前
JUC编程04
java·idea