LocalToOssUtil.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package com.fdkankan.fusion.common.util;
  2. import cn.hutool.core.io.FileUtil;
  3. import com.fdkankan.fusion.config.CacheUtil;
  4. import com.fdkankan.fusion.response.FileInfoVo;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.apache.commons.io.FileUtils;
  7. import org.springframework.beans.factory.annotation.Value;
  8. import org.springframework.stereotype.Component;
  9. import java.io.File;
  10. import java.nio.charset.Charset;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.stream.Collectors;
  14. @Component
  15. @Slf4j
  16. public class LocalToOssUtil {
  17. @Value("${oss.bucket:4dkankan}")
  18. private String bucket;
  19. private String getOssPath(String bucket, String filePath) {
  20. return CacheUtil.basePath.concat(bucket).concat(File.separator).concat(filePath);
  21. }
  22. public String getOssPath( String filePath) {
  23. return CacheUtil.basePath.concat(bucket).concat(File.separator).concat(filePath);
  24. }
  25. public boolean existKey(String objectName) {
  26. return FileUtil.exist(getOssPath(bucket, objectName));
  27. }
  28. public boolean downFormRel(String objectName, String localPath) {
  29. try {
  30. FileUtil.copyContent(new File(getOssPath(bucket, objectName)), new File(localPath), true);
  31. }catch (Exception e){
  32. log.info("下载文件失败,remoteFilePath:{},localPath:{}", objectName, localPath);
  33. log.info("下载文件失败", e);
  34. return false;
  35. }
  36. return true;
  37. }
  38. public boolean downFormAbs(String objectName, String localPath) {
  39. try {
  40. FileUtil.copyContent(new File( objectName), new File(localPath), true);
  41. }catch (Exception e){
  42. log.info("下载文件失败,remoteFilePath:{},localPath:{}", objectName, localPath);
  43. log.info("下载文件失败", e);
  44. return false;
  45. }
  46. return true;
  47. }
  48. public String uploadFile(String bucket, String filePath, String remoteFilePath, Map<String, String> headers) {
  49. try {
  50. if (!new File(filePath).exists()) {
  51. log.warn("文件不存在:{}", filePath);
  52. return null;
  53. }
  54. FileUtil.copyContent(new File(filePath), new File(getOssPath(bucket, remoteFilePath)), true);
  55. //FileUtils.contentEqualsIgnoreEOL(new File(filePath),new File(getOssPath(bucket, remoteFilePath)), "UTF-8");
  56. }catch (Exception e){
  57. log.error("上传文件失败,filePath:{},remoteFilePath:{}", filePath, remoteFilePath);
  58. log.error("上传文件失败", e);
  59. }
  60. return null;
  61. }
  62. public void uploadOss(String filePath, String key1) {
  63. uploadFile(bucket, filePath, key1, null);
  64. }
  65. public void delete(String objectName) {
  66. FileUtil.del(getOssPath(bucket, objectName));
  67. }
  68. public void uploadFileOss(File file) {
  69. if(file.isFile()){
  70. String ossPath = file.getPath();
  71. ossPath = ossPath.replace("/mnt/","");
  72. ossPath = ossPath.replace("\\","/");
  73. this.uploadOss(file.getPath(),ossPath);
  74. }else {
  75. File[] files = file.listFiles();
  76. for (File file1 : files) {
  77. uploadFileOss(file1);
  78. }
  79. }
  80. }
  81. public List<String> listKeysFromAli(String sourcePath) {
  82. return FileUtil.loopFiles(getOssPath(bucket, sourcePath)).stream()
  83. .map(f -> f.getAbsolutePath().replace(CacheUtil.basePath.concat(bucket).concat(File.separator), ""))
  84. .collect(Collectors.toList());
  85. }
  86. public void copyFile(String sourcePath, String targetPath) {
  87. try {
  88. FileUtil.copyContent(new File(getOssPath(bucket, sourcePath)), new File(getOssPath(bucket, targetPath)), true);
  89. }catch (Exception e){
  90. log.error("复制文件失败,sourcePath:{},targetPath:{}",sourcePath,targetPath);
  91. log.error("复制文件失败", e);
  92. }
  93. }
  94. public FileInfoVo getFileInfo(String filePath) {
  95. return MD5Checksum.getFileInfo(getOssPath(bucket, filePath));
  96. }
  97. public Long getSizeCount(String filePath) {
  98. log.info("getSize:{}",filePath);
  99. File file = new File(filePath);
  100. if(file.isFile()){
  101. return file.length();
  102. }
  103. File[] files = file.listFiles();
  104. if(files == null){
  105. return 0L;
  106. }
  107. Long size = 0L;
  108. for (File file1 : files) {
  109. size+= getSizeCount(file1.getPath());
  110. }
  111. return size;
  112. }
  113. public Long getSize(String filePath) {
  114. return getSizeCount( getOssPath(bucket, filePath));
  115. }
  116. public String getObjectContent(String s) {
  117. return FileUtil.readUtf8String(new File(getOssPath(s)));
  118. }
  119. }