UploadService.java 4.1 KB

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