阿里云oss上传和下载

2023-11-19

 这段为上传的代码,账号之类前往注册,可以根据个人情况进行修改。

/***
     * 上传文件
     *
     * @param endPoint 路径
     * @param bucketName 桶名称
     * @param fileName 文件绝对路径和名称
     * @param ossPath 上传存储路径
     * */
    public static String uploadFile2(String endPoint,String accessKeyId,String accessKeySecret,String bucketName,String ossPath,String fileName){
        //连接
        OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
        try {
            //判断是否以/结尾
            if(!ossPath.endsWith("/")){
                ossPath += "/";
            }
            //判断是否以/开始
            if(ossPath.startsWith("/")){
                ossPath=ossPath.substring(1);
            }
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, ossPath+nName, new File(fileName));
            ossClient.putObject(putObjectRequest);
            return "true";
        }finally {
            ossClient.shutdown();
        }

    }

下载,根据官网进行修改。

 /**
     * @Title:downloadToFile
     * @Description: 下载文件到本地
     * @param @param ossFilePath oss上的文件路径(全路径)
     * @param @param newFilePath 本地文件路径 (全路径)
     * @return void    返回类型
     */
    public static void downloadToFile(String endPoint, String accessKeyId, String accessKeySecret, String bucketName, String ossFilePath, String newFilePath){
        //实例化OSSClient对象
        OSSClient ossClient = (OSSClient) new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
        try {
            // 指定路径如果没有则创建并添加
            File file = new File(newFilePath);
            //获取父目录
            File fileParent = file.getParentFile();
            //判断是否存在
            if (!fileParent.exists()) {
                // 创建父目录文件
                fileParent.mkdirs();
            }
            file.createNewFile();
            // 下载Object到本地文件,并保存到指定的本地路径中。如果指定的本地文件存在会覆盖,不存在则新建。
            // 如果未指定本地路径,则下载后的文件默认保存到示例程序所属项目对应本地路径中。
            ossClient.getObject(new GetObjectRequest(bucketName, ossFilePath), new File(newFilePath));

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

ossUtils工具类,根据情况进行修改

package com.rs.common.utils;

import com.aliyun.oss.*;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectRequest;

import java.io.*;

public class OssUtils {
    /***
     * 上传文件
     *  wangxikun
     * @param endPoint 路径
     * @param bucketName 桶名称
     * @param fileName 文件绝对路径和名称
     * @param ossPath 上传存储路径
     * */
    public static String uploadFile(String endPoint,String accessKeyId,String accessKeySecret,String bucketName,String ossPath,String fileName){
        OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
        try {
            //判断是否以/结尾
            if(ossPath.endsWith("/")){
                ossPath=ossPath.substring(0,ossPath.length()-1);
            }
            //判断是否以/开头
            if(ossPath.startsWith("/")){
                ossPath=ossPath.substring(1);
            }
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, ossPath+fileName, new File(fileName));
            ossClient.putObject(putObjectRequest);
            return "true";
        }finally {
            ossClient.shutdown();
        }

    }
    /***
     * @param endPoint 路径
     * @param bucketName 桶名称
     * @param fileName 文件绝对路径和名称
     * @param ossPath 下载存储路径
     * */
    public static InputStream getFileStream(String endPoint,String accessKeyId,String accessKeySecret,String bucketName,String ossPath,String fileName) throws Exception {
        if(ossPath.endsWith("/")){
            ossPath=ossPath.substring(0,ossPath.length()-1);
        }
        if(ossPath.startsWith("/")){
            ossPath=ossPath.substring(1);
        }
        String objectName = ossPath+fileName;
        OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
        try {
            OSSObject ossObject = ossClient.getObject(bucketName, objectName);
            InputStream ins=ossObject.getObjectContent();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = ins.read(buffer)) > -1 ) {
                baos.write(buffer, 0, len);
            }
            baos.flush();
            return new ByteArrayInputStream(baos.toByteArray());
        }finally {
            ossClient.shutdown();
        }

    }
    /**
     * @Title:downloadToFile
     * @Description: 下载文件到本地
     * @param @param ossFilePath oss上的文件路径
     * @param @param newFilePath 本地文件路径
     * @return void    返回类型
     */
    public static void downloadToFile(String endPoint, String accessKeyId, String accessKeySecret, String bucketName, String ossFilePath, String newFilePath){
        //实例化OSSClient对象
        OSSClient ossClient = (OSSClient) new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
        try {
            // 指定路径如果没有则创建并添加
            File file = new File(newFilePath);
            //获取父目录
            File fileParent = file.getParentFile();
            //判断是否存在
            if (!fileParent.exists()) {
                // 创建父目录文件
                fileParent.mkdirs();
            }
            file.createNewFile();
            // 下载Object到本地文件,并保存到指定的本地路径中。如果指定的本地文件存在会覆盖,不存在则新建。
            // 如果未指定本地路径,则下载后的文件默认保存到示例程序所属项目对应本地路径中。
            ossClient.getObject(new GetObjectRequest(bucketName, ossFilePath), new File(newFilePath));

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

阿里云oss上传和下载 的相关文章

随机推荐