package com.muyu.load; import com.aliyun.oss.OSSClient; import com.aliyun.oss.model.OSSObject; import com.muyu.common.core.domain.Result; import com.muyu.compile.OSSFileCompile; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Map; /** * @Author:蓬叁 * @Package:com.muyu.load * @Project:cloud-rule * @name:OSSFileLoad * @Date:2024/8/31 下午6:38 */ public class OSSFileLoad { // 阿里云 endpoint private static final String endpoint = "oss-cn-shanghai.aliyuncs.com"; // 阿里云 accessKeyId private static final String accessKeyId = "LTAI5tHKdDATVKeBjFH8mb8D"; // 阿里云 accessKeySecret   private static final String accessKeySecret = "5ejb4qdgukfD1FdM181kdeAkSuURo7"; // bucket private static final String bucketName = "java-web-generating-class"; public static Result streamingDownload(String fileName) throws IOException { String source = null; // 创建 OSSClient 实例 OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret); // 判断 bucket 是否存在 Boolean flag_bucket = ossClient.doesBucketExist(bucketName); if (!flag_bucket) { System.out.println("bucket不存在"); return Result.error("bucket不存在"); } // 判断 要下载的文件 是否存在 Boolean flag_file = ossClient.doesObjectExist(bucketName, fileName); if (!flag_file) { System.out.println("预下载文件不存在"); return Result.error("预下载文件不存在"); } // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流 OSSObject ossObject = ossClient.getObject(bucketName, fileName); // 按行读取文件内容 BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent())); while(true){ String line = reader.readLine(); source += line; if (line == null) break; } System.out.println(source); System.out.println("读取成功"); // 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作 reader.close(); // 关闭oss ossClient.shutdown(); // Map compile = OSSFileCompile.compile(fileName, source, "target/"); Result compile = OSSFileCompile.compile(source, "target/"); System.out.println(compile.getData()); return Result.success(source); } }