OSS封装成Util类

封了当好像又没封-。-

OSSUtils 工具类

package com.itcast.common.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import java.io.FileInputStream;
import java.io.*;

public class OSSUtils {
    private static String endpoint  = "oss-cn-shenzhen.aliyuncs.com";
    private static String accessKeyId  = "xxxxxx";
    private static String accessKeySecret  = "xxxxxx";
    private static String bucketName = "xxxxxx";
    private static String ossPath = "health/";
    private static OSS ossClient;

    /**
     *@Title:create
     *@Description 获取ossClient客户端
     */
    public static void create(){
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }

    /**
     *@Title:close
     *@Description 关闭ossClient客户端
     */
    public static void close(){
        if (ossClient != null) {
            ossClient.shutdown();
        }
    }

    /**
     *@Title:uploadByInputStream
     *@Description 本地路径上传
     *@param localpath 本地路径
     * @param fileName 文件名
     */
    public void uploadByLocaPath(String localpath,String fileName) throws FileNotFoundException {
        File fileInputStream = new File(localpath);
        ossClient.putObject(bucketName, ossPath + fileName,fileInputStream);
    }

    /**
     *@Title:uploadByInputStream
     *@Description 文件流上传
     *@Param ossPath OSS上的路径
     */
    public void uploadByfileInputStream(String fileName,FileInputStream fileInputStream) {
        ossClient.putObject(bucketName, ossPath + fileName,fileInputStream);
    }

    /**
     *@Title:download
     *@Description 流式下载
     *@Param filePath OSS上的路径
     */
    public InputStream download(String filePath){
        OSSObject ossObject = ossClient.getObject(bucketName, filePath);
        InputStream content = ossObject.getObjectContent();
        return content;
    }


    /**
     *@Title:download
     *@Description 文件下载
     *@Param filePath OSS上的路径
     *@Param localPath 本地的路径+文件名
     */
    public void downloadToFile(String pathName ,String localPath){
        ossClient.getObject(new GetObjectRequest(bucketName, pathName), new File(localPath));
    }


    /**
     * @Title: delete
     * @Description: 根据key删除oss服务器上的文件
     * @param filePath 文件路径+文件名
     */
    public void delete(String filePath){
        // 创建OSSClient实例。
        ossClient.deleteObject(bucketName, filePath);
    }
}

下面是测试代码

import com.itcast.common.utils.OSSUtils;
import org.junit.Test;

import java.io.*;

public class OSSTest {

    @Test
    public void uploadTest(){
        String filePath = "D:\\图片\\00.jpg";
        OSSUtils ossUtils = new OSSUtils();
        try {
            ossUtils.create();
            ossUtils.uploadByLocaPath(filePath,"00.jpg");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally{
            ossUtils.close();
        }
    }

    @Test
    public void uploadTest2() {
        String filePath = "D:\\图片\\00.jpg";
        OSSUtils ossUtils = new OSSUtils();
        try {
            ossUtils.create();
            FileInputStream fileInputStream = new FileInputStream(filePath);
            ossUtils.uploadByfileInputStream("test02.jpg",fileInputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally{
            ossUtils.close();
        }
    }

    @Test
    public void downloadTest() {
        String locaPath = "D:\\图片\\01.jpg";
        String filePath = "health/00.jpg";
        OSSUtils ossUtils = new OSSUtils();
        try {
            ossUtils.create();
            InputStream content = ossUtils.download(filePath); //流

            ossUtils.downloadToFile(filePath,locaPath);  //文件
            if (content != null) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                while (true) {
                    String line = reader.readLine();
                    if (line == null) break;
                    System.out.println("\n" + line);
                }
                // 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
                content.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            ossUtils.close();
        }
    }


    @Test
    public void deletetest() {
        String filePath = "health/00.jpg";
        OSSUtils ossUtils = new OSSUtils();
        try {
            ossUtils.create();
            ossUtils.delete(filePath);
        } finally{
            ossUtils.close();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值