OSSUtils.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package com.caimei.modules.oss.utils;
  2. import com.aliyun.oss.OSS;
  3. import com.aliyun.oss.OSSClientBuilder;
  4. import com.aliyun.oss.model.GetObjectRequest;
  5. import com.aliyun.oss.model.ObjectMetadata;
  6. import com.aliyun.oss.model.UploadFileRequest;
  7. import com.caimei.modules.archive.entity.CmProductArchiveContent;
  8. import com.thinkgem.jeesite.common.config.Global;
  9. import org.springframework.web.multipart.MultipartFile;
  10. import java.io.File;
  11. import java.io.IOException;
  12. import java.text.ParseException;
  13. import java.text.SimpleDateFormat;
  14. import java.util.Date;
  15. import java.util.UUID;
  16. public class OSSUtils {
  17. private static String endpoint = Global.getConfig("aliyun.endpoint");
  18. private static String accessKeyId = Global.getConfig("aliyun.accessKeyId");
  19. private static String accessKeySecret = Global.getConfig("aliyun.accessKeySecret");
  20. private static String privateBucket = Global.getConfig("aliyun.bucketName");
  21. private static String config = Global.getConfig("cm.config");
  22. public static String ossUpload(String fileName, File file, String contentType) {
  23. String url = null;
  24. try {
  25. if ("product".equals(config)) {
  26. fileName = "prod/" + fileName;
  27. } else {
  28. fileName = config + "/" + fileName;
  29. }
  30. OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
  31. ObjectMetadata meta = new ObjectMetadata();
  32. meta.setContentType(contentType);
  33. UploadFileRequest uploadFileRequest = new UploadFileRequest(privateBucket, fileName);
  34. // 指定上传的本地文件。
  35. uploadFileRequest.setUploadFile(file.toString());
  36. // 指定上传并发线程数,默认为1。
  37. uploadFileRequest.setTaskNum(10);
  38. // 指定上传的分片大小,范围为100KB~5GB,默认为文件大小/10000。
  39. uploadFileRequest.setPartSize(1024 * 1024);
  40. // 开启断点续传,默认关闭。
  41. uploadFileRequest.setEnableCheckpoint(true);
  42. uploadFileRequest.setCheckpointFile(file.getAbsolutePath() + "uploadFile.ucp");
  43. // 文件的元数据。
  44. uploadFileRequest.setObjectMetadata(meta);
  45. // 设置上传成功回调,参数为Callback类型。
  46. //uploadFileRequest.setCallback("<yourCallbackEvent>");
  47. // 断点续传上传。
  48. ossClient.uploadFile(uploadFileRequest);
  49. Date expiration = new Date(new Date().getTime() + 3600L * 1000);
  50. url = ossClient.generatePresignedUrl(privateBucket, fileName, expiration).toString();
  51. // 关闭OSSClient。
  52. ossClient.shutdown();
  53. } catch (Throwable e) {
  54. e.printStackTrace();
  55. }
  56. return url;
  57. }
  58. /**
  59. * 通过文件名判断并获取OSS服务文件上传时文件的contentType
  60. */
  61. public static String getContentType(String fileName) {
  62. String fileExtension = fileName.substring(fileName.lastIndexOf("."));
  63. if (".bmp".equalsIgnoreCase(fileExtension)) {
  64. return "image/bmp";
  65. }
  66. if (".gif".equalsIgnoreCase(fileExtension)) {
  67. return "image/gif";
  68. }
  69. if (".jpeg".equalsIgnoreCase(fileExtension)) {
  70. return "image/jpeg";
  71. }
  72. if (".jpg".equalsIgnoreCase(fileExtension)) {
  73. return "image/jpg";
  74. }
  75. if (".png".equalsIgnoreCase(fileExtension)) {
  76. return "image/png";
  77. }
  78. if (".html".equalsIgnoreCase(fileExtension)) {
  79. return "text/html";
  80. }
  81. if (".txt".equalsIgnoreCase(fileExtension)) {
  82. return "text/plain";
  83. }
  84. if (".vsd".equalsIgnoreCase(fileExtension)) {
  85. return "application/vnd.visio";
  86. }
  87. if (".ppt".equalsIgnoreCase(fileExtension) || "pptx".equalsIgnoreCase(fileExtension)) {
  88. return "application/vnd.ms-powerpoint";
  89. }
  90. if (".doc".equalsIgnoreCase(fileExtension) || "docx".equalsIgnoreCase(fileExtension)) {
  91. return "application/msword";
  92. }
  93. if (".xml".equalsIgnoreCase(fileExtension)) {
  94. return "text/xml";
  95. }
  96. if (".mp4".equalsIgnoreCase(fileExtension)) {
  97. return "video/mp4";
  98. }
  99. if (".mp3".equalsIgnoreCase(fileExtension)) {
  100. return "audio/mp3";
  101. }
  102. if (".pdf".equalsIgnoreCase(fileExtension)) {
  103. return "application/pdf";
  104. }
  105. return "text/html";
  106. }
  107. public static void deleteFile(File... files) {
  108. for (File file : files) {
  109. //logger.info("File:[{}]",file.getAbsolutePath());
  110. if (file.exists()) {
  111. file.delete();
  112. }
  113. }
  114. }
  115. public static File ossUpload(MultipartFile file) throws IOException {
  116. // 获取文件名
  117. String fileName = file.getOriginalFilename();
  118. // 获取文件后缀
  119. String prefix = fileName.substring(fileName.lastIndexOf("."));
  120. // 用uuid作为文件名,防止生成的临时文件重复
  121. File excelFile = File.createTempFile(UUID.randomUUID().toString(), prefix);
  122. // MultipartFile to File
  123. file.transferTo(excelFile);
  124. //程序结束时,删除临时文件
  125. return excelFile;
  126. }
  127. /**
  128. * 授权生成签名URL临时访问
  129. *
  130. * @param fileName 文件名称
  131. */
  132. public static String getOssUrl(String fileName) {
  133. OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
  134. // 设置URL过期时间为1个小时
  135. Date expiration = new Date(new Date().getTime() + 3600L * 1000);
  136. if ("product".equals(config)) {
  137. fileName = "prod/" + fileName;
  138. } else {
  139. fileName = config + "/" + fileName;
  140. }
  141. String url = ossClient.generatePresignedUrl(privateBucket, fileName, expiration).toString();
  142. // 关闭OSSClient。
  143. ossClient.shutdown();
  144. return url;
  145. }
  146. /**
  147. * 生成商品资料库链接
  148. *
  149. * @param archiveFile
  150. * @return
  151. */
  152. public static String generateProductArchiveUrl(String ossName, Date uploadTime) {
  153. OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
  154. // 设置URL过期时间为1个小时
  155. Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000);
  156. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  157. try {
  158. Date dateOne = format.parse("2021-06-01 00:00:00");
  159. Date dateTwo = format.parse("2021-09-17 18:00:00");
  160. String active = "product".equals(config) ? "prod" : config;
  161. if (uploadTime != null && uploadTime.compareTo(dateOne) > 0 && uploadTime.compareTo(dateTwo) < 0) {
  162. ossName = active + "/" + ossName;
  163. } else if (uploadTime != null && uploadTime.compareTo(dateTwo) > 0) {
  164. ossName = active + "/archiveFile/" + ossName;
  165. }
  166. } catch (ParseException e) {
  167. }
  168. String url = ossClient.generatePresignedUrl(privateBucket, ossName, expiration).toString();
  169. ossClient.shutdown();
  170. return url;
  171. }
  172. /**
  173. * oss单个文件删除
  174. *
  175. * @param fileName 文件名称或文件夹名称
  176. */
  177. public static void deleteSingleFile(String fileName) {
  178. // 创建OSSClient实例。
  179. OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
  180. // 删除文件。如需删除文件夹,请将ObjectName设置为对应的文件夹名称。
  181. // 如果文件夹非空,则需要将文件夹下的所有object删除后才能删除该文件夹。
  182. try {
  183. //之前的文件没有放进文件夹
  184. ossClient.deleteObject(privateBucket, fileName);
  185. } catch (Exception e) {
  186. if ("product".equals(config)) {
  187. fileName = "prod/" + fileName;
  188. } else {
  189. fileName = config + "/" + fileName;
  190. }
  191. ossClient.deleteObject(privateBucket, fileName);
  192. }
  193. // 关闭OSSClient。
  194. ossClient.shutdown();
  195. }
  196. /**
  197. * oss单个文件下载
  198. */
  199. public static void downFile(String ossName, String fileName) {
  200. // 创建OSSClient实例。
  201. OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
  202. // 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖,不存在则新建。
  203. try {
  204. //之前的文件没有放进文件夹
  205. ossClient.getObject(new GetObjectRequest(privateBucket, ossName), new File("./" + fileName));
  206. } catch (Exception e) {
  207. if ("product".equals(config)) {
  208. ossName = "prod/" + ossName;
  209. } else {
  210. ossName = config + "/" + ossName;
  211. }
  212. ossClient.getObject(new GetObjectRequest(privateBucket, ossName), new File("./" + fileName));
  213. }
  214. // 关闭OSSClient。
  215. ossClient.shutdown();
  216. }
  217. }