第四节 阿里云OSS上传工具类

亮子 2022-06-25 03:57:18 18138 0 0 0

1、添加依赖

        <!--阿里云OSS        -->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.10.2</version>
        </dependency>

2、工具类

package com.shenma2009.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * @author 军哥
 * @version 1.0
 * @description: 阿里云OSS管理工具类
 * @date 2022/6/25 11:03
 */

public class AliyunOssUtil {

    private static AliyunOssUtil _instance = null;
    private static OSS _ossClient = null;

    private static String endpoint = "http://oss-cn-beijing.aliyuncs.com";
    private static String accessKeyId = "LTAI4FtqoX9aXnwt5WRPVtJ9";
    private static String accessKeySecret = "BjxfqbWKvpusWM328UGzwYT8PQYlm5";
    private static String bucketName = "shenmazong";
    private static String domain = "https://images.shenmazong.com/";

    private String fileName;
    private String fileUrl;
    private Long fileSize;

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFileUrl() {
        return fileUrl;
    }

    public void setFileUrl(String url) {
        this.fileUrl = url;
    }

    public Long getFileSize() {
        return fileSize;
    }

    public void setFileSize(Long fileSize) {
        this.fileSize = fileSize;
    }

    private AliyunOssUtil() {

    }

    public static AliyunOssUtil OSS() {
        if(_instance == null) {
            _instance = new AliyunOssUtil();
            _ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            return _instance;
        }

        return _instance;
    }

    /**
     * @description 上传本地文件接口
     * @author 军哥
     * @date 2022/6/25 11:35
     * @version 1.0
     */
    public String upload(String fileName) {
        //--1 随机起名
        UUID uuid = UUID.randomUUID();
        String suffix = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
        String newName = "temp/"+uuid.toString() + suffix;

        String url = null;

        try {
            InputStream inputStream = new FileInputStream(fileName);
            _ossClient.putObject(bucketName, newName, inputStream);

            url = domain + newName;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return url;
    }

    public String upload(MultipartFile file) {
        //--1 判断文件是否有效
        if(file.isEmpty()){
            return null;
        }

        //--2 生成随机文件名
        String fileName = file.getOriginalFilename();
        UUID uuid = UUID.randomUUID();
        String suffix = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
        String newName = "temp/"+uuid.toString() + suffix;

        String url = null;
        try {
            PutObjectResult result = _ossClient.putObject(bucketName, newName, file.getInputStream());
            url = domain + newName;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        // 进行赋值
        this.setFileName(fileName);
        this.setFileUrl(url);
        this.setFileSize(file.getSize());

        return url;
    }

    public String getEndpoint() {
        return endpoint;
    }

    public String getAccessKeyId() {
        return accessKeyId;
    }

    public String getAccessKeySecret() {
        return accessKeySecret;
    }

    public String getBucketName() {
        return bucketName;
    }

    public void setEndpoint(String endpoint) {
        endpoint = endpoint;
    }

    public void setAccessKeyId(String accessKeyId) {
        accessKeyId = accessKeyId;
    }

    public void setAccessKeySecret(String accessKeySecret) {
        accessKeySecret = accessKeySecret;
    }

    public void setBucketName(String bucketName) {
        bucketName = bucketName;
    }

    public static String getDomain() {
        return domain;
    }

    public static void setDomain(String domain) {
        AliyunOssUtil.domain = domain;
    }
}

3、使用工具类

    @PostMapping(value = "/upload")
    public ResultResponse uploadFile(@RequestParam("file") MultipartFile file) {
        String url = AliyunOssUtil.OSS().upload(file);

        return ResultResponse.SUCCESS(url);
    }

4、测试运行

图片alt

图片alt