| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- package com.poteviohealth.cgp.statistics.utils;
- import com.aliyun.oss.OSS;
- import com.aliyun.oss.OSSClientBuilder;
- import com.poteviohealth.cgp.common.model.VaultsResponse;
- import com.poteviohealth.cgp.common.utils.CgpTool;
- import com.poteviohealth.cgp.common.utils.FingerImage;
- import lombok.extern.log4j.Log4j2;
- import org.apache.commons.io.FileUtils;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- /**
- * 阿里云图片上传工具类
- * @author Qin
- */
- @Component
- @Log4j2
- 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.internetpoint}")
- private String internetpoint;
- @Value("${aliyun.oss.bucketName}")
- private String bucketName;
- @Value("${aliyun.oss.dir}")
- private String dir;
- public VaultsResponse<String> uploadOneFile(MultipartFile dto, Long orderId) {
- try {
- return uploadOneFile(dto.getOriginalFilename(),orderId,dto.getInputStream());
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- public VaultsResponse<String> uploadOneFile(String filename, Long orderId,InputStream inputStream) {
- // 创建OSSClient实例。
- log.info("开始上传===");
- OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, secretAccessKey);
- try {
- log.info("fileName==="+filename);
- //设置文件名
- Integer pos = filename.lastIndexOf('.');
- String suffix = "";
- if (pos != -1) {
- suffix = filename.substring(pos);
- }
- SimpleDateFormat format = new SimpleDateFormat("yyMM");
- String dirStr = dir+format.format(new Date());
- String fileName = dirStr+"/S"+orderId+"N-"+ CgpTool.generateUUID() +suffix;
- // 创建PutObject请求。
- ossClient.putObject(bucketName, fileName, inputStream);
- String url = "https://" + bucketName + "." + internetpoint + "/" + fileName;
- // System.out.println(url);
- log.info("结束上传===");
- return VaultsResponse.success(url);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- } finally {
- if (ossClient != null) {
- ossClient.shutdown();
- }
- }
- }
- 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 + "." + internetpoint + "/").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();
- }
- }
- }
- public File compressVideo(File inputFile) {
- log.info("视频压缩开始==="+inputFile.getName());
- try {
- // 创建ProcessBuilder对象
- log.info("输入路径==="+inputFile.getAbsolutePath());
- // 等待命令执行完成
- String outName = "out"+CgpTool.generateUUID()+".mp4";
- String command = "ffmpeg -i " + inputFile.getAbsolutePath() + " -r 15 -b:v 600k -s 720x1280 -ss 00:00:00 -t 00:00:15 "+ outName;
- log.info("压缩方式==="+command);
- ProcessBuilder pb = new ProcessBuilder("ffmpeg","-i",inputFile.getAbsolutePath(),"-r","15","-b:v","600k","-s","720x1280","-ss","00:00:00","-t","00:00:15",outName);
- pb.redirectErrorStream(true);
- pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
- Process process = pb.start();
- process.waitFor();
- log.info("视频压缩完成===");
- return new File(outName);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (InterruptedException e){
- e.printStackTrace();
- }
- return null;
- }
- /**
- * 获取指定的视频文件后进行封面截图为jpg
- */
- public File generateCover(File inputFile) {
- log.info("视频截图开始==="+inputFile.getName());
- try {
- // 创建ProcessBuilder对象
- log.info("输入路径==="+inputFile.getAbsolutePath());
- // 截图保存位置
- String outName = "out"+CgpTool.generateUUID()+".jpg";
- String command = "ffmpeg -sseof -1 -i " + inputFile.getAbsolutePath() + " -vframes 1 "+ outName;
- log.info("封面截图方式==="+command);
- ProcessBuilder pb = new ProcessBuilder("ffmpeg","-sseof","-1","-i",inputFile.getAbsolutePath(),"-vframes","1",outName);
- pb.redirectErrorStream(true);
- pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
- Process process = pb.start();
- process.waitFor();
- log.info("封面截图完成===");
- return new File(outName);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (InterruptedException e){
- e.printStackTrace();
- }
- return null;
- }
- /**
- * 获取文件
- * @param fileUrl
- * @return
- */
- public String getFileFinger(String fileUrl) {
- // 创建OSSClient实例。
- OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, secretAccessKey);
- try {
- String url = fileUrl.replace("https://ptsubsidy.oss-cn-beijing.aliyuncs.com/", "");
- InputStream inputStream = ossClient.getObject(bucketName, url).getObjectContent();
- if(url.indexOf("mp4") != -1){
- File tempFile = File.createTempFile("temp_", ".mp4");
- FileUtils.copyInputStreamToFile(inputStream, tempFile);
- File file = this.generateCover(tempFile);
- tempFile.delete();
- String val = FingerImage.readImagePix(file);
- file.delete();
- return val;
- }
- return FingerImage.readImagePix(inputStream);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- } finally {
- if (ossClient != null) {
- ossClient.shutdown();
- }
- }
- }
- }
|