UtilTools.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package com.jiayue.ipfcst.client.utils;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6. import java.io.StringWriter;
  7. import java.util.Properties;
  8. /**
  9. * @author bizy
  10. * @version 1.0
  11. * @ClassName UtilTools
  12. * @Description
  13. * @date 2018/7/18 15:17 下午
  14. * @Modified By
  15. */
  16. @Slf4j
  17. public class UtilTools {
  18. public static Properties props = System.getProperties();
  19. public static String osName = props.getProperty("os.name");
  20. /**
  21. * 功能描述: 根据操作系统格式化路径
  22. *
  23. * @param: fpath
  24. * @return: fpath
  25. * @auther: bizy
  26. * @date: 2018/8/6 9:19 上午
  27. */
  28. public static String buildFilePathBySystem(String fPath) {
  29. File directory = new File("");
  30. String truePath = null;
  31. try {
  32. truePath = directory.getCanonicalPath();
  33. } catch (IOException e) {
  34. log.error("系统错误:" + e.getMessage(), e);
  35. }
  36. if (osName.indexOf("Windows") > -1) {
  37. return truePath + fPath.replace("/", "\\\\");
  38. }
  39. if (osName.indexOf("Linux") > -1) {
  40. return truePath + fPath.replace("\\\\", "/");
  41. }
  42. return null;
  43. }
  44. public static void mvFile(String fsPath, String fdPath) {
  45. // File fs = new File("D:\\迅雷下载\\f31.mp4");
  46. // File fd = new File("D:\\");
  47. //源文件
  48. File fs = new File(fsPath);
  49. //目标目录
  50. File fd = new File(fdPath);
  51. //判断目标目录是否存在同名文件,存在则删除
  52. File md = new File(fdPath + fs.getName());
  53. if (md.exists()) {
  54. md.delete();
  55. }
  56. try {
  57. log.info("移动下载文件{}到{}",fs,fd);
  58. org.apache.commons.io.FileUtils.copyFileToDirectory(fs, fd, true);
  59. if("是".equals(Constant.cacheClientConfig.getRenameFile())) {
  60. log.info("使用最新科东反向隔离,需要对文件重命名");
  61. String newFileName = fdPath + fs.getName().substring(0, fs.getName().length() - 6) + "999.RB";
  62. log.info("睡眠5秒后修改文件名{}为{}", md, newFileName);
  63. Thread.sleep(1000);
  64. md.renameTo(new File(newFileName));
  65. }else if("南瑞(2.1.6)".equals(Constant.cacheClientConfig.getRenameFile())){
  66. /**
  67. * 升级到2.1.6版本
  68. * 1、只能传输E文本
  69. * 2、文件名(包括后缀)不能超过256字节
  70. * 3、文件后缀只能是".txt"、".TXT"、".cime"、".CIME"或无后缀名
  71. * 4、文件名仅允许使用中文(包含全角符号),半角的英文大小写字母、数字和下划线
  72. */
  73. log.info("使用南瑞(2.1.6)反向隔离,需要对文件重命名,转换后缀RB->txt");
  74. String newFileName = fdPath + fs.getName().substring(0, fs.getName().length() - 6) + "999.txt";
  75. log.info("睡眠5秒后修改文件名{}为{}", md, newFileName);
  76. Thread.sleep(1000);
  77. md.renameTo(new File(newFileName));
  78. }
  79. fs.delete();
  80. } catch (Exception e) {
  81. log.error("文件移动错误", e);
  82. }
  83. }
  84. /**
  85. * 功能描述: 判断文件夹是否存在
  86. *
  87. * @param:
  88. * @return:
  89. * @auther: bizy
  90. * @date: 2018/7/18 15:47 下午
  91. */
  92. public static String judeDirExists(String path) {
  93. try {
  94. if (osName.indexOf("Windows") > -1) {
  95. path = path.replaceAll("/", "\\\\");
  96. } else {
  97. path = path.replaceAll("\\\\", "/");
  98. path = "/" + path;
  99. }
  100. File file = new File(path);
  101. if (file.exists()) {
  102. if (file.isDirectory()) {
  103. log.info("dir exists");
  104. } else {
  105. log.info("the same name file exists, can not create dir");
  106. }
  107. } else {
  108. log.info("dir not exists, create it ...");
  109. File parent = file.getParentFile(); // 获取父文件
  110. if (!parent.exists()) {
  111. parent.mkdirs(); //创建所有父文件夹
  112. }
  113. file.mkdir();
  114. if (osName.indexOf("Windows") <= -1) {
  115. Runtime.getRuntime().exec("chmod 777 -R " + path);//ftp服务器在linux环境中,生成的文件涉及到读写权限问题,直接给777权限
  116. }
  117. }
  118. log.info("判断路径path:{} 是否存在", path);
  119. } catch (IOException e) {
  120. log.error("系统错误:" + e.getMessage(), e);
  121. } finally {
  122. return path;
  123. }
  124. }
  125. /**
  126. * 功能描述: 获取文件下载路径
  127. *
  128. * @param:
  129. * @return:
  130. * @auther: bizy
  131. * @date: 2018/7/18 15:47 下午
  132. */
  133. public static String getDownLoadPath() {
  134. if (osName.indexOf("Windows") > -1) {
  135. return Constant.FTP_DOWNLOAD_PATH_WIN;
  136. } else {
  137. return Constant.FTP_DOWNLOAD_PATH_LINUX;
  138. }
  139. }
  140. /**
  141. * 功能描述: 获取异常堆栈信息
  142. *
  143. * @param: Throwable
  144. * @return: String
  145. * @auther: bizy
  146. * @date: 2018/8/9 11:29 上午
  147. */
  148. public static String printStackTraceToString(Throwable t) {
  149. StringWriter sw = new StringWriter();
  150. t.printStackTrace(new PrintWriter(sw, true));
  151. return sw.getBuffer().toString();
  152. }
  153. /**
  154. * 动态给String数组添加String
  155. *
  156. * @param arr 数据集合
  157. * @param str 需要插入的字符串
  158. * @return String[] 返回的数组集合
  159. */
  160. public static String[] insert(String[] arr, String str) {
  161. int size = arr.length;
  162. for (int i = arr.length - 1; i >= 0; i--) {
  163. if (arr[i].trim().isEmpty()) {
  164. size = size - 1;
  165. }
  166. }
  167. String[] tmp = new String[size + 1];
  168. System.arraycopy(arr, 0, tmp, 0, size);
  169. tmp[size] = str;
  170. return tmp;
  171. }
  172. public static void main(String[] args) {
  173. String newFileName = "DQ_202110240715440.RB";
  174. System.out.println(newFileName.substring(0,newFileName.length()-6)+"999.RB");
  175. //System.out.println(newFileName.replace(newFileName.charAt(newFileName.length()-6)+"","999.RB"));
  176. }
  177. }