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;
    }
相关推荐
腾渊信息科技公司43 分钟前
Spring Boot对接MES实战:视觉检测数据自动同步方案
java·人工智能·spring boot·后端·计算机视觉·ai·软件需求
爱笑的源码基地2 小时前
高并发 Redis 缓存门诊HIS系统源码,含财务统计药房进销存
java·程序·门诊系统·诊所系统·云诊所源码
段一凡-华北理工大学2 小时前
向量数据库实战:选型、调优与落地~系列文章12:文本分块策略实战:chunk_size 怎么选?重叠多少?
开发语言·数据库·后端·oracle·rust·工业智能体·高炉智能化
Ljwuhe2 小时前
C++——多态
开发语言·c++
心平气和量大福大3 小时前
C#-WPF-Window主窗体
开发语言·c#·wpf
莫逸风4 小时前
【AgentScope 2.0】 0. 学习指南
java·llm·agent·agentscope
从零开始的代码生活_4 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法
云小逸4 小时前
【C++ 第七阶段:模板、泛型编程与工程综合详解】
开发语言·c++
z123456789865 小时前
2026最新两款AI编程工具深度对比实测
java·数据库·ai编程
GIS阵地5 小时前
QgsSingleBandPseudoColorRenderer 完整详解(QGIS 3.40.13 C++)
开发语言·前端·c++·qt·qgis