package com.jiayue.ipfcst.client.utils; import lombok.extern.slf4j.Slf4j; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.util.Properties; /** * @author bizy * @version 1.0 * @ClassName UtilTools * @Description * @date 2018/7/18 15:17 下午 * @Modified By */ @Slf4j public class UtilTools { public static Properties props = System.getProperties(); public static String osName = props.getProperty("os.name"); /** * 功能描述: 根据操作系统格式化路径 * * @param: fpath * @return: fpath * @auther: bizy * @date: 2018/8/6 9:19 上午 */ public static String buildFilePathBySystem(String fPath) { File directory = new File(""); String truePath = null; try { truePath = directory.getCanonicalPath(); } catch (IOException e) { log.error("系统错误:" + e.getMessage(), e); } if (osName.indexOf("Windows") > -1) { return truePath + fPath.replace("/", "\\\\"); } if (osName.indexOf("Linux") > -1) { return truePath + fPath.replace("\\\\", "/"); } return null; } public static void mvFile(String fsPath, String fdPath) { // File fs = new File("D:\\迅雷下载\\f31.mp4"); // File fd = new File("D:\\"); //源文件 File fs = new File(fsPath); //目标目录 File fd = new File(fdPath); //判断目标目录是否存在同名文件,存在则删除 File md = new File(fdPath + fs.getName()); if (md.exists()) { md.delete(); } try { log.info("移动下载文件{}到{}",fs,fd); org.apache.commons.io.FileUtils.copyFileToDirectory(fs, fd, true); if("是".equals(Constant.cacheClientConfig.getRenameFile())) { log.info("使用最新科东反向隔离,需要对文件重命名"); String newFileName = fdPath + fs.getName().substring(0, fs.getName().length() - 6) + "999.RB"; log.info("睡眠5秒后修改文件名{}为{}", md, newFileName); Thread.sleep(1000); md.renameTo(new File(newFileName)); }else if("南瑞(2.1.6)".equals(Constant.cacheClientConfig.getRenameFile())){ /** * 升级到2.1.6版本 * 1、只能传输E文本 * 2、文件名(包括后缀)不能超过256字节 * 3、文件后缀只能是".txt"、".TXT"、".cime"、".CIME"或无后缀名 * 4、文件名仅允许使用中文(包含全角符号),半角的英文大小写字母、数字和下划线 */ log.info("使用南瑞(2.1.6)反向隔离,需要对文件重命名,转换后缀RB->txt"); String newFileName = fdPath + fs.getName().substring(0, fs.getName().length() - 6) + "999.txt"; log.info("睡眠5秒后修改文件名{}为{}", md, newFileName); Thread.sleep(1000); md.renameTo(new File(newFileName)); } fs.delete(); } catch (Exception e) { log.error("文件移动错误", e); } } /** * 功能描述: 判断文件夹是否存在 * * @param: * @return: * @auther: bizy * @date: 2018/7/18 15:47 下午 */ public static String judeDirExists(String path) { try { if (osName.indexOf("Windows") > -1) { path = path.replaceAll("/", "\\\\"); } else { path = path.replaceAll("\\\\", "/"); path = "/" + path; } File file = new File(path); if (file.exists()) { if (file.isDirectory()) { log.info("dir exists"); } else { log.info("the same name file exists, can not create dir"); } } else { log.info("dir not exists, create it ..."); File parent = file.getParentFile(); // 获取父文件 if (!parent.exists()) { parent.mkdirs(); //创建所有父文件夹 } file.mkdir(); if (osName.indexOf("Windows") <= -1) { Runtime.getRuntime().exec("chmod 777 -R " + path);//ftp服务器在linux环境中,生成的文件涉及到读写权限问题,直接给777权限 } } log.info("判断路径path:{} 是否存在", path); } catch (IOException e) { log.error("系统错误:" + e.getMessage(), e); } finally { return path; } } /** * 功能描述: 获取文件下载路径 * * @param: * @return: * @auther: bizy * @date: 2018/7/18 15:47 下午 */ public static String getDownLoadPath() { if (osName.indexOf("Windows") > -1) { return Constant.FTP_DOWNLOAD_PATH_WIN; } else { return Constant.FTP_DOWNLOAD_PATH_LINUX; } } /** * 功能描述: 获取异常堆栈信息 * * @param: Throwable * @return: String * @auther: bizy * @date: 2018/8/9 11:29 上午 */ public static String printStackTraceToString(Throwable t) { StringWriter sw = new StringWriter(); t.printStackTrace(new PrintWriter(sw, true)); return sw.getBuffer().toString(); } /** * 动态给String数组添加String * * @param arr 数据集合 * @param str 需要插入的字符串 * @return String[] 返回的数组集合 */ public static String[] insert(String[] arr, String str) { int size = arr.length; for (int i = arr.length - 1; i >= 0; i--) { if (arr[i].trim().isEmpty()) { size = size - 1; } } String[] tmp = new String[size + 1]; System.arraycopy(arr, 0, tmp, 0, size); tmp[size] = str; return tmp; } public static void main(String[] args) { String newFileName = "DQ_202110240715440.RB"; System.out.println(newFileName.substring(0,newFileName.length()-6)+"999.RB"); //System.out.println(newFileName.replace(newFileName.charAt(newFileName.length()-6)+"","999.RB")); } }