lyh
2 天以前 2ab86210fb27787cb1be8976286b9b827f90997f
src/main/java/com/lxzn/base/service/impl/DncPassLogServiceImpl.java
@@ -79,209 +79,209 @@
        return super.baseMapper.findDateTimeDay(dateDay);
    }
    @Override
    @Scheduled(cron = "${fileCron}")
    public void fileClientTxtOrNc() {
        SysLogMessageDto message = new SysLogMessageDto();
        String host = "127.0.0.1";
        if (StringUtils.isNotBlank(serverIp)) {
            host = serverIp;
        }
        String port = "8299";
        if (StringUtils.isNotBlank(serverPort)) {
            port = serverPort;
        }
        String username = "admin";
        if (StringUtils.isNotBlank(usernameService)) {
            username = usernameService;
        }
        String pwd = "123";
        if (StringUtils.isNotBlank(pwdService)) {
            pwd = pwdService;
        }
        //文件本地地址,根据地址找到文件,将文件解析成文件流
        String localFilePath = "E:\\test\\a\\";
        if (StringUtils.isNotBlank(localFilePathC)) {
            localFilePath = localFilePathC;
        }
        //文件上传目的地址,将文件上传到该地址
        String servicePath = "E:\\test\\b\\";
        if (StringUtils.isNotBlank(servicePathS)) {
            servicePath = servicePathS;
        }
        boolean btelnetPort =  TelnetUtil.telnetPort(serverIp,Integer.valueOf(serverPort),10);
        if (!btelnetPort) {
            System.out.println("服务器连接: " + serverIp + ":" + serverPort + " 异常!" );
            return;
        }
        //第一步,获取token
        String token = null;
        try {
            token = FileClient.getToken(host,port,username,pwd,addressToken);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        //若获取token成功,再进行上传文件接口调用
        if (token !=null && !token.equals("")){
            try {
                File f3 = new File(localFilePath);
                File[] files = f3.listFiles();
                for (File fi : files){
                    if (fi.isFile()){
                        SysLogTypeObjectDto objectName = new SysLogTypeObjectDto();
                        objectName.setDateTime(DateUtil.format(DateUtil.getNow(),DateUtil.STR_DATE_TIME_SMALL));
                        objectName.setFileName(fi.getName());
                        objectName.setFileSize(FileUtil.changeFileFormatKb(String.valueOf(new File(localFilePath).length())));
                        objectName.setSourceAddress(localFilePath);
                        objectName.setDestination(serverIp);//服务端IP
                        //顺序号
                        DncPassLog passLog = getById("num0001");
                        if (passLog == null) {
                            passLog = new DncPassLog();
                            passLog.setId("num0001");
                            passLog.setSequenceNumber(1);
                            objectName.setFileNum(Integer.toString(1));
                            save(passLog);
                        } else {
                            Integer number = passLog.getSequenceNumber()+1;
                            removeById("num0001");
                            objectName.setFileNum(Integer.toString(number));
                            passLog = new DncPassLog();
                            passLog.setId("num0001");
                            passLog.setSequenceNumber(number);
                            save(passLog);
                        }
                        InetAddress address = null;
                        try {
                            address = InetAddress.getLocalHost();
                            String ip = address.getHostAddress();
                            objectName.setSourceAddress("20.10.17.11");//客户端IP
                            objectName.setAddress(ip);
                        } catch (UnknownHostException e) {
                            objectName.setSourceAddress("127.0.0.1");
                            objectName.setAddress("127.0.0.1");
                        }
                        objectName.setDestination(host);
                        objectName.setResult("失败");
                        //获取某个文件下的所有文件
                        String loFilePath = localFilePath + fi.getName();
                        String servicePathName =servicePath+"//" + fi.getName();
                        File file = new File(loFilePath);
                        if (file == null || !file.exists()){
                            objectName.setAbstract1("sm3");
                        } else {
                            String sm3 = SmUtil.sm3(file);
                            objectName.setAbstract1(sm3);
                        }
                        String b  = FileClient.uploadFile(host,port,token,fi.getName(),servicePathName,loFilePath,addressUploadFile);
                        //文件备份删除
                        if (b == null) {
                            try {
                                objectName.setResult("失败");
                                objectName.setTypes("error");
                                SyslogClient.sendClient(logIp,Integer.valueOf(logPort),objectName.toString());
                            }catch (Exception e) {
                                return;
                            }
                        }
                        else if ( b.equals("成功")) {
                            //备份数据
                            boolean fCopy = FileUtil.copyNcFile(loFilePath,newFilePathC + "/" + DateUtil.format(DateUtil.getNow(),
                                    DateUtil.STR_YEARMONTHDAY) + "/" +fi.getName());
                            if (fCopy) {
                                FileUtil.deleteNcFile(loFilePath);
                            } else {
                                FileUtil.copyNcFile(loFilePath,newFilePathC + "/" + DateUtil.format(DateUtil.getNow(),
                                        DateUtil.STR_YEARMONTHDAY) + "/" +fi.getName());
                                FileUtil.deleteNcFile(loFilePath);
                            }
                            try {
                                objectName.setTypes("Info");
                                objectName.setResult("成功");
                                SyslogClient.sendClient(logIp,Integer.valueOf(logPort),objectName.toString());
                            }catch (Exception e) {
                                return;
                            }
                        } else {
                            try {
                                objectName.setResult("失败");
                                objectName.setTypes("error");
                                SyslogClient.sendClient(logIp,Integer.valueOf(logPort),objectName.toString());
                            }catch (Exception e) {
                                return;
                            }
                        }
                    }
                }
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }
    /**
     * 定时任务执行器参数  每天凌晨1.30执行
     */
    @Override
    @Scheduled(cron = "0 30 1 * * ?")
    public void deleteNcSendFile() {
        Integer day;
        //取字典时间
        List<SingleDictionary> singleDictionaries=iSingleDictionaryService.findListFromTypeCode("DEL_TIME_SEND");
        if (singleDictionaries.size()!=1){
            return;
        }else {
            day= singleDictionaries.get(0).getDicValue();
        }
        Path ncDirectory = Paths.get("D:\\NC");
        //天转秒
        long dayToSecond = day * 24 * 60 * 60;
        if (Files.exists(ncDirectory) && Files.isDirectory(ncDirectory)) {
            // 递归查找NC目录下所有名为send的文件夹
            try (Stream<Path> sendFolders = Files.walk(ncDirectory)
                    .filter(Files::isDirectory)
                    .filter(path -> path.getFileName().toString().equalsIgnoreCase("send"))) {
                sendFolders.forEach(sendFolder -> {
                    try {
                        // 获取当前时间
                        Instant now = Instant.now();
                        // 遍历SEND文件夹内的所有文件
                        Files.walk(sendFolder)
                                .filter(Files::isRegularFile)
                                .forEach(file -> {
                                    try {
                                        // 获取文件的最后修改时间
                                        Instant lastModified = Files.getLastModifiedTime(file).toInstant();
                                        // 计算时间差
                                        Duration duration = Duration.between(lastModified, now);
                                        // 判断是否超时(时间存在字典中,base_single_dictionary)
                                        if (duration.getSeconds() > dayToSecond) {
                                            try {
                                                Files.delete(file);
                                                System.out.println("已删除文件: " + file);
                                            } catch (IOException e) {
                                                System.err.println("删除文件 " + file + " 时出现错误: " + e.getMessage());
                                            }
                                        }
                                    } catch (IOException e) {
                                        System.err.println("获取文件 " + file + " 的最后修改时间时出现错误: " + e.getMessage());
                                    }
                                });
                    } catch (IOException e) {
                        System.err.println("遍历 " + sendFolder + " 文件夹时出现错误: " + e.getMessage());
                    }
                });
            } catch (IOException e) {
                System.err.println("查找send文件夹时出现错误: " + e.getMessage());
            }
        } else {
            System.err.println("D盘的NC目录不存在或不是有效的目录");
        }
    }
//    @Override
//    @Scheduled(cron = "${fileCron}")
//    public void fileClientTxtOrNc() {
//        SysLogMessageDto message = new SysLogMessageDto();
//        String host = "127.0.0.1";
//        if (StringUtils.isNotBlank(serverIp)) {
//            host = serverIp;
//        }
//        String port = "8299";
//        if (StringUtils.isNotBlank(serverPort)) {
//            port = serverPort;
//        }
//        String username = "admin";
//        if (StringUtils.isNotBlank(usernameService)) {
//            username = usernameService;
//        }
//        String pwd = "123";
//        if (StringUtils.isNotBlank(pwdService)) {
//            pwd = pwdService;
//        }
//        //文件本地地址,根据地址找到文件,将文件解析成文件流
//        String localFilePath = "E:\\test\\a\\";
//        if (StringUtils.isNotBlank(localFilePathC)) {
//            localFilePath = localFilePathC;
//        }
//        //文件上传目的地址,将文件上传到该地址
//        String servicePath = "E:\\test\\b\\";
//        if (StringUtils.isNotBlank(servicePathS)) {
//            servicePath = servicePathS;
//        }
//        boolean btelnetPort =  TelnetUtil.telnetPort(serverIp,Integer.valueOf(serverPort),10);
//        if (!btelnetPort) {
//            System.out.println("服务器连接: " + serverIp + ":" + serverPort + " 异常!" );
//            return;
//        }
//        //第一步,获取token
//        String token = null;
//        try {
//            token = FileClient.getToken(host,port,username,pwd,addressToken);
//        } catch (Throwable throwable) {
//            throwable.printStackTrace();
//        }
//
//        //若获取token成功,再进行上传文件接口调用
//        if (token !=null && !token.equals("")){
//            try {
//                File f3 = new File(localFilePath);
//                File[] files = f3.listFiles();
//                for (File fi : files){
//                    if (fi.isFile()){
//                        SysLogTypeObjectDto objectName = new SysLogTypeObjectDto();
//                        objectName.setDateTime(DateUtil.format(DateUtil.getNow(),DateUtil.STR_DATE_TIME_SMALL));
//                        objectName.setFileName(fi.getName());
//                        objectName.setFileSize(FileUtil.changeFileFormatKb(String.valueOf(new File(localFilePath).length())));
//                        objectName.setSourceAddress(localFilePath);
//                        objectName.setDestination(serverIp);//服务端IP
//                        //顺序号
//                        DncPassLog passLog = getById("num0001");
//                        if (passLog == null) {
//                            passLog = new DncPassLog();
//                            passLog.setId("num0001");
//                            passLog.setSequenceNumber(1);
//                            objectName.setFileNum(Integer.toString(1));
//                            save(passLog);
//                        } else {
//                            Integer number = passLog.getSequenceNumber()+1;
//                            removeById("num0001");
//                            objectName.setFileNum(Integer.toString(number));
//
//                            passLog = new DncPassLog();
//                            passLog.setId("num0001");
//                            passLog.setSequenceNumber(number);
//                            save(passLog);
//                        }
//
//                        InetAddress address = null;
//                        try {
//                            address = InetAddress.getLocalHost();
//                            String ip = address.getHostAddress();
//                            objectName.setSourceAddress("20.10.17.11");//客户端IP
//                            objectName.setAddress(ip);
//                        } catch (UnknownHostException e) {
//                            objectName.setSourceAddress("127.0.0.1");
//                            objectName.setAddress("127.0.0.1");
//                        }
//                        objectName.setDestination(host);
//                        objectName.setResult("失败");
//                        //获取某个文件下的所有文件
//                        String loFilePath = localFilePath + fi.getName();
//                        String servicePathName =servicePath+"//" + fi.getName();
//                        File file = new File(loFilePath);
//                        if (file == null || !file.exists()){
//                            objectName.setAbstract1("sm3");
//                        } else {
//                            String sm3 = SmUtil.sm3(file);
//                            objectName.setAbstract1(sm3);
//                        }
//
//                        String b  = FileClient.uploadFile(host,port,token,fi.getName(),servicePathName,loFilePath,addressUploadFile);
//                        //文件备份删除
//                        if (b == null) {
//                            try {
//                                objectName.setResult("失败");
//                                objectName.setTypes("error");
//                                SyslogClient.sendClient(logIp,Integer.valueOf(logPort),objectName.toString());
//                            }catch (Exception e) {
//                                return;
//                            }
//                        }
//                        else if ( b.equals("成功")) {
//                            //备份数据
//                            boolean fCopy = FileUtil.copyNcFile(loFilePath,newFilePathC + "/" + DateUtil.format(DateUtil.getNow(),
//                                    DateUtil.STR_YEARMONTHDAY) + "/" +fi.getName());
//                            if (fCopy) {
//                                FileUtil.deleteNcFile(loFilePath);
//                            } else {
//                                FileUtil.copyNcFile(loFilePath,newFilePathC + "/" + DateUtil.format(DateUtil.getNow(),
//                                        DateUtil.STR_YEARMONTHDAY) + "/" +fi.getName());
//                                FileUtil.deleteNcFile(loFilePath);
//                            }
//                            try {
//                                objectName.setTypes("Info");
//                                objectName.setResult("成功");
//                                SyslogClient.sendClient(logIp,Integer.valueOf(logPort),objectName.toString());
//                            }catch (Exception e) {
//                                return;
//                            }
//                        } else {
//                            try {
//                                objectName.setResult("失败");
//                                objectName.setTypes("error");
//                                SyslogClient.sendClient(logIp,Integer.valueOf(logPort),objectName.toString());
//                            }catch (Exception e) {
//                                return;
//                            }
//                        }
//                    }
//                }
//
//            } catch (Throwable throwable) {
//                throwable.printStackTrace();
//            }
//        }
//    }
//
//    /**
//     * 定时任务执行器参数  每天凌晨1.30执行
//     */
//    @Override
//    @Scheduled(cron = "0 30 1 * * ?")
//    public void deleteNcSendFile() {
//        Integer day;
//        //取字典时间
//        List<SingleDictionary> singleDictionaries=iSingleDictionaryService.findListFromTypeCode("DEL_TIME_SEND");
//        if (singleDictionaries.size()!=1){
//            return;
//        }else {
//            day= singleDictionaries.get(0).getDicValue();
//        }
//        Path ncDirectory = Paths.get("D:\\NC");
//        //天转秒
//        long dayToSecond = day * 24 * 60 * 60;
//        if (Files.exists(ncDirectory) && Files.isDirectory(ncDirectory)) {
//            // 递归查找NC目录下所有名为send的文件夹
//            try (Stream<Path> sendFolders = Files.walk(ncDirectory)
//                    .filter(Files::isDirectory)
//                    .filter(path -> path.getFileName().toString().equalsIgnoreCase("send"))) {
//                sendFolders.forEach(sendFolder -> {
//                    try {
//                        // 获取当前时间
//                        Instant now = Instant.now();
//                        // 遍历SEND文件夹内的所有文件
//                        Files.walk(sendFolder)
//                                .filter(Files::isRegularFile)
//                                .forEach(file -> {
//                                    try {
//                                        // 获取文件的最后修改时间
//                                        Instant lastModified = Files.getLastModifiedTime(file).toInstant();
//                                        // 计算时间差
//                                        Duration duration = Duration.between(lastModified, now);
//                                        // 判断是否超时(时间存在字典中,base_single_dictionary)
//                                        if (duration.getSeconds() > dayToSecond) {
//                                            try {
//                                                Files.delete(file);
//                                                System.out.println("已删除文件: " + file);
//                                            } catch (IOException e) {
//                                                System.err.println("删除文件 " + file + " 时出现错误: " + e.getMessage());
//                                            }
//                                        }
//                                    } catch (IOException e) {
//                                        System.err.println("获取文件 " + file + " 的最后修改时间时出现错误: " + e.getMessage());
//                                    }
//                                });
//                    } catch (IOException e) {
//                        System.err.println("遍历 " + sendFolder + " 文件夹时出现错误: " + e.getMessage());
//                    }
//                });
//            } catch (IOException e) {
//                System.err.println("查找send文件夹时出现错误: " + e.getMessage());
//            }
//        } else {
//            System.err.println("D盘的NC目录不存在或不是有效的目录");
//        }
//    }
}