FilesUtil.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package com.jiayue.ipfcst.spare2.util;
  2. import cn.hutool.core.io.FileUtil;
  3. import com.jiayue.ipfcst.common.data.util.CommonDataUtil;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.apache.commons.io.FileUtils;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.nio.file.Files;
  9. import java.nio.file.LinkOption;
  10. import java.nio.file.Path;
  11. import java.nio.file.Paths;
  12. import java.nio.file.attribute.BasicFileAttributeView;
  13. import java.nio.file.attribute.BasicFileAttributes;
  14. import java.security.MessageDigest;
  15. import java.util.Date;
  16. /**
  17. * 文件工具类
  18. *
  19. * @author xsl
  20. * @version 3.0
  21. */
  22. @Slf4j
  23. public class FilesUtil extends CommonDataUtil {
  24. private static final String imageFileSuffix = "txt";
  25. public static <T> String imageFileName(Class<T> clazz) {
  26. return clazz.getSimpleName() + "." + imageFileSuffix;
  27. }
  28. /**
  29. * 移动本地文件
  30. *
  31. * @param remoteAbsoluteFile 远程文件名(包括完整路径)
  32. * @param localAbsoluteFile 本地文件名(包括完整路径)
  33. * @return 成功时,返回true,失败返回false
  34. * @throws Exception
  35. */
  36. public static void move(String localAbsoluteFile, String remoteAbsoluteFile) throws Exception {
  37. move(localAbsoluteFile, remoteAbsoluteFile, true);
  38. }
  39. public static void move(String localAbsoluteFile, String remoteAbsoluteFile, boolean overwrite) throws Exception {
  40. File srcFile = new File(localAbsoluteFile);
  41. File destDir = new File(remoteAbsoluteFile);
  42. StringBuilder opts = new StringBuilder();
  43. try {
  44. if (!destDir.exists()) {// 如果目录不存在则创建目录
  45. boolean b = destDir.mkdirs();
  46. if (!b) // 如果创建失败则抛出异常
  47. throw new RuntimeException(destDir + " 目录创建失败");
  48. }
  49. File desFile = new File(remoteAbsoluteFile + File.separator + srcFile.getName());
  50. if (overwrite && desFile.exists()) {
  51. FileUtils.deleteQuietly(desFile);
  52. opts.append(" >> 覆盖 >> ");
  53. } else {
  54. opts.append(" >> 移动 >> ");
  55. }
  56. FileUtils.moveFile(srcFile, desFile);
  57. log.info("文件:" + srcFile.getName() + opts + destDir.getPath());
  58. } catch (IOException e) {
  59. throw new Exception("文件:" + srcFile.getName() + opts + destDir.getPath() + "失败。", e);
  60. }
  61. }
  62. //文件内容md5加密
  63. public static String getMD5(byte[] fileByte) {
  64. String md5 = new String();
  65. try {
  66. MessageDigest md = MessageDigest.getInstance("MD5");
  67. md.update(fileByte);
  68. byte b[] = md.digest();
  69. int i;
  70. StringBuffer buf = new StringBuffer("");
  71. for (int offset = 0; offset < b.length; offset++) {
  72. i = b[offset];
  73. if (i < 0)
  74. i += 256;
  75. if (i < 16)
  76. buf.append("0");
  77. buf.append(Integer.toHexString(i));
  78. }
  79. md5 = buf.toString();
  80. } catch (Exception e) {
  81. e.printStackTrace();
  82. }
  83. return md5;
  84. }
  85. /**
  86. * 获取文件创建时间
  87. *
  88. * @param fullFileName
  89. * @return
  90. */
  91. public static Long getFileCreateTime(String fullFileName) {
  92. Path path = Paths.get(fullFileName);
  93. BasicFileAttributeView basicview = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
  94. BasicFileAttributes attr;
  95. try {
  96. attr = basicview.readAttributes();
  97. Date createDate = new Date(attr.creationTime().toMillis());
  98. return createDate.getTime();
  99. } catch (Exception e) {
  100. e.printStackTrace();
  101. }
  102. return new Date().getTime();
  103. }
  104. /**
  105. * 检查uploadfile目录下是否生成过文件{name}
  106. * @param name
  107. * @return
  108. */
  109. public static boolean checkUpGened(String name) {
  110. return FileUtil.exist(getFileUploadPath(), name);
  111. }
  112. }