OssUtils.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.model.VaultsResponse;
  5. import com.poteviohealth.cgp.common.utils.CgpTool;
  6. import com.poteviohealth.cgp.common.utils.FingerImage;
  7. import lombok.extern.log4j.Log4j2;
  8. import org.apache.commons.io.FileUtils;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.stereotype.Component;
  11. import org.springframework.web.multipart.MultipartFile;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.text.SimpleDateFormat;
  16. import java.util.Date;
  17. /**
  18. * 阿里云图片上传工具类
  19. * @author Qin
  20. */
  21. @Component
  22. @Log4j2
  23. public class OssUtils {
  24. @Value("${aliyun.oss.accessKeyId}")
  25. private String accessKeyId;
  26. @Value("${aliyun.oss.accessKeySecret}")
  27. private String secretAccessKey;
  28. @Value("${aliyun.oss.endpoint}")
  29. private String endPoint;
  30. @Value("${aliyun.oss.internetpoint}")
  31. private String internetpoint;
  32. @Value("${aliyun.oss.bucketName}")
  33. private String bucketName;
  34. @Value("${aliyun.oss.dir}")
  35. private String dir;
  36. public VaultsResponse<String> uploadOneFile(MultipartFile dto, Long orderId) {
  37. try {
  38. return uploadOneFile(dto.getOriginalFilename(),orderId,dto.getInputStream());
  39. } catch (IOException e) {
  40. e.printStackTrace();
  41. }
  42. return null;
  43. }
  44. public VaultsResponse<String> uploadOneFile(String filename, Long orderId,InputStream inputStream) {
  45. // 创建OSSClient实例。
  46. log.info("开始上传===");
  47. OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, secretAccessKey);
  48. try {
  49. log.info("fileName==="+filename);
  50. //设置文件名
  51. Integer pos = filename.lastIndexOf('.');
  52. String suffix = "";
  53. if (pos != -1) {
  54. suffix = filename.substring(pos);
  55. }
  56. SimpleDateFormat format = new SimpleDateFormat("yyMM");
  57. String dirStr = dir+format.format(new Date());
  58. String fileName = dirStr+"/S"+orderId+"N-"+ CgpTool.generateUUID() +suffix;
  59. // 创建PutObject请求。
  60. ossClient.putObject(bucketName, fileName, inputStream);
  61. String url = "https://" + bucketName + "." + internetpoint + "/" + fileName;
  62. // System.out.println(url);
  63. log.info("结束上传===");
  64. return VaultsResponse.success(url);
  65. } catch (Exception e) {
  66. e.printStackTrace();
  67. return null;
  68. } finally {
  69. if (ossClient != null) {
  70. ossClient.shutdown();
  71. }
  72. }
  73. }
  74. public boolean deleteFile(String fileUrl) {
  75. // 创建OSSClient实例。
  76. OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, secretAccessKey);
  77. /** oss删除文件是根据文件完成路径删除的,但文件完整路径中不能包含Bucket名称。
  78. * 比如文件路径为:http://edu-czf.oss-cn-guangzhou.aliyuncs.com/2022/08/abc.jpg",
  79. * 则完整路径就是:2022/08/abc.jpg
  80. */
  81. int begin = ("https://" + bucketName + "." + internetpoint + "/").length(); //找到文件路径的开始下标
  82. String deleteUrl = fileUrl.substring(begin);
  83. try {
  84. // 删除文件请求
  85. ossClient.deleteObject(bucketName, deleteUrl);
  86. return true;
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. return false;
  90. } finally {
  91. if (ossClient != null) {
  92. ossClient.shutdown();
  93. }
  94. }
  95. }
  96. public File compressVideo(File inputFile) {
  97. log.info("视频压缩开始==="+inputFile.getName());
  98. try {
  99. // 创建ProcessBuilder对象
  100. log.info("输入路径==="+inputFile.getAbsolutePath());
  101. // 等待命令执行完成
  102. String outName = "out"+CgpTool.generateUUID()+".mp4";
  103. String command = "ffmpeg -i " + inputFile.getAbsolutePath() + " -r 15 -b:v 600k -s 720x1280 -ss 00:00:00 -t 00:00:15 "+ outName;
  104. log.info("压缩方式==="+command);
  105. ProcessBuilder pb = new ProcessBuilder("ffmpeg","-i",inputFile.getAbsolutePath(),"-r","15","-b:v","600k","-s","720x1280","-ss","00:00:00","-t","00:00:15",outName);
  106. pb.redirectErrorStream(true);
  107. pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
  108. Process process = pb.start();
  109. process.waitFor();
  110. log.info("视频压缩完成===");
  111. return new File(outName);
  112. } catch (IOException e) {
  113. e.printStackTrace();
  114. } catch (InterruptedException e){
  115. e.printStackTrace();
  116. }
  117. return null;
  118. }
  119. /**
  120. * 获取指定的视频文件后进行封面截图为jpg
  121. */
  122. public File generateCover(File inputFile) {
  123. log.info("视频截图开始==="+inputFile.getName());
  124. try {
  125. // 创建ProcessBuilder对象
  126. log.info("输入路径==="+inputFile.getAbsolutePath());
  127. // 截图保存位置
  128. String outName = "out"+CgpTool.generateUUID()+".jpg";
  129. String command = "ffmpeg -sseof -1 -i " + inputFile.getAbsolutePath() + " -vframes 1 "+ outName;
  130. log.info("封面截图方式==="+command);
  131. ProcessBuilder pb = new ProcessBuilder("ffmpeg","-sseof","-1","-i",inputFile.getAbsolutePath(),"-vframes","1",outName);
  132. pb.redirectErrorStream(true);
  133. pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
  134. Process process = pb.start();
  135. process.waitFor();
  136. log.info("封面截图完成===");
  137. return new File(outName);
  138. } catch (IOException e) {
  139. e.printStackTrace();
  140. } catch (InterruptedException e){
  141. e.printStackTrace();
  142. }
  143. return null;
  144. }
  145. /**
  146. * 获取文件
  147. * @param fileUrl
  148. * @return
  149. */
  150. public String getFileFinger(String fileUrl) {
  151. // 创建OSSClient实例。
  152. OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, secretAccessKey);
  153. try {
  154. String url = fileUrl.replace("https://ptsubsidy.oss-cn-beijing.aliyuncs.com/", "");
  155. InputStream inputStream = ossClient.getObject(bucketName, url).getObjectContent();
  156. if(url.indexOf("mp4") != -1){
  157. File tempFile = File.createTempFile("temp_", ".mp4");
  158. FileUtils.copyInputStreamToFile(inputStream, tempFile);
  159. File file = this.generateCover(tempFile);
  160. tempFile.delete();
  161. String val = FingerImage.readImagePix(file);
  162. file.delete();
  163. return val;
  164. }
  165. return FingerImage.readImagePix(inputStream);
  166. } catch (Exception e) {
  167. e.printStackTrace();
  168. return null;
  169. } finally {
  170. if (ossClient != null) {
  171. ossClient.shutdown();
  172. }
  173. }
  174. }
  175. }