1
0

ServiceFileUtil.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package com.siwei.apply.utils;
  2. import com.siwei.common.core.exception.ServiceException;
  3. import com.siwei.common.core.utils.DateUtils;
  4. import com.siwei.common.core.utils.StringUtils;
  5. import com.siwei.common.core.utils.uuid.Seq;
  6. import org.apache.commons.io.FilenameUtils;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import java.io.IOException;
  10. import java.nio.file.Files;
  11. import java.nio.file.Path;
  12. import java.nio.file.Paths;
  13. import java.nio.file.StandardCopyOption;
  14. import java.util.Arrays;
  15. /**
  16. * 文件工具类
  17. */
  18. public class ServiceFileUtil {
  19. private static final Logger logger = LoggerFactory.getLogger(ServiceFileUtil.class);
  20. private static final String createFilePath = "/home/siwei/uploadPath";
  21. /**
  22. * 编码目录名
  23. */
  24. public static final String extractFilename(String name)
  25. {
  26. return StringUtils.format("{}/{}_{}", DateUtils.datePath(),
  27. FilenameUtils.getBaseName(name), Seq.getId(Seq.uploadSeqType));
  28. }
  29. //给定一个名称创建一个目录,返回路径
  30. public static String createDirectory(String directoryName) {
  31. String directoryPathAndName = null;
  32. try {
  33. directoryPathAndName =createFilePath+"/"+ extractFilename(directoryName);
  34. Path file = Paths.get(directoryPathAndName);
  35. if (Files.exists(file)) {
  36. logger.warn("处理文件失败:文件不存在: {}", directoryPathAndName);
  37. }else {
  38. //直接创建目录
  39. Path extractPath = Paths.get(directoryPathAndName);
  40. Files.createDirectories(extractPath);
  41. }
  42. } catch (IOException e) {
  43. throw new ServiceException("创建目录失败,请检查!"+e.getMessage());
  44. }
  45. return directoryPathAndName;
  46. }
  47. //给定一个名称创建一个目录,返回路径
  48. public static String createMultipartDirectory(String directoryName,String[] subDirectoryName) {
  49. String directoryPath = createFilePath;
  50. directoryName = extractFilename(directoryName);
  51. String directoryPathAndName = createDirectory(directoryPath,directoryName);
  52. Arrays.stream(subDirectoryName).forEach(name -> {
  53. createDirectory(directoryPathAndName,name);
  54. });
  55. return directoryPath+"/"+ directoryName;
  56. }
  57. //给定一个名称创建一个目录,返回路径
  58. public static String createDirectory(String directoryPath, String directoryName) {
  59. String directoryPathAndName = null;
  60. try {
  61. directoryPathAndName =directoryPath+"/"+ directoryName;
  62. Path file = Paths.get(directoryPathAndName);
  63. if (Files.exists(file)) {
  64. logger.warn("处理文件失败:文件不存在: {}", directoryPathAndName);
  65. }else {
  66. //直接创建目录
  67. Path extractPath = Paths.get(directoryPathAndName);
  68. Files.createDirectories(extractPath);
  69. }
  70. } catch (IOException e) {
  71. throw new ServiceException("创建目录失败,请检查!"+e.getMessage());
  72. }
  73. return directoryPathAndName;
  74. }
  75. //给定一个名称创建一个目录,返回路径
  76. public static String moveFileToDirectory(String filePath,String directoryPath) {
  77. Path file = Paths.get(filePath);
  78. Path dirPath = Paths.get(directoryPath);
  79. if (!Files.exists(file)) {
  80. logger.warn("处理文件失败,文件不存在: {}",filePath);
  81. throw new ServiceException("处理文件失败:文件不存在: "+filePath);
  82. }
  83. if (!Files.exists(dirPath)) {
  84. logger.warn("目录不存在: {}", directoryPath);
  85. throw new ServiceException("目录不存在: "+directoryPath);
  86. }
  87. //这里拼接文件名
  88. if(directoryPath.endsWith("/")){
  89. directoryPath+=file.getFileName();
  90. }else {
  91. directoryPath+="/"+file.getFileName();
  92. }
  93. dirPath = Paths.get(directoryPath);
  94. try {
  95. //Files.copy(file, dirPath, StandardCopyOption.REPLACE_EXISTING);
  96. Files.move(file, dirPath, StandardCopyOption.REPLACE_EXISTING);
  97. } catch (IOException e) {
  98. throw new RuntimeException(e);
  99. }
  100. return directoryPath;
  101. }
  102. }