Springboot整合阿里云OSS存储,实现图片上传,保存数据库,删除图片

1、首先到阿里云oss申请

阿里云OSS对象存储服务购买与基础使用指南

2、引入阿里云OSS依赖包

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

3、配置aliyun.properties

这里根据自己配置的阿里云服务器的信息,进行填写。

aliyun.access-id=
aliyun.access-key=
aliyun.bucket=
aliyun.endpoint=
aliyun.dir=

4、编写oss工具类

OSSProperties.java

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("classpath:/aliyun.properties")
public class OSSProperties implements InitializingBean {
    
    

    @Value("${aliyun.endpoint}")
    private String aliyun_endpoint;

    @Value("${aliyun.access-id}")
    private String aliyun_access_id;

    @Value("${aliyun.access-key}")
    private String aliyun_access_key;

    @Value("${aliyun.bucket}")
    private String aliyun_bucket;

    @Value("${aliyun.dir}")
    private String aliyun_dir;

    // 声明静态变量
    public static String ALIYUN_ENDPOINT;

    public static String ALIYUN_ACCESS_ID;

    public static String ALIYUN_ACCESS_KEY;

    public static String ALIYUN_BUCKET;

    public static String ALIYUN_DIR;

    @Override
    public void afterPropertiesSet() {
    
    
        ALIYUN_ENDPOINT = aliyun_endpoint;
        ALIYUN_ACCESS_ID = aliyun_access_id;
        ALIYUN_ACCESS_KEY = aliyun_access_key;
        ALIYUN_BUCKET = aliyun_bucket;
        ALIYUN_DIR = aliyun_dir;
    }

}

OSSUtil

这里边完成了删除图片、上传图片等功能。
在上传图片的地方,由于我想指定我上传文件的名称,所以移除了随机生成字符串的代码,如果有需要可以改过来。

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URL;
import java.util.Date;
import java.util.Random;

public class OSSUtil {
    
    

    /**
     * log日志
     */
    public static final Logger logger = LoggerFactory.getLogger(OSSUtil.class);

    private String endpoint = OSSProperties.ALIYUN_ENDPOINT;
    private String accessKeyId = OSSProperties.ALIYUN_ACCESS_ID;
    private String accessKeySecret = OSSProperties.ALIYUN_ACCESS_KEY;
    private String bucketName = OSSProperties.ALIYUN_BUCKET;
    private String FOLDER = OSSProperties.ALIYUN_DIR;

    /**
     * 删除单个图片
     *
     * @param url
     */
    public void deleteImg(String url) {
    
    
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        url = FOLDER + url;
        ossClient.deleteObject(bucketName, url);
        System.out.println("删除 " + url + " 成功");
        ossClient.shutdown();
    }

    /**
     * 上传图片
     *
     * @param url
     * @throws
     */
    public void uploadImg2Oss(String url) throws IOException {
    
    
        File fileOnServer = new File(url);
        FileInputStream fin;
        try {
    
    
            fin = new FileInputStream(fileOnServer);
            String[] split = url.split("/");
            this.uploadFile2OSS(fin, split[split.length - 1]);
        } catch (FileNotFoundException e) {
    
    
            throw new IOException("图片上传失败");
        }
    }

    public String uploadImg2Oss(MultipartFile file, String fileName) throws IOException {
    
    
       /* if (file.getSize() > 10 * 1024 * 1024) {
            throw new IOException("上传图片大小不能超过10M!");
        }*/
//        String originalFilename = file.getOriginalFilename();
//        String substring = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
//        Random random = new Random();
//        String name = random.nextInt(10000) + System.currentTimeMillis() + substring;
        try {
    
    
            InputStream inputStream = file.getInputStream();
            this.uploadFile2OSS(inputStream, fileName);
            return fileName;
        } catch (Exception e) {
    
    
            throw new IOException("图片上传失败");
        }
    }

    /**
     * 获得图片路径
     *
     * @param fileUrl
     * @return
     */
    public String getImgUrl(String fileUrl) {
    
    
        System.out.println(fileUrl);
        if (!StringUtils.isEmpty(fileUrl)) {
    
    
            String[] split = fileUrl.split("/");
            return this.getUrl(this.FOLDER + split[split.length - 1]);
        }
        return "";
    }

    /**
     * 上传到OSS服务器 如果同名文件会覆盖服务器上的
     *
     * @param instream 文件流
     * @param fileName 文件名称 包括后缀名
     * @return 出错返回"" ,唯一MD5数字签名
     */
    public String uploadFile2OSS(InputStream instream, String fileName) {
    
    

        String ret = "";
        try {
    
    
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            // 创建上传Object的Metadata
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentLength(instream.available());
            objectMetadata.setCacheControl("no-cache");
            objectMetadata.setHeader("Pragma", "no-cache");
            objectMetadata.setContentType(getcontentType(fileName.substring(fileName.lastIndexOf("."))));
            objectMetadata.setContentDisposition("inline;filename=" + fileName);
            // 上传文件
            PutObjectResult putResult = ossClient.putObject(bucketName, FOLDER + fileName, instream, objectMetadata);
            ret = putResult.getETag();
        } catch (IOException e) {
    
    
//            logger.error(e.getMessage(), e);
            System.out.println(e.getMessage());
        } finally {
    
    
            try {
    
    
                if (instream != null) {
    
    
                    instream.close();
                }
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }
        return ret;
    }

    /**
     * Description: 判断OSS服务文件上传时文件的contentType
     */
    public static String getcontentType(String filenameExtension) {
    
    
        if (filenameExtension.equalsIgnoreCase("bmp")) {
    
    
            return "image/bmp";
        }
        if (filenameExtension.equalsIgnoreCase("gif")) {
    
    
            return "image/gif";
        }
        if (filenameExtension.equalsIgnoreCase("jpeg") || filenameExtension.equalsIgnoreCase("jpg")
                || filenameExtension.equalsIgnoreCase("png")) {
    
    
            return "image/jpeg";
        }
        if (filenameExtension.equalsIgnoreCase("html")) {
    
    
            return "text/html";
        }
        if (filenameExtension.equalsIgnoreCase("txt")) {
    
    
            return "text/plain";
        }
        if (filenameExtension.equalsIgnoreCase("vsd")) {
    
    
            return "application/vnd.visio";
        }
        if (filenameExtension.equalsIgnoreCase("pptx") || filenameExtension.equalsIgnoreCase("ppt")) {
    
    
            return "application/vnd.ms-powerpoint";
        }
        if (filenameExtension.equalsIgnoreCase("docx") || filenameExtension.equalsIgnoreCase("doc")) {
    
    
            return "application/msword";
        }
        if (filenameExtension.equalsIgnoreCase("xml")) {
    
    
            return "text/xml";
        }
        return "image/jpeg";
    }

    /**
     * 获得url链接
     *
     * @param key
     * @return
     */
    public String getUrl(String key) {
    
    
        // 设置URL过期时间为10年 3600l* 1000*24*365*10
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10);
        // 生成URL
        URL url = ossClient.generatePresignedUrl(bucketName, key, expiration);
        /*        url = "https://" + bucketName + ".oss-cn-beijing.aliyuncs.com/" + bucketName+"/"+ key;*/
        if (url != null) {
    
    

            String host = "https://" + url.getHost() + url.getPath();

            return host;
        }
        return "";
    }


}

猜你喜欢

转载自blog.csdn.net/qq_43656233/article/details/109712198