|
|
@@ -0,0 +1,660 @@
|
|
|
+package com.sckw.file.utils;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+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.*;
|
|
|
+import com.sckw.core.utils.IdWorker;
|
|
|
+import com.sckw.core.utils.StringUtils;
|
|
|
+import com.sckw.file.common.enums.AliyunOssFileTypeEnum;
|
|
|
+import com.sckw.file.common.enums.FileEnum;
|
|
|
+import jakarta.annotation.PostConstruct;
|
|
|
+import jakarta.servlet.http.HttpServletResponse;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.text.DecimalFormat;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author lfdc
|
|
|
+ * @version 1.0
|
|
|
+ * @className FileUtils
|
|
|
+ * @description Oss文件工具类
|
|
|
+ * @company sckw
|
|
|
+ * @date 2023-06-05 09:06:57
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+@Configuration
|
|
|
+public class FileUtils {
|
|
|
+
|
|
|
+ private static String BASE_DIR = "kll/uploads/";
|
|
|
+
|
|
|
+ //oss客户端连接
|
|
|
+ private static OSS ossclient = null;
|
|
|
+
|
|
|
+
|
|
|
+ private static String DEFAULT_ENDPOINT = "oss-cn-chengdu.aliyuncs.com";
|
|
|
+ private static String DEFAULT_ACCESS_KEY_ID = "LTAI5tPEbubCGq5Rdwygbz4Q";
|
|
|
+ private static String DEFAULT_ACCESS_KEY_SECRET = "7mQLWMaBJeZPRV1SRGogctYGXwppjQ";
|
|
|
+ private static String DEFAULT_BUCKET_NAME = "kaiwu-saas";
|
|
|
+
|
|
|
+ private static String endpoint;
|
|
|
+ private static String accessKeyId;
|
|
|
+ private static String accessKeySecret;
|
|
|
+ private static String bucketName;
|
|
|
+
|
|
|
+
|
|
|
+ @Value("${aliyun.oss.endpoint}")
|
|
|
+ private String oss_endpoint;
|
|
|
+
|
|
|
+ @Value("${aliyun.oss.accessKeyId}")
|
|
|
+ private String oss_accessKeyId;
|
|
|
+
|
|
|
+ @Value("${aliyun.oss.secret}")
|
|
|
+ private String oss_accessKeySecret;
|
|
|
+
|
|
|
+ @Value("${aliyun.oss.bucket}")
|
|
|
+ public String oss_bucketName;
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void setEndpoint() {
|
|
|
+ endpoint = this.oss_endpoint;
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void setAccessKeyId() {
|
|
|
+ accessKeyId = this.oss_accessKeyId;
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void setAccessKeySecret() {
|
|
|
+ accessKeySecret = this.oss_accessKeySecret;
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void setBucketName() {
|
|
|
+ bucketName = this.oss_bucketName;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传文件
|
|
|
+ * <p>
|
|
|
+ * kll/uploads/年月日/md5(file).xxx
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @param fileEnum
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String uploadFile(MultipartFile file, FileEnum fileEnum) {
|
|
|
+ try {
|
|
|
+ //创建OSSClient实例
|
|
|
+ defalutOSS();
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+
|
|
|
+ //容器不存在,就创建
|
|
|
+ if (!ossClient.doesBucketExist(bucketName)) {
|
|
|
+ ossClient.createBucket(bucketName);
|
|
|
+ CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
|
|
|
+ createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
|
|
|
+ ossClient.createBucket(createBucketRequest);
|
|
|
+ }
|
|
|
+ //上传文件流
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ //生成随机唯一值,使用uuid,添加到文件名称里面
|
|
|
+ long uuid = new IdWorker(1).nextId();
|
|
|
+ fileName = String.valueOf(uuid) + fileName;
|
|
|
+ //按照当前日期,创建文件夹,上传到创建文件夹里面
|
|
|
+ //2021/02/02/01.jpgossClient = {OSSClient@13049}
|
|
|
+ String timeUrl = new DateTime().toString("yyyyMMdd");
|
|
|
+ fileName = timeUrl + "/" + fileName;
|
|
|
+ String filePath = BASE_DIR + fileName;
|
|
|
+ //调用方法实现上传
|
|
|
+ ossClient.putObject(bucketName, filePath, inputStream);
|
|
|
+ //上传后的文件地址
|
|
|
+// String url1 = getUrl(ossClient, bucketName, filePath);
|
|
|
+// System.out.println(url1);
|
|
|
+ //关闭OSSClient。
|
|
|
+ ossClient.shutdown();
|
|
|
+ //上传之后文件路径
|
|
|
+ //https://yygh-atguigu.oss-cn-beijing.aliyuncs.com/01.jpg
|
|
|
+ String url = "https://" + bucketName + "." + endpoint + "/" + filePath;
|
|
|
+ //返回 上传文件地址
|
|
|
+ return url;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("oss-upload-file-error:{}", e.getMessage(), e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void defalutOSS() {
|
|
|
+ if (StringUtils.isBlank(endpoint)) {
|
|
|
+ endpoint = DEFAULT_ENDPOINT;
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(accessKeyId)) {
|
|
|
+ accessKeyId = DEFAULT_ACCESS_KEY_ID;
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(accessKeySecret)) {
|
|
|
+ accessKeySecret = DEFAULT_ACCESS_KEY_SECRET;
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(bucketName)) {
|
|
|
+ bucketName = DEFAULT_BUCKET_NAME;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文件上传大小
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getFileSize(MultipartFile file) {
|
|
|
+ long size = file.getSize();
|
|
|
+ DecimalFormat df = new DecimalFormat("#.00");
|
|
|
+ String fileSizeString;
|
|
|
+ if (size < 1024) {
|
|
|
+ fileSizeString = df.format((double) size) + "B";
|
|
|
+ } else if (size < 1048576) {
|
|
|
+ fileSizeString = df.format((double) size / 1024) + "KB";
|
|
|
+ } else if (size < 1073741824) {
|
|
|
+ fileSizeString = df.format((double) size / 1048576) + "MB";
|
|
|
+ } else {
|
|
|
+ fileSizeString = df.format((double) size / 1073741824) + "GB";
|
|
|
+ }
|
|
|
+ return fileSizeString;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得阿里云OSS客户端对象
|
|
|
+ *
|
|
|
+ * @param ossEndpoint
|
|
|
+ * @param accessId
|
|
|
+ * @param accessKey
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static OSS getOssClient(String ossEndpoint, String accessId, String accessKey) {
|
|
|
+ if (ossclient == null) {
|
|
|
+ ossclient = new OSSClientBuilder().build(ossEndpoint, accessId, accessKey);
|
|
|
+ }
|
|
|
+ return ossclient;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过文件名判断并获取OSS服务文件上传时文件的contentType
|
|
|
+ *
|
|
|
+ * @param fileName 文件名
|
|
|
+ * @return 文件的contentType
|
|
|
+ */
|
|
|
+ public static String getContentType(String fileName) {
|
|
|
+ // 文件的后缀名
|
|
|
+ String fileExtension = fileName.substring(fileName.lastIndexOf("."));
|
|
|
+ log.info("getContentType->fileName={},fileExtension={}", fileName, fileExtension);
|
|
|
+ for (AliyunOssFileTypeEnum e : AliyunOssFileTypeEnum.values()) {
|
|
|
+ if (e.getCode().equalsIgnoreCase(fileExtension)) {
|
|
|
+ return e.getText();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 默认返回类型
|
|
|
+ return AliyunOssFileTypeEnum.TXT.getText();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void downOSSFile(String fileName, HttpServletResponse response) {
|
|
|
+ BufferedInputStream input = null;
|
|
|
+ OutputStream outputStream = null;
|
|
|
+ defalutOSS();
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+// OSSObject ossObject = ossClient.getObject(bucketName, folder + fileName);
|
|
|
+ OSSObject ossObject = ossClient.getObject(bucketName, fileName);
|
|
|
+ try {
|
|
|
+ response.reset();
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ response.setContentType("application/x-msdownload");
|
|
|
+ response.addHeader("Content-Disposition",
|
|
|
+ "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1"));
|
|
|
+
|
|
|
+ input = new BufferedInputStream(ossObject.getObjectContent());
|
|
|
+ byte[] buffBytes = new byte[1024];
|
|
|
+ outputStream = response.getOutputStream();
|
|
|
+ int read = 0;
|
|
|
+ while ((read = input.read(buffBytes)) != -1) {
|
|
|
+ outputStream.write(buffBytes, 0, read);
|
|
|
+ }
|
|
|
+ outputStream.flush();
|
|
|
+ // 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
|
|
|
+ ossObject.close();
|
|
|
+ } catch (IOException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (outputStream != null) {
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
+ if (input != null) {
|
|
|
+ input.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过文件名字下载
|
|
|
+ *
|
|
|
+ * @param response response
|
|
|
+ * @param fileName 文件名字,带后缀,例子:postman.txt 文件全路径 https://kaiwu-saas.oss-cn-chengdu.aliyuncs.com/kll/uploads/20230605/146325493677821952598454132.txt
|
|
|
+ */
|
|
|
+ public static void downloadByFileName(HttpServletResponse response, String fileName) {
|
|
|
+ defalutOSS();
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ OSSObject ossObject = ossClient.getObject(bucketName, fileName);
|
|
|
+ String contentType = ossObject.getObjectMetadata().getContentType();
|
|
|
+ String contentType1 = getContentType(fileName);
|
|
|
+ System.out.println("contentType:" + contentType);
|
|
|
+ System.out.println("contentType1:" + contentType1);
|
|
|
+ //设置响应内容类型,当设置了ContentType为“image/jpg”时,浏览器可以直接显示图片;
|
|
|
+ response.setContentType(contentType);
|
|
|
+ BufferedInputStream in = new BufferedInputStream(ossObject.getObjectContent());
|
|
|
+ try {
|
|
|
+ BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
|
|
|
+ //通知浏览器以附件形式下载
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
|
|
|
+ byte[] car = new byte[1024];
|
|
|
+ int len = 0;
|
|
|
+ while ((len = in.read(car)) != -1) {
|
|
|
+ out.write(car, 0, len);
|
|
|
+ }
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ in.close();
|
|
|
+ ossClient.shutdown();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建存储空间
|
|
|
+ *
|
|
|
+ * @param ossClient OSS连接
|
|
|
+ * @param bucketName 存储空间
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String createBucketName(OSS ossClient, String bucketName) {
|
|
|
+ // 存储空间
|
|
|
+ final String bucketNames = bucketName;
|
|
|
+ if (!ossClient.doesBucketExist(bucketName)) {
|
|
|
+ // 创建存储空间
|
|
|
+ Bucket bucket = ossClient.createBucket(bucketName);
|
|
|
+ log.info("创建存储空间成功");
|
|
|
+ return bucket.getName();
|
|
|
+ }
|
|
|
+ return bucketNames;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除存储空间bucketName
|
|
|
+ *
|
|
|
+ * @param ossClient oss对象
|
|
|
+ * @param bucketName 存储空间
|
|
|
+ */
|
|
|
+ public static void deleteBucket(OSS ossClient, String bucketName) {
|
|
|
+ ossClient.deleteBucket(bucketName);
|
|
|
+ log.info("删除" + bucketName + "Bucket成功");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建模拟文件夹:多级目录
|
|
|
+ *
|
|
|
+ * @param ossClient oss连接
|
|
|
+ * @param bucketName 存储空间
|
|
|
+ * @param folder 模拟文件夹名如"upload/2023/01/11/"
|
|
|
+ * @return 文件夹名
|
|
|
+ */
|
|
|
+ public static String createFolder(OSS ossClient, String bucketName, String folder) {
|
|
|
+ // 文件夹名
|
|
|
+ final String keySuffixWithSlash = folder;
|
|
|
+ // 判断文件夹是否存在,不存在则创建
|
|
|
+ if (!ossClient.doesObjectExist(bucketName, keySuffixWithSlash)) {
|
|
|
+ // 创建文件夹
|
|
|
+ ossClient.putObject(bucketName, keySuffixWithSlash, new ByteArrayInputStream(new byte[0]));
|
|
|
+ log.info("创建文件夹成功");
|
|
|
+ // 得到文件夹名
|
|
|
+ OSSObject object = ossClient.getObject(bucketName, keySuffixWithSlash);
|
|
|
+ String fileDir = object.getKey();
|
|
|
+ return fileDir;
|
|
|
+ }
|
|
|
+ return keySuffixWithSlash;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据key删除OSS服务器上的文件
|
|
|
+ *
|
|
|
+ * @param ossClient oss连接
|
|
|
+ * @param bucketName 存储空间
|
|
|
+ * @param key Bucket下的文件的路径名+文件名 如:"upload/2023/01/11/cake.jpg"
|
|
|
+ */
|
|
|
+ public static void deleteObject(OSS ossClient, String bucketName, String key) {
|
|
|
+ ossClient.deleteObject(bucketName, key);
|
|
|
+ log.info("删除" + bucketName + "下的文件" + key + "成功");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传文件
|
|
|
+ *
|
|
|
+ * @param ossClient oss连接
|
|
|
+ * @param bucketName 存储空间
|
|
|
+ * @param ossPath 上传文件相对路径+文件名如"upload/2023/01/11/cake.jpg"
|
|
|
+ * @param is 以输入流的形式上传文件
|
|
|
+ * @param fileName 上传文件后新文件名
|
|
|
+ * @return String 返回的唯一MD5数字签名
|
|
|
+ */
|
|
|
+ public static String uploadFileOss(OSS ossClient, String bucketName, String ossPath, InputStream is, String fileName) {
|
|
|
+ try {
|
|
|
+ // 文件大小
|
|
|
+ long fileSize = is.available();
|
|
|
+ // 创建上传Object的Metadata
|
|
|
+ ObjectMetadata metadata = new ObjectMetadata();
|
|
|
+ // 上传的文件的长度
|
|
|
+ metadata.setContentLength(is.available());
|
|
|
+ // 指定该Object被下载时的网页的缓存行为
|
|
|
+ metadata.setCacheControl("no-cache");
|
|
|
+ // 指定该Object下设置Header
|
|
|
+ metadata.setHeader("Pragma", "no-cache");
|
|
|
+ // 指定该Object被下载时的内容编码格式
|
|
|
+ metadata.setContentEncoding("utf-8");
|
|
|
+ // 文件的MIME,定义文件的类型及网页编码,决定浏览器将以什么形式、什么编码读取文件。如果用户没有指定则根据Key或文件名的扩展名生成,
|
|
|
+ // 如果没有扩展名则填默认值application/octet-stream
|
|
|
+ metadata.setContentType(getContentType(fileName));
|
|
|
+ // 指定该Object被下载时的名称(指示MINME用户代理如何显示附加的文件,打开或下载,及文件名称)
|
|
|
+ metadata.setContentDisposition("filename/filesize=" + fileName + "/" + fileSize + "Byte");
|
|
|
+ //上传文件到OSS时需要指定包含文件后缀在内的完整路径如ossPath="upload/2023/01/11/cake.jpg"
|
|
|
+ PutObjectResult putResult = ossClient.putObject(bucketName, ossPath, is, metadata);
|
|
|
+ // 解析结果
|
|
|
+ String resultStr = putResult.getETag();
|
|
|
+ log.info("唯一MD5数字签名:" + resultStr);
|
|
|
+ //上传文件后相对路径如"upload/2023/01/11/cake.jpg"
|
|
|
+ String path = ossPath;
|
|
|
+ return path;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("上传阿里云OSS服务器异常." + e.getMessage(), e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载文件到本地
|
|
|
+ *
|
|
|
+ * @param ossClient oss连接
|
|
|
+ * @param bucketName 存储空间
|
|
|
+ * @param key Bucket下的文件的路径名+文件名 如:"upload/2023/01/11/cake.jpg"
|
|
|
+ * @param localFilePath 下载本地文件绝对路径如"C:\Users\Administrator\Desktop\oss-download\xxx.pdf"
|
|
|
+ */
|
|
|
+ public static void downloadFileOss(OSS ossClient, String bucketName, String key, String localFilePath) {
|
|
|
+ try {
|
|
|
+ //创建本地文件
|
|
|
+ File file = new File(localFilePath);
|
|
|
+ GetObjectRequest objectRequest = new GetObjectRequest(bucketName, key);
|
|
|
+ //下载OSS文件到本地文件,若指定的本地文件存在则覆盖,否则新建
|
|
|
+ ossClient.getObject(objectRequest, file);
|
|
|
+ log.info("下载文件到本地成功");
|
|
|
+ } catch (OSSException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (ClientException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取上传文件对象
|
|
|
+ * 备注:最重要的是获取上传文件的输出流InputStream
|
|
|
+ *
|
|
|
+ * @param ossClient oss连接
|
|
|
+ * @param bucketName 存储空间
|
|
|
+ * @param key Bucket下的文件的路径名+文件名 如:"upload/2023/01/11/cake.jpg"
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static OSSObject getObject(OSS ossClient, String bucketName, String key) {
|
|
|
+ OSSObject object = null;
|
|
|
+ try {
|
|
|
+ object = ossClient.getObject(bucketName, key);
|
|
|
+ //文件大小
|
|
|
+ long fileSize = object.getObjectMetadata().getContentLength();
|
|
|
+ //文件相对路径
|
|
|
+ String ossPath = object.getKey();
|
|
|
+ //文件输入流
|
|
|
+ InputStream is = object.getObjectContent();
|
|
|
+ log.info("success to getObject,fileSize:" + fileSize + "\nossPath:" + ossPath + "\ninputStream:" + is);
|
|
|
+ } catch (OSSException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (ClientException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return object;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取上传文件url
|
|
|
+ *
|
|
|
+ * @param ossClient oss连接
|
|
|
+ * @param bucketName bucketName
|
|
|
+ * @param key 文件全路径
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getUrl(OSS ossClient, String bucketName, String key) {
|
|
|
+ //设置URl过期时间为99年:3600L*1000*24*365*99
|
|
|
+ Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 99);
|
|
|
+ GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key);
|
|
|
+ generatePresignedUrlRequest.setExpiration(expiration);
|
|
|
+ URL url = ossClient.generatePresignedUrl(generatePresignedUrlRequest);
|
|
|
+ String returnUrl = url.toString();
|
|
|
+ return returnUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void downloadByFileName(String objectName) {
|
|
|
+ // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。关于其他Region对应的Endpoint信息,请参见访问域名和数据中心。
|
|
|
+// String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
|
|
|
+ // 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
|
|
|
+// String accessKeyId = "yourAccessKeyId";
|
|
|
+// String accessKeySecret = "yourAccessKeySecret";
|
|
|
+ // 填写Bucket名称,例如examplebucket。
|
|
|
+// String bucketName = "examplebucket";
|
|
|
+ // 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
|
|
|
+// String objectName = "exampledir/exampleobject.txt";
|
|
|
+
|
|
|
+ // 创建OSSClient实例。
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ try {
|
|
|
+ // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
|
|
|
+ OSSObject ossObject = ossClient.getObject(bucketName, objectName);
|
|
|
+ // 读取文件内容。
|
|
|
+ System.out.println("Object content:");
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()));
|
|
|
+ while (true) {
|
|
|
+ String line = reader.readLine();
|
|
|
+ if (line == null) break;
|
|
|
+
|
|
|
+ System.out.println("\n" + line);
|
|
|
+ }
|
|
|
+ // 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
|
|
|
+ reader.close();
|
|
|
+ // ossObject对象使用完毕后必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
|
|
|
+ ossObject.close();
|
|
|
+
|
|
|
+ } catch (OSSException oe) {
|
|
|
+ System.out.println("Caught an OSSException, which means your request made it to OSS, "
|
|
|
+ + "but was rejected with an error response for some reason.");
|
|
|
+ System.out.println("Error Message:" + oe.getErrorMessage());
|
|
|
+ System.out.println("Error Code:" + oe.getErrorCode());
|
|
|
+ System.out.println("Request ID:" + oe.getRequestId());
|
|
|
+ System.out.println("Host ID:" + oe.getHostId());
|
|
|
+ } catch (Throwable ce) {
|
|
|
+ System.out.println("Caught an ClientException, which means the client encountered "
|
|
|
+ + "a serious internal problem while trying to communicate with OSS, "
|
|
|
+ + "such as not being able to access the network.");
|
|
|
+ System.out.println("Error Message:" + ce.getMessage());
|
|
|
+ } finally {
|
|
|
+ if (ossClient != null) {
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过oss的完整key下载
|
|
|
+ *
|
|
|
+ * @param response response
|
|
|
+ * @param objectName oss完整的key,例子:fj_wechat_web/public/postman.txt
|
|
|
+ */
|
|
|
+ public static void downloadByObjectName(HttpServletResponse response, String objectName) {
|
|
|
+// String endpointStr = "http://" + endpoint + ".aliyuncs.com";
|
|
|
+ String endpointStr = "http://" + endpoint;
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ OSSObject ossObject = ossClient.getObject(bucketName, objectName);
|
|
|
+
|
|
|
+ BufferedInputStream in = new BufferedInputStream(ossObject.getObjectContent());
|
|
|
+
|
|
|
+ try {
|
|
|
+ BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
|
|
|
+ //通知浏览器以附件形式下载
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("postman.txt", "utf-8"));
|
|
|
+
|
|
|
+ byte[] car = new byte[1024];
|
|
|
+ int len;
|
|
|
+ while ((len = in.read(car)) != -1) {
|
|
|
+ out.write(car, 0, len);
|
|
|
+ }
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ in.close();
|
|
|
+ ossClient.shutdown();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过oss的完整key下载
|
|
|
+ *
|
|
|
+ * @param response response
|
|
|
+ * @param url 全路径的url地址
|
|
|
+ */
|
|
|
+ public static void downloadByUrl(HttpServletResponse response, String url) {
|
|
|
+ downloadByFileName(response, url);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断文件是否存在
|
|
|
+ *
|
|
|
+ * @param fileName 文件名:postman.txt
|
|
|
+ * @return true文件存在,false文件不存在
|
|
|
+ */
|
|
|
+ public boolean isExitByFileName(String fileName) {
|
|
|
+// String endpointStr = "http://" + endpoint + ".aliyuncs.com";
|
|
|
+ String endpointStr = "http://" + endpoint;
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpointStr, accessKeyId, accessKeySecret);
|
|
|
+ return ossClient.doesObjectExist(bucketName, BASE_DIR + fileName);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断文件是否存在
|
|
|
+ *
|
|
|
+ * @param url 能直接访问的完整url
|
|
|
+ * @return true文件存在,false文件不存在
|
|
|
+ */
|
|
|
+ public boolean isExitByUrl(String url) {
|
|
|
+ return isExitByFileName(getFileNameByUrl(url));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过url获取文件名
|
|
|
+ *
|
|
|
+ * @param url 能够直接访问的url
|
|
|
+ * @return 文件名:postman.txt
|
|
|
+ */
|
|
|
+ public static String getFileNameByUrl(String url) {
|
|
|
+ String[] split = url.split("/");
|
|
|
+ return split[split.length - 1];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 测试
|
|
|
+ public static void main(String[] args) throws FileNotFoundException {
|
|
|
+ //阿里云OSS账号自行到阿里云官网申请
|
|
|
+ String ossEndpoint = "XXX";
|
|
|
+ String accessId = "XXX";
|
|
|
+ String accessKey = "XXX";
|
|
|
+ String bucketName = "test";
|
|
|
+ // 初始化OSSClient
|
|
|
+// OSS ossClient = AliyunOSSClientUtil.getOssClient(ossEndpoint, accessId, accessKey);
|
|
|
+ OSS ossClient = FileUtils.getOssClient(ossEndpoint, accessId, accessKey);
|
|
|
+
|
|
|
+
|
|
|
+ //测试创建多级目录
|
|
|
+ /*String tmpDir = "upload/2023/01/11/";
|
|
|
+ String folder = createFolder(ossClient, bucketName, tmpDir);
|
|
|
+ System.out.println("folder:"+folder);*/
|
|
|
+
|
|
|
+
|
|
|
+ //测试删除文件
|
|
|
+ /*String key="upload/2023/01/11/xxx.pdf";
|
|
|
+ deleteObject(ossClient,bucketName,key);*/
|
|
|
+
|
|
|
+
|
|
|
+ //测试上传文件
|
|
|
+ /*String pathAndname = "C:\\Users\\Administrator\\Desktop\\测试文件上传\\xxx.pdf";
|
|
|
+ File file = new File(pathAndname);
|
|
|
+ //原始文件名:带后缀
|
|
|
+ String oldfilename = file.getName();
|
|
|
+ //新文件名:带后缀
|
|
|
+ String newfilename = "9065df0f3ab72419b36d2dec088e11d6.pdf";//可以自行生成随机唯一文件名
|
|
|
+ String newpath = "C:\\Users\\Administrator\\Desktop\\upload\\2023\\01\\11\\";
|
|
|
+ String ossPath = newpath + newfilename;
|
|
|
+ InputStream is = new FileInputStream(file);
|
|
|
+ String absolutePath = uploadFileOss(ossClient, bucketName, ossPath, is, newfilename);
|
|
|
+ System.out.println("absolutePath:"+absolutePath);*/
|
|
|
+
|
|
|
+
|
|
|
+ //测试获取文件url
|
|
|
+ /*String key="upload/2023/01/11/9065df0f3ab72419b36d2dec088e11d6.pdf";
|
|
|
+ String url = getUrl(ossClient, bucketName, key);
|
|
|
+ System.out.println("url:"+url);*/
|
|
|
+
|
|
|
+
|
|
|
+ //测试获取上传对象
|
|
|
+ /*String key = "upload/2023/01/11/9065df0f3ab72419b36d2dec088e11d6.pdf";
|
|
|
+ getObject(ossClient, bucketName, key);*/
|
|
|
+
|
|
|
+
|
|
|
+ //测试下载文件到本地
|
|
|
+ /*String key = "upload/2023/01/11/9065df0f3ab72419b36d2dec088e11d6.pdf";
|
|
|
+ String localFilePath = "C:\\Users\\Administrator\\Desktop\\oss-download\\xxx.pdf";
|
|
|
+ downloadFileOss(ossClient, bucketName, key, localFilePath);*/
|
|
|
+ }
|
|
|
+
|
|
|
+}
|