FileRemoveUtils.java 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package com.zskk.dicom.monitor.utils;
  2. import com.zskk.dicom.monitor.config.Configs;
  3. import com.zskk.dicom.monitor.report.ErrReporter;
  4. import org.apache.commons.io.FileUtils;
  5. import org.apache.http.util.TextUtils;
  6. import java.io.File;
  7. import java.io.FileNotFoundException;
  8. import java.io.IOException;
  9. /**
  10. * 文件移动帮助类
  11. */
  12. public class FileRemoveUtils {
  13. public static boolean remove(String sourceFilePath, String targetFilePath) {
  14. if(TextUtils.isEmpty(sourceFilePath) || TextUtils.isEmpty(targetFilePath)) {
  15. Configs.sysLog.warn("remove error: sourceFilePath or targetFilePath is empty \tsourceFilePath: " + sourceFilePath + "\ttargetFilePath: " + targetFilePath);
  16. return false;
  17. }
  18. File sourceFile = new File(sourceFilePath);
  19. if(!sourceFile.exists() || !sourceFile.isFile()) {
  20. Configs.sysLog.warn("remove error: sourceFile not exists or not isFile \tsourceFilePath: " + sourceFilePath);
  21. return false;
  22. }
  23. Configs.sysLog.info("remove file \tsourceFilePath:" + sourceFilePath + "\ttargetFilePath:" + targetFilePath);
  24. File targetFile = new File(targetFilePath);
  25. return remove(sourceFile, targetFile);
  26. }
  27. private static boolean remove(File sourceFile, File targetFile) {
  28. String targetFileStr = targetFile.getAbsolutePath();
  29. if (targetFile.exists()) {
  30. // 如果移动的文件,已经在目标地址存在 那么,比对Hash值,看是否为同一个文件 如果是同一个文件,只需要删除当前文件,不必移除 如果不是同一个文件,另备份目录
  31. Configs.sysLog.info("备份目标文件已经存在,需要比对文件Hash值");
  32. String tHash = FileHashUtil.getFileMD5(targetFile);
  33. String sHash = FileHashUtil.getFileMD5(sourceFile);
  34. Configs.sysLog.info(sourceFile.getAbsolutePath() + "[" + sHash + "] ----->>----- " + targetFile.getAbsolutePath() + "[" + tHash + "]");
  35. if (tHash.equals(sHash)) {
  36. // 2个文件的Hash值相同 不再移动文件,删除原文件
  37. boolean isSucDel = sourceFile.delete();
  38. Configs.sysLog.info("删除源文件:" + sourceFile.getAbsolutePath() + " 结果:" + isSucDel);
  39. if (!isSucDel) {
  40. // 文件没有成功删除
  41. ErrReporter.report("文件删除失败:" + sourceFile.getAbsolutePath());
  42. }
  43. return isSucDel;
  44. } else {
  45. // 2个文件的Hash值不同
  46. targetFile = new File(targetFileStr + "2");
  47. if (targetFile.exists()) {
  48. // 如果备份文件也存在, 先删除,再将源文件移至此文件
  49. targetFile.delete();
  50. }
  51. }
  52. }
  53. boolean flag = false;
  54. try {
  55. FileUtils.moveFile(sourceFile, targetFile);
  56. flag = true;
  57. Configs.sysLog.info("文件移动-----------:" + sourceFile.getCanonicalPath() + " to:" + targetFile.getAbsolutePath() + " 成功");
  58. } catch (IOException ioe) {
  59. if (ioe instanceof FileNotFoundException) {
  60. // 源文件已不存在
  61. Configs.sysLog.info("文件移动------------:" + sourceFile.getAbsolutePath() + " to:" + targetFile.getAbsolutePath() + " " + ioe.getMessage());
  62. } else {
  63. // 文件没有成功删除
  64. ErrReporter.report("文件移动失败----------:" + sourceFile.getAbsolutePath() + " to:" + targetFileStr + " " + ioe.getMessage());
  65. }
  66. }
  67. return flag;
  68. }
  69. }