| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package com.poteviohealth.cgp.statistics.utils;
- import com.aliyun.oss.OSS;
- import com.aliyun.oss.OSSClientBuilder;
- import com.poteviohealth.cgp.common.utils.CgpTool;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- import org.springframework.web.multipart.MultipartFile;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * 阿里云图片上传工具类
- * @author Qin
- */
- @Component
- public class OssUtils {
- @Value("${aliyun.oss.accessKeyId}")
- private String accessKeyId;
- @Value("${aliyun.oss.accessKeySecret}")
- private String secretAccessKey;
- @Value("${aliyun.oss.endpoint}")
- private String endPoint;
- @Value("${aliyun.oss.bucketName}")
- private String bucketName;
- @Value("${aliyun.oss.dir}")
- private String dir;
- public String uploadOneFile(MultipartFile file,String key) {
- // 创建OSSClient实例。
- OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, secretAccessKey);
- //设置文件名
- Integer pos = file.getName().lastIndexOf('.');
- String suffix = "";
- if (pos != -1) {
- suffix = file.getName().substring(pos);
- }
- String fileName = dir+"/S"+key+"N-"+ CgpTool.generateUUID() +suffix;
- try {
- // 创建PutObject请求。
- ossClient.putObject(bucketName, fileName, file.getInputStream());
- String url = "https://" + bucketName + "." + endPoint + "/" + fileName;
- return url;
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- } finally {
- if (ossClient != null) {
- ossClient.shutdown();
- }
- }
- }
- public List<String> uploadArrayFile(MultipartFile[] files,String key) {
- // 创建OSSClient实例。
- OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, secretAccessKey);
- List<String> list = new ArrayList<>();
- try {
- //设置文件名
- for (MultipartFile file : files) {
- Integer pos = file.getName().lastIndexOf('.');
- String suffix = "";
- if (pos != -1) {
- suffix = file.getName().substring(pos);
- }
- String fileName = dir+"/S"+key+"N-"+ CgpTool.generateUUID() +suffix;
- // 创建PutObject请求。
- ossClient.putObject(bucketName, fileName, file.getInputStream());
- String url = "https://" + bucketName + "." + endPoint + "/" + fileName;
- // System.out.println(url);
- list.add(url);
- }
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- } finally {
- if (ossClient != null) {
- ossClient.shutdown();
- }
- }
- return list;
- }
- public boolean deleteFile(String fileUrl) {
- // 创建OSSClient实例。
- OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, secretAccessKey);
- /** oss删除文件是根据文件完成路径删除的,但文件完整路径中不能包含Bucket名称。
- * 比如文件路径为:http://edu-czf.oss-cn-guangzhou.aliyuncs.com/2022/08/abc.jpg",
- * 则完整路径就是:2022/08/abc.jpg
- */
- int begin = ("https://" + bucketName + "." + endPoint + "/").length(); //找到文件路径的开始下标
- String deleteUrl = fileUrl.substring(begin);
- try {
- // 删除文件请求
- ossClient.deleteObject(bucketName, deleteUrl);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- } finally {
- if (ossClient != null) {
- ossClient.shutdown();
- }
- }
- }
- }
|