依赖

<!--阿里云OSS-->
<dependency&gt;
    <groupId&gt;com.aliyun.oss</groupId&gt;
    <artifactId&gt;aliyun-sdk-oss</artifactId&gt;
    <version&gt;3.15.1</version&gt;
</dependency&gt;

 AliOssUtils工具

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @Description: 实现上传图片阿里云OSS
 * @Author: 翰戈.summer
 * @Date: 2023/11/16
 * @Param:
 * @Return:
 */
@Component
@RequiredArgsConstructor
public class AliOssUtils {

    private final AliOssProperties aliOSSProperties;

    /**
     * @Description: 上传图片,并获取图片访问路径
     * @Author: 翰戈.summer
     * @Date: 2023/11/16
     * @Param: MultipartFile
     * @Return: String
     */
    public String upload(MultipartFile file) throws IOException {
        //获取配置信息
        String endpoint = aliOSSProperties.getEndpoint();
        String accessKeyId = aliOSSProperties.getAccessKeyId();
        String accessKeySecret = aliOSSProperties.getAccessKeySecret();
        String bucketName = aliOSSProperties.getBucketName();

        //获取上传图片的输入流
        InputStream inputStream = file.getInputStream();

        //避免图片覆盖
        String originalFilename = file.getOriginalFilename();
        String fileName = null;
        if (originalFilename != null) {
            fileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
        }

        //上传图片到OSS
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, fileName, inputStream);

        //图片访问路径
        String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
        //关闭ossClient
        ossClient.shutdown();
        return url; //返回图片访问路径
    }
}

AliOssProperties属性

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @Description: 阿里云OSS配置属性类
 * @Author: 翰戈.summer
 * @Date: 2023/11/16
 * @Param:
 * @Return:
 */
@Data
@Component
@ConfigurationProperties(prefix = "upload.alioss")
public class AliOssProperties {
    //区域地址
    private String endpoint;

    //密钥id
    private String accessKeyId;

    //密钥密码
    private String accessKeySecret;

    //bucket名称
    private String bucketName;
}

 yml配置文件

upload:
  alioss:
    endpoint: #区域地址
    access-key-id: #密钥id
    access-key-secret: #密钥密码
    bucket-name: #bucket名称

UploadController

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description: 图片上传相关接口
 * @Author: 翰戈.summer
 * @Date: 2023/11/28
 * @Param:
 * @Return:
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/upload")
public class UploadController {

    private final AliOssUtils aliOssUtils;

    @PostMapping
    public String uploadImage(MultipartFile multipartFile) {
        String url;
        try {
            url = aliOssUtils.upload(multipartFile);
        } catch (Exception ex) {
            throw new RuntimeException("图片上传失败!");
        }
        return url;
    }
}

原文地址:https://blog.csdn.net/qq_74312711/article/details/134665447

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_17865.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注