UploadService.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package com.fdkankan.fusion.service.impl;
  2. import com.fdkankan.fusion.common.FilePath;
  3. import com.fdkankan.fusion.common.ResultCode;
  4. import com.fdkankan.fusion.common.util.ShellUtil;
  5. import com.fdkankan.fusion.common.util.UploadToOssUtil;
  6. import com.fdkankan.fusion.common.util.VideoUtil;
  7. import com.fdkankan.fusion.exception.BusinessException;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.apache.commons.lang3.StringUtils;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.web.multipart.MultipartFile;
  13. import sun.rmi.runtime.Log;
  14. import javax.annotation.Resource;
  15. import java.io.File;
  16. import java.io.IOException;
  17. import java.util.LinkedHashSet;
  18. import java.util.UUID;
  19. @Service
  20. @Slf4j
  21. public class UploadService {
  22. @Resource
  23. private UploadToOssUtil uploadToOssUtil;
  24. @Value("${upload.query-path}")
  25. private String queryPath;
  26. @Value("${spring.profiles.active}")
  27. private String environment;
  28. public String uploadFile(MultipartFile file, Integer videoFolderId) {
  29. if(file.isEmpty()){
  30. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  31. }
  32. if(file.getSize()>10 * 1024 * 1024 * 100){
  33. System.out.println(file.getSize());
  34. throw new BusinessException(ResultCode.UPLOAD_FILE_TO_LONG);
  35. }
  36. //获取文件名
  37. String fileName = file.getOriginalFilename();
  38. if(StringUtils.isEmpty(fileName)){
  39. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  40. }
  41. File localFile = null;
  42. try {
  43. //获取文件后缀名
  44. String suffixName = fileName.substring(fileName.lastIndexOf("."));
  45. //重新生成文件名
  46. fileName = UUID.randomUUID().toString().replace("-","") ;
  47. localFile = new File(String.format(FilePath.VIDEO_LOCAL_PATH,environment) +"/"+videoFolderId +"/"+fileName + suffixName );
  48. if(!localFile.getParentFile().exists()){
  49. localFile.getParentFile().mkdirs();
  50. }
  51. file.transferTo(localFile);
  52. return localFile.getPath();
  53. }catch (Exception e){
  54. e.printStackTrace();
  55. }finally {
  56. if(localFile!=null){
  57. localFile.deleteOnExit(); //退出删除,后续需要使用文件
  58. }
  59. }
  60. return null;
  61. }
  62. public String uploadFile(MultipartFile file, boolean newName, String filePathAdd) {
  63. if(file.isEmpty()){
  64. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  65. }
  66. if(file.getSize()>10 * 1024 * 1024 * 100){
  67. System.out.println(file.getSize());
  68. throw new BusinessException(ResultCode.UPLOAD_FILE_TO_LONG);
  69. }
  70. //获取文件名
  71. String fileName = file.getOriginalFilename();
  72. if(StringUtils.isEmpty(fileName)){
  73. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  74. }
  75. File localFile = null;
  76. try {
  77. //获取文件后缀名
  78. String suffixName = fileName.substring(fileName.lastIndexOf("."));
  79. //重新生成文件名
  80. if(newName){
  81. fileName = UUID.randomUUID().toString().replace("-","") ;
  82. }else {
  83. fileName= fileName.substring(0,fileName.lastIndexOf("."));
  84. }
  85. localFile = File.createTempFile(fileName + suffixName,suffixName);
  86. file.transferTo(localFile);
  87. String path = localFile.getPath();
  88. uploadToOssUtil.uploadOss(path,filePathAdd+ fileName + suffixName);
  89. if(!uploadToOssUtil.existKey(filePathAdd + fileName + suffixName)){
  90. throw new BusinessException(ResultCode.UPLOAD_ERROR.code,ResultCode.UPLOAD_ERROR.msg);
  91. }
  92. return queryPath +filePathAdd+ fileName + suffixName;
  93. }catch (Exception e){
  94. log.info("upload-error:{}",e);
  95. throw new BusinessException(ResultCode.UPLOAD_ERROR.code,ResultCode.UPLOAD_ERROR.msg);
  96. }finally {
  97. if(localFile!=null){
  98. localFile.delete();
  99. }
  100. }
  101. }
  102. public void deleteOssUrl(String path) {
  103. try {
  104. String replace = path.replace(queryPath, "");
  105. uploadToOssUtil.delete(replace);
  106. }catch (Exception e){
  107. e.printStackTrace();
  108. }
  109. }
  110. }