OssUtils.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package com.poteviohealth.cgp.statistics.utils;
  2. import com.aliyun.oss.OSS;
  3. import com.aliyun.oss.OSSClientBuilder;
  4. import com.poteviohealth.cgp.common.utils.CgpTool;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.stereotype.Component;
  7. import org.springframework.web.multipart.MultipartFile;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. /**
  11. * 阿里云图片上传工具类
  12. * @author Qin
  13. */
  14. @Component
  15. public class OssUtils {
  16. @Value("${aliyun.oss.accessKeyId}")
  17. private String accessKeyId;
  18. @Value("${aliyun.oss.accessKeySecret}")
  19. private String secretAccessKey;
  20. @Value("${aliyun.oss.endpoint}")
  21. private String endPoint;
  22. @Value("${aliyun.oss.bucketName}")
  23. private String bucketName;
  24. @Value("${aliyun.oss.dir}")
  25. private String dir;
  26. public String uploadOneFile(MultipartFile file,String key) {
  27. // 创建OSSClient实例。
  28. OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, secretAccessKey);
  29. //设置文件名
  30. Integer pos = file.getName().lastIndexOf('.');
  31. String suffix = "";
  32. if (pos != -1) {
  33. suffix = file.getName().substring(pos);
  34. }
  35. String fileName = dir+"/S"+key+"N-"+ CgpTool.generateUUID() +suffix;
  36. try {
  37. // 创建PutObject请求。
  38. ossClient.putObject(bucketName, fileName, file.getInputStream());
  39. String url = "https://" + bucketName + "." + endPoint + "/" + fileName;
  40. return url;
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. return null;
  44. } finally {
  45. if (ossClient != null) {
  46. ossClient.shutdown();
  47. }
  48. }
  49. }
  50. public List<String> uploadArrayFile(MultipartFile[] files,String key) {
  51. // 创建OSSClient实例。
  52. OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, secretAccessKey);
  53. List<String> list = new ArrayList<>();
  54. try {
  55. //设置文件名
  56. for (MultipartFile file : files) {
  57. Integer pos = file.getName().lastIndexOf('.');
  58. String suffix = "";
  59. if (pos != -1) {
  60. suffix = file.getName().substring(pos);
  61. }
  62. String fileName = dir+"/S"+key+"N-"+ CgpTool.generateUUID() +suffix;
  63. // 创建PutObject请求。
  64. ossClient.putObject(bucketName, fileName, file.getInputStream());
  65. String url = "https://" + bucketName + "." + endPoint + "/" + fileName;
  66. // System.out.println(url);
  67. list.add(url);
  68. }
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. return null;
  72. } finally {
  73. if (ossClient != null) {
  74. ossClient.shutdown();
  75. }
  76. }
  77. return list;
  78. }
  79. public boolean deleteFile(String fileUrl) {
  80. // 创建OSSClient实例。
  81. OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, secretAccessKey);
  82. /** oss删除文件是根据文件完成路径删除的,但文件完整路径中不能包含Bucket名称。
  83. * 比如文件路径为:http://edu-czf.oss-cn-guangzhou.aliyuncs.com/2022/08/abc.jpg",
  84. * 则完整路径就是:2022/08/abc.jpg
  85. */
  86. int begin = ("https://" + bucketName + "." + endPoint + "/").length(); //找到文件路径的开始下标
  87. String deleteUrl = fileUrl.substring(begin);
  88. try {
  89. // 删除文件请求
  90. ossClient.deleteObject(bucketName, deleteUrl);
  91. return true;
  92. } catch (Exception e) {
  93. e.printStackTrace();
  94. return false;
  95. } finally {
  96. if (ossClient != null) {
  97. ossClient.shutdown();
  98. }
  99. }
  100. }
  101. }