UploadService.java 4.3 KB

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