diff --git a/src/main/java/org/springblade/ftpdemo/controller/SSHRemoteCall.java b/src/main/java/org/springblade/ftpdemo/controller/SSHRemoteCall.java index 56fd724..476a6ca 100644 --- a/src/main/java/org/springblade/ftpdemo/controller/SSHRemoteCall.java +++ b/src/main/java/org/springblade/ftpdemo/controller/SSHRemoteCall.java @@ -3,299 +3,391 @@ package org.springblade.ftpdemo.controller; import com.jcraft.jsch.*; import java.io.*; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Vector; public class SSHRemoteCall { - // 私有的对象 - private static SSHRemoteCall sshRemoteCall; + private static final int DEFAULT_PORT = 30020;// 默认端口号 + // 私有的对象 + private static SSHRemoteCall sshRemoteCall; - /** - * 私有的构造方法 - */ - private SSHRemoteCall() { - } - - // 懒汉式,线程不安全,适合单线程 + // 懒汉式,线程不安全,适合单线程 // public static SSHRemoteCall getInstance() { // if (sshRemoteCall == null) { // sshRemoteCall = new SSHRemoteCall(); // } // return sshRemoteCall; // } - - // 懒汉式,线程安全,适合多线程 - public static synchronized SSHRemoteCall getInstance() { - if (sshRemoteCall == null) { - sshRemoteCall = new SSHRemoteCall(); - } - return sshRemoteCall; - } - - private static final int DEFAULT_PORT = 30020;// 默认端口号 - private int port;// 端口号 - - private static String ipAddress = "223.99.228.240";// ip地址 - private static String userName = "root";// 账号 - private static String password = "Qingniao@123!";// 密码 - - private Session session;// JSCH session - private boolean logined = false;// 是否登陆 - - /** - * 构造方法,可以直接使用DEFAULT_PORT - * - * @param ipAddress - * @param userName - * @param password - */ - public SSHRemoteCall(String ipAddress, String userName, String password) { - this(ipAddress, DEFAULT_PORT, userName, password); - } - - /** - * 构造方法,方便直接传入ipAddress,userName,password进行调用 - * - * @param ipAddress - * @param port - * @param userName - * @param password - */ - public SSHRemoteCall(String ipAddress, int port, String userName, String password) { - super(); - this.ipAddress = ipAddress; - this.userName = userName; - this.password = password; - this.port = port; - } - - /** - * 远程登陆 - * - * @throws Exception - */ - public void sshRemoteCallLogin(String ipAddress, String userName, String password) throws Exception { - // 如果登陆就直接返回 - if (logined) { - return; - } - // 创建jSch对象 - JSch jSch = new JSch(); - try { - // 获取到jSch的session, 根据用户名、主机ip、端口号获取一个Session对象 - session = jSch.getSession(userName, ipAddress, 22102); - // 设置密码 - session.setPassword(password); - - // 方式一,通过Session建立连接 - // session.setConfig("StrictHostKeyChecking", "no"); - // session.connect(); - - // 方式二,通过Session建立连接 - // java.util.Properties; + private static String ipAddress = "223.99.228.240";// ip地址 + private static String userName = "root";// 账号 + private static String password = "Qingniao@123!";// 密码 + private int port;// 端口号 + private Session session;// JSCH session + private boolean logined = false;// 是否登陆 + + /** + * 私有的构造方法 + */ + private SSHRemoteCall() { + } + + /** + * 构造方法,可以直接使用DEFAULT_PORT + * + * @param ipAddress + * @param userName + * @param password + */ + public SSHRemoteCall(String ipAddress, String userName, String password) { + this(ipAddress, DEFAULT_PORT, userName, password); + } + + /** + * 构造方法,方便直接传入ipAddress,userName,password进行调用 + * + * @param ipAddress + * @param port + * @param userName + * @param password + */ + public SSHRemoteCall(String ipAddress, int port, String userName, String password) { + super(); + this.ipAddress = ipAddress; + this.userName = userName; + this.password = password; + this.port = port; + } + + // 懒汉式,线程安全,适合多线程 + public static synchronized SSHRemoteCall getInstance() { + if (sshRemoteCall == null) { + sshRemoteCall = new SSHRemoteCall(); + } + return sshRemoteCall; + } + + /** + * 远程登陆 + * + * @throws Exception + */ + public void sshRemoteCallLogin(String ipAddress, String userName, String password) throws Exception { + // 如果登陆就直接返回 + if (logined) { + return; + } + // 创建jSch对象 + JSch jSch = new JSch(); + try { + // 获取到jSch的session, 根据用户名、主机ip、端口号获取一个Session对象 + session = jSch.getSession(userName, ipAddress, 22); //012 + // 设置密码 + session.setPassword(password); + + // 方式一,通过Session建立连接 + // session.setConfig("StrictHostKeyChecking", "no"); + // session.connect(); + + // 方式二,通过Session建立连接 + // java.util.Properties; // Properties config = new Properties(); // config.put("StrictHostKeyChecking", "no"); // session.setConfig(config);// 为Session对象设置properties - // session.setTimeout( );// 设置超时 - session.setConfig("StrictHostKeyChecking", "no"); - session.connect();//// 通过Session建立连接 - - // 设置登陆状态 - logined = true; - } catch (JSchException e) { - // 设置登陆状态为false - logined = false; - throw new Exception( - "主机登录失败, IP = " + ipAddress + ", USERNAME = " + userName + ", Exception:" + e.getMessage()); - } - } - - /** - * 关闭连接 - */ - public void closeSession() { - // 调用session的关闭连接的方法 - if (session != null) { - // 如果session不为空,调用session的关闭连接的方法 - session.disconnect(); - } - - } - - /** - * 执行相关的命令 - * - * @param command - * @throws IOException - */ - public String execCommand(String command) throws IOException { - InputStream in = null;// 输入流(读) - Channel channel = null;// 定义channel变量 - try { - // 如果命令command不等于null - if (command != null) { - // 打开channel - //说明:exec用于执行命令;sftp用于文件处理 - channel = session.openChannel("exec"); - // 设置command - ((ChannelExec) channel).setCommand(command); - // channel进行连接 - channel.connect(); - // 获取到输入流 - in = channel.getInputStream(); - // 执行相关的命令 - String processDataStream = processDataStream(in); - // 打印相关的命令 - return processDataStream; - } - } catch (JSchException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (in != null) { - in.close(); - } - if (channel != null) { - channel.disconnect(); - } - } - return null; - } - - /** - * 对将要执行的linux的命令进行遍历 - * - * @param in - * @return - * @throws Exception - */ - public String processDataStream(InputStream in) throws Exception { - StringBuffer sb = new StringBuffer(); - BufferedReader br = new BufferedReader(new InputStreamReader(in)); - String result = ""; - try { - while ((result = br.readLine()) != null) { - sb.append(result); - // System.out.println(sb.toString()); - } - } catch (Exception e) { - throw new Exception("获取数据流失败: " + e); - } finally { - br.close(); - } - return sb.toString(); - } - - /** - * 上传文件 可参考:https://www.cnblogs.com/longyg/archive/ / / / .html - * - * @param directory 上传文件的目录 - * @param uploadFile 将要上传的文件 - */ - public void uploadFile(String directory, String uploadFile) { - try { - // 打开channelSftp - ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp"); - // 远程连接 - channelSftp.connect(); - // 创建一个文件名称问uploadFile的文件 - File file = new File(uploadFile); - // 将文件进行上传(sftp协议) - // 将本地文件名为src的文件上传到目标服务器,目标文件名为dst,若dst为目录,则目标文件名将与src文件名相同. - // 采用默认的传输模式:OVERWRITE - channelSftp.put(new FileInputStream(file), directory, ChannelSftp.OVERWRITE); - // 切断远程连接 - channelSftp.exit(); - System.out.println("上传文件:" + file.getName()); - } catch (JSchException e) { - e.printStackTrace(); - } catch (SftpException e) { - e.printStackTrace(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - - } - - /** - * 下载文件 采用默认的传输模式:OVERWRITE - * - * @param src linux服务器文件地址 - * @param dst 本地存放地址 - * @throws JSchException - * @throws SftpException - */ - public void fileDownload(String src, String dst) throws JSchException, SftpException { - // src 是linux服务器文件地址,dst 本地存放地址 - ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp"); - // 远程连接 - channelSftp.connect(); - // 下载文件,多个重载方法 - channelSftp.get(src, dst); - // 切断远程连接,quit()等同于exit(),都是调用disconnect() - channelSftp.quit(); - // channelSftp.disconnect(); - System.out.println("下载文件:" + src); - } - - /** - * 删除文件 - * - * @throws SftpException - * @throws JSchException - */ - public void deleteFile(String directoryFile) throws SftpException, JSchException { - // 打开openChannel的sftp - ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp"); - // 远程连接 - channelSftp.connect(); - // 删除文件 - channelSftp.rm(directoryFile); - // 切断远程连接 - channelSftp.exit(); - System.out.println("删除文件:" + directoryFile); - } - - /** - * 如果此目录不存在将新建目录 - */ - public void directory(String path) { - // 打开openChannel的sftp - ChannelSftp channelSftp = null; - try { - channelSftp = (ChannelSftp) session.openChannel("sftp"); - // 远程连接 - channelSftp.connect(); - // 切换到主目录 + // session.setTimeout( );// 设置超时 + session.setConfig("StrictHostKeyChecking", "no"); + session.connect();//// 通过Session建立连接 + + // 设置登陆状态 + logined = true; + } catch (JSchException e) { + // 设置登陆状态为false + logined = false; + throw new Exception( + "主机登录失败, IP = " + ipAddress + ", USERNAME = " + userName + ", Exception:" + e.getMessage()); + } + } + + /** + * 关闭连接 + */ + public void closeSession() { + // 调用session的关闭连接的方法 + if (session != null) { + // 如果session不为空,调用session的关闭连接的方法 + session.disconnect(); + } + + } + + /** + * 执行相关的命令 + * + * @param command + * @throws IOException + */ + public String execCommand(String command) throws IOException { + InputStream in = null;// 输入流(读) + Channel channel = null;// 定义channel变量 + try { + // 如果命令command不等于null + if (command != null) { + // 打开channel + //说明:exec用于执行命令;sftp用于文件处理 + channel = session.openChannel("exec"); + // 设置command + ((ChannelExec) channel).setCommand(command); + // channel进行连接 + channel.connect(); + // 获取到输入流 + in = channel.getInputStream(); + // 执行相关的命令 + String processDataStream = processDataStream(in); + // 打印相关的命令 + return processDataStream; + } + } catch (JSchException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (in != null) { + in.close(); + } + if (channel != null) { + channel.disconnect(); + } + } + return null; + } + + /** + * 对将要执行的linux的命令进行遍历 + * + * @param in + * @return + * @throws Exception + */ + public String processDataStream(InputStream in) throws Exception { + StringBuffer sb = new StringBuffer(); + BufferedReader br = new BufferedReader(new InputStreamReader(in)); + String result = ""; + try { + while ((result = br.readLine()) != null) { + sb.append(result); + // System.out.println(sb.toString()); + } + } catch (Exception e) { + throw new Exception("获取数据流失败: " + e); + } finally { + br.close(); + } + return sb.toString(); + } + + /** + * 上传文件 可参考:https://www.cnblogs.com/longyg/archive/ / / / .html + * + * @param directory 上传文件的目录 + * @param uploadFile 将要上传的文件 + */ + public void uploadFile(String directory, String uploadFile) { + try { + // 打开channelSftp + ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp"); + // 远程连接 + channelSftp.connect(); + // 创建一个文件名称问uploadFile的文件 + File file = new File(uploadFile); + // 将文件进行上传(sftp协议) + // 将本地文件名为src的文件上传到目标服务器,目标文件名为dst,若dst为目录,则目标文件名将与src文件名相同. + // 采用默认的传输模式:OVERWRITE + channelSftp.put(new FileInputStream(file), directory, ChannelSftp.OVERWRITE); + // 切断远程连接 + channelSftp.exit(); + System.out.println("上传文件:" + file.getName()); + } catch (JSchException e) { + e.printStackTrace(); + } catch (SftpException e) { + e.printStackTrace(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + + } + + /** + * 下载文件 采用默认的传输模式:OVERWRITE + * + * @param src linux服务器文件地址 + * @param dst 本地存放地址 + * @throws JSchException + * @throws SftpException + */ + public void fileDownload(String src, String dst) throws JSchException, SftpException { + // src 是linux服务器文件地址,dst 本地存放地址 + ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp"); + // 远程连接 + channelSftp.connect(); + // 下载文件,多个重载方法 + channelSftp.get(src, dst); + // 切断远程连接,quit()等同于exit(),都是调用disconnect() + channelSftp.quit(); + // channelSftp.disconnect(); + System.out.println("下载文件:" + src); + } + + /** + * 删除文件 + * + * @throws SftpException + * @throws JSchException + */ + public void deleteFile(String directoryFile) throws SftpException, JSchException { + // 打开openChannel的sftp + ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp"); + // 远程连接 + channelSftp.connect(); + // 删除文件 + channelSftp.rm(directoryFile); + // 切断远程连接 + channelSftp.exit(); + System.out.println("删除文件:" + directoryFile); + } + + /** + * 如果此目录不存在将新建目录 + */ + public void directory(String path) { + // 打开openChannel的sftp + ChannelSftp channelSftp = null; + try { + channelSftp = (ChannelSftp) session.openChannel("sftp"); + // 远程连接 + channelSftp.connect(); + // 切换到主目录 // channelSftp.cd("/"); - } catch (JSchException e) { - throw new RuntimeException(e); - } - - String[] folders = path.split( "/" ); - for ( String folder : folders ) { - if ( folder.length() > 0 ) { - try { - channelSftp.cd( folder ); - System.out.println("切换目录"+folder); - } - catch ( SftpException e ) { - try { - channelSftp.mkdir( folder ); - System.out.println("生成目录"+folder); - channelSftp.cd( folder ); - } catch (SftpException ex) { - throw new RuntimeException(ex); - - } - - } - } - } - } + } catch (JSchException e) { + throw new RuntimeException(e); + } + + String[] folders = path.split("/"); + for (String folder : folders) { + if (folder.length() > 0) { + try { + channelSftp.cd(folder); + System.out.println("切换目录" + folder); + } catch (SftpException e) { + try { + channelSftp.mkdir(folder); + System.out.println("生成目录" + folder); + channelSftp.cd(folder); + } catch (SftpException ex) { + throw new RuntimeException(ex); + + } + + } + } + } + } + + + /** + * 列出目录下的文件 + * + * @param directory 要列出的目录 + * @return + * @throws SftpException + * @throws JSchException + */ + public Vector listFilesBack(String directory) throws JSchException, SftpException { + ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp"); + // 远程连接 + channelSftp.connect(); + // 显示目录信息 + Vector ls = channelSftp.ls(directory); + + Iterator iterator = ls.iterator(); + + Map fileMap = new HashMap<>(); + + try { + + while (iterator.hasNext()) { + ChannelSftp.LsEntry file = (ChannelSftp.LsEntry) iterator.next(); + //文件名称 + String fileName = file.getFilename(); + System.out.println(fileName); + //todo 这里可使用if或正则不下载一些文件 + InputStream inputStream = channelSftp.get(directory + fileName); + if (inputStream != null) { + File newFile = new File(fileName); + //将InputStream转File +// FileUtils.copyToFile(inputStream, newFile); + fileMap.put(fileName, newFile); + } + } + + } catch (Exception e) { + e.printStackTrace(); + } finally { + // 切断连接 + channelSftp.exit(); + } + + return ls; + } + + /** + * 列出目录下的文件 + * + * @param directory 要列出的目录 + * @return + * @throws SftpException + * @throws JSchException + */ + public void listFiles(String directory) throws JSchException, SftpException { + ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp"); + // 远程连接 + channelSftp.connect(); + // 显示目录信息 + Vector ls = channelSftp.ls(directory); + + Iterator iterator = ls.iterator(); + + Map fileMap = new HashMap<>(); + + try { + + while (iterator.hasNext()) { + ChannelSftp.LsEntry file = (ChannelSftp.LsEntry) iterator.next(); + //文件名称 + String fileName = file.getFilename(); + System.out.println(fileName); + //todo 这里可使用if或正则不下载一些文件 +// InputStream inputStream = channelSftp.get(directory + fileName); +// if (inputStream != null) { +// File newFile = new File(fileName); +// //将InputStream转File +//// FileUtils.copyToFile(inputStream, newFile); +// fileMap.put(fileName, newFile); +// } + } + + } catch (Exception e) { + e.printStackTrace(); + } finally { + // 切断连接 + channelSftp.exit(); + } + + } diff --git a/src/main/java/org/springblade/ftpdemo/entity/SysDict.java b/src/main/java/org/springblade/ftpdemo/entity/SysDict.java index 5e79af8..5a66617 100644 --- a/src/main/java/org/springblade/ftpdemo/entity/SysDict.java +++ b/src/main/java/org/springblade/ftpdemo/entity/SysDict.java @@ -1,5 +1,6 @@ package org.springblade.ftpdemo.entity; +import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; import java.util.Date; @@ -9,6 +10,7 @@ import java.util.Date; * @since 2022/11/28 */ @Data +@TableName("sys_dict") public class SysDict { private Long id; private String dictKey; diff --git a/src/main/java/org/springblade/ftpdemo/service/IdentificationIp.java b/src/main/java/org/springblade/ftpdemo/service/IdentificationIp.java index 31b33b6..20999b7 100644 --- a/src/main/java/org/springblade/ftpdemo/service/IdentificationIp.java +++ b/src/main/java/org/springblade/ftpdemo/service/IdentificationIp.java @@ -1,7 +1,9 @@ package org.springblade.ftpdemo.service; +import lombok.AllArgsConstructor; import org.springblade.core.tool.utils.DateUtil; import org.springblade.ftpdemo.service.impl.MethodImpl; +import org.springframework.beans.factory.annotation.Autowired; import java.io.File; @@ -11,9 +13,12 @@ import java.io.File; * @author laifeng * @since 2022/11/27 */ +@AllArgsConstructor public class IdentificationIp { + + private final Method method; public static void main(String[] args) throws Exception { -// MethodImpl method = new MethodImpl(); + // String fileName = "d://aaa//test.txt"; // // 要上传的目录 // String upSrc = "/002/" + DateUtil.format(DateUtil.now(), "yyyyMMdd"); @@ -23,6 +28,7 @@ public class IdentificationIp { // String operationName = "-add.bin"; // method.upMethod(fileName, upSrc, tableName, operationName); + } public static void deleteFile(String src) { diff --git a/src/main/java/org/springblade/ftpdemo/service/Method.java b/src/main/java/org/springblade/ftpdemo/service/Method.java index 664d35d..0eedf51 100644 --- a/src/main/java/org/springblade/ftpdemo/service/Method.java +++ b/src/main/java/org/springblade/ftpdemo/service/Method.java @@ -1,6 +1,5 @@ package org.springblade.ftpdemo.service; -import java.io.FileNotFoundException; import java.io.IOException; /** @@ -15,4 +14,6 @@ public interface Method { Boolean creatForUpFile(Integer type, String yyyyMMdd, String yyyyMMddToday) throws IOException; Boolean creatFiveUpFile(Integer type, String yyyyMMdd, String yyyyMMddToday) throws IOException; + + void creatSixUpFileAndUp(String yyyyMMdd, String yyyyMMddToday) throws Exception; } diff --git a/src/main/java/org/springblade/ftpdemo/service/impl/MethodImpl.java b/src/main/java/org/springblade/ftpdemo/service/impl/MethodImpl.java index 1940d9d..0bb79df 100644 --- a/src/main/java/org/springblade/ftpdemo/service/impl/MethodImpl.java +++ b/src/main/java/org/springblade/ftpdemo/service/impl/MethodImpl.java @@ -1,5 +1,8 @@ package org.springblade.ftpdemo.service.impl; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.jcraft.jsch.*; import lombok.AllArgsConstructor; import org.springblade.core.tool.utils.DateUtil; import org.springblade.core.tool.utils.Func; @@ -11,11 +14,14 @@ import org.springblade.ftpdemo.service.Method; import org.springblade.ftpdemo.service.NbmsEnprefixService; import org.springblade.ftpdemo.service.NbmsEntApplyServcie; import org.springblade.ftpdemo.util.Constant; +import org.springblade.ftpdemo.util.Decrypt; import org.springblade.ftpdemo.util.Encrypt; import org.springframework.stereotype.Service; import java.io.*; +import java.util.Date; import java.util.List; +import java.util.zip.GZIPInputStream; /** * @author laifeng @@ -25,19 +31,7 @@ import java.util.List; @AllArgsConstructor public class MethodImpl implements Method { private final NbmsEntApplyServcie nbmsEntApplyServcie; - String companyCode; //标识码企业前缀 - String companyName; // 标识解析节点经营主体名称 - String level; // 标识解析系统级别 - String accessProvince; // 接入省份 - String accessCity; // 接入地市 - String registerTime; // 注册时间 - String categoryA; // 经营主体行业门类 - String categoryB; // 经营主体行业大类 - String companyType; // 公司性质 - String serviceProfession; //节点服务行业 - String startIP = "223.99.228.240"; - String endIP = "223.99.228.240"; - String position = "中国山东省济南市自由贸易试验区济南片区颖秀路2299号"; + private NbmsEnprefixService enprefixService; @Override @@ -94,6 +88,16 @@ public class MethodImpl implements Method { */ @Override public Boolean creatIdentificationUpFile(Integer type, String yyyyMMdd, String yyyyMMddToday) throws IOException { + String companyCode; //标识码企业前缀 + String companyName; // 标识解析节点经营主体名称 + String level; // 标识解析系统级别 + String accessProvince; // 接入省份 + String accessCity; // 接入地市 + String registerTime; // 注册时间 + String categoryA; // 经营主体行业门类 + String categoryB; // 经营主体行业大类 + String companyType; // 公司性质 + String serviceProfession; //节点服务行业 List list = null; // 新增操作 @@ -157,6 +161,10 @@ public class MethodImpl implements Method { */ @Override public Boolean creatForUpFile(Integer type, String yyyyMMdd, String yyyyMMddToday) throws IOException { + String companyName; // 标识解析节点经营主体名称 + String startIP = "223.99.228.240"; + String endIP = "223.99.228.240"; + String position = "中国山东省济南市自由贸易试验区济南片区颖秀路2299号"; List list = null; if (type == 0) { list = enprefixService.query() @@ -192,6 +200,16 @@ public class MethodImpl implements Method { */ @Override public Boolean creatFiveUpFile(Integer type, String yyyyMMdd, String yyyyMMddToday) throws IOException { + String companyCode; //标识码企业前缀 + String companyName; // 标识解析节点经营主体名称 + String level; // 标识解析系统级别 + String accessProvince; // 接入省份 + String accessCity; // 接入地市 + String registerTime; // 注册时间 + String categoryA; // 经营主体行业门类 + String categoryB; // 经营主体行业大类 + String companyType; // 公司性质 + String serviceProfession; //节点服务行业 List list = null; // 新增操作 @@ -248,6 +266,89 @@ public class MethodImpl implements Method { return false; } + @Override + public void creatSixUpFileAndUp(String yyyyMMdd, String yyyyMMddToday) throws Exception { + JSch jsch = new JSch(); + Session session = jsch.getSession(Constant.user, Constant.host, 22); + session.setPassword(Constant.password1); + session.setConfig("StrictHostKeyChecking", "no"); + session.connect(); + + ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp"); + sftp.connect(); + + // 连接远程服务器,日志存放地 + Date date = DateUtil.minusDays(new Date(), 1); + String yesterday = DateUtil.formatDate(date); + List allFile = Decrypt.getAllFile("/opt/" + yesterday); + if (Func.isEmpty(allFile)){ + return; + } + for (File file : allFile) { + System.out.println("成功进入"); + String filePath = file.getName(); + // 读取远程服务器上的日志文件 gz压缩格式 + GZIPInputStream gzipInputStream = new GZIPInputStream(sftp.get(filePath)); + Reader decoder = new InputStreamReader(gzipInputStream, "UTF-8"); + BufferedReader reader = new BufferedReader(decoder); + // 创建输出流 + FileOutputStream outputStream = new FileOutputStream(Constant.sixLocalFileNamePath); + String tempString; + //一次读一行,读入null时文件结束 + while ((tempString = reader.readLine()) != null) { + // 获得请求时间 + JSONObject jsonObject = JSON.parseObject(tempString); + String operatorTime = jsonObject.getString("operatorTime"); + Date date1 = DateUtil.parse(operatorTime, DateUtil.PATTERN_DATETIME_MINI); + String requestTime = DateUtil.format(date1, "yyyy-MM-dd HH:mm:ss"); + // 获得响应时间 + String responseTime = requestTime; + // 获得源IP + String sourceIP = jsonObject.getString("userIp"); + // 端口 + String port = "80"; + // 获得目的IP + String destinationIP = jsonObject.getString("idisIp"); + // 方法 + String method = "HTTPM_GET"; + // 企业标识码 + String companyCode = jsonObject.getString("prefix"); + // 标识编码 + String ideCode = jsonObject.getString("handleName"); + // + + // 组装字符串 + tempString = requestTime+"|"+ responseTime +"|"+sourceIP+"|"+port+"|"+destinationIP+"|"+port+"|"+method+"|"+companyCode+"|"+ideCode+"|"+"|"+"|"+"|"+"http://code.qnaiot.com"+"|"+"http://code.qnaiot.com"+"|"+"\n"; +// String temp = tempString + "\n"; + + byte[] bytes = tempString.getBytes(); + outputStream.write(bytes); + + } + outputStream.flush(); + outputStream.close(); + decoder.close(); + gzipInputStream.close(); + // 上传 + String fileName = "d://aaa//test.txt"; + // 要上传的目录 + String upSrc = "/004/" + yyyyMMdd; + // 表名(文件名的一部分) + String tableName = "SDQN-basic_registered_log-"; + String operationName = ".bin"; + this.upMethod(fileName,upSrc,tableName,operationName); + } + + + + + + sftp.disconnect(); + session.disconnect(); + + + } + // private void deleteFile(String src) { // diff --git a/src/main/java/org/springblade/ftpdemo/task/IdentificationUpTask.java b/src/main/java/org/springblade/ftpdemo/task/IdentificationUpTask.java index 7024d6f..06df4ae 100644 --- a/src/main/java/org/springblade/ftpdemo/task/IdentificationUpTask.java +++ b/src/main/java/org/springblade/ftpdemo/task/IdentificationUpTask.java @@ -24,7 +24,7 @@ public class IdentificationUpTask { private final NbmsEntApplyServcie nbmsEntApplyServcie; private final Method method; - @Scheduled(cron = "0 0/1 * * * ?") + @Scheduled(cron = "0 1/10 * * * ?") public void refreshAliDeviceStatus() { // 标识解析企业主体信息上报检测新增并上传 threePackMethod(0); diff --git a/src/main/java/org/springblade/ftpdemo/task/TestTask.java b/src/main/java/org/springblade/ftpdemo/task/TestTask.java new file mode 100644 index 0000000..49baf84 --- /dev/null +++ b/src/main/java/org/springblade/ftpdemo/task/TestTask.java @@ -0,0 +1,42 @@ +package org.springblade.ftpdemo.task; + +import lombok.AllArgsConstructor; +import org.springblade.core.tool.utils.DateUtil; +import org.springblade.ftpdemo.controller.SSHRemoteCall; +import org.springblade.ftpdemo.service.Method; +import org.springblade.ftpdemo.util.Constant; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import org.springframework.util.StopWatch; + +import java.util.Vector; + +/** + * @author laifeng + * @since 2022/12/1 + */ +@Component +@EnableScheduling +@AllArgsConstructor +public class TestTask { + private final Method method; + + @Scheduled(cron = "0 0/1 * * * ?") + public void refreshAliDeviceStatus() throws Exception { +// StopWatch stopWatch = new StopWatch("读取表6日志,生成加密文件,上传" ); +// stopWatch.start(); +// String yyyyMMdd = DateUtil.format(DateUtil.now(), "yyyyMMdd"); +// +// method.creatSixUpFileAndUp(yyyyMMdd, "1"); +// stopWatch.stop(); +// double seconds = stopWatch.getTotalTimeSeconds(); +// System.out.println("完成,花费:"+seconds); + + + // 1、首先远程连接ssh + SSHRemoteCall.getInstance().sshRemoteCallLogin(Constant.host, Constant.user,Constant.password1); + SSHRemoteCall.getInstance().listFiles("/opt/2022-11-30"); + + } +} diff --git a/src/main/java/org/springblade/ftpdemo/util/Constant.java b/src/main/java/org/springblade/ftpdemo/util/Constant.java index 780cb2c..e51d228 100644 --- a/src/main/java/org/springblade/ftpdemo/util/Constant.java +++ b/src/main/java/org/springblade/ftpdemo/util/Constant.java @@ -12,4 +12,11 @@ public interface Constant { // String ipAddress = "47.97.5.58";// ip地址 // String userName = "root";// 账号 // String password = "Aa1572427111.";// 密码 + + String host = "47.97.5.58";// ip地址 + String user = "root";// 账号 + String password1 = "Aa1572427111.";// 密码 + + // 表6 本地文件生成地址 + String sixLocalFileNamePath = "D:\\aaa\\test.txt"; } diff --git a/src/main/java/org/springblade/ftpdemo/util/Decrypt.java b/src/main/java/org/springblade/ftpdemo/util/Decrypt.java index 798f024..951b30d 100644 --- a/src/main/java/org/springblade/ftpdemo/util/Decrypt.java +++ b/src/main/java/org/springblade/ftpdemo/util/Decrypt.java @@ -1,5 +1,6 @@ package org.springblade.ftpdemo.util; +import com.github.xiaoymin.knife4j.core.util.StrUtil; import org.apache.commons.codec.binary.Base64; import javax.crypto.Cipher; @@ -8,6 +9,8 @@ import javax.crypto.spec.SecretKeySpec; import java.io.*; import java.util.ArrayList; import java.util.List; +import java.util.Objects; +import java.util.zip.GZIPInputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -246,4 +249,84 @@ public class Decrypt { return bytes; } + + + /** + * 读取gz文件 + * @param fileName + */ + public static void readFileByLines(String fileName) { + GZIPInputStream gzipInputStream; + BufferedReader reader = null; + try { + gzipInputStream =new GZIPInputStream(new FileInputStream(fileName)); + Reader decoder = new InputStreamReader(gzipInputStream, "UTF-8"); + reader = new BufferedReader(decoder); + String tempString; + //一次读一行,读入null时文件结束 + while ((tempString = reader.readLine()) != null) { + //把当前行号显示出来 + System.out.println(tempString); + } + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e1) { + } + } + } + } + + + /** + * 获取指定文件夹下所有文件,不含文件夹里的文件 + * + * @param dirFilePath 文件夹路径 + * @return + */ + public static List getAllFile(String dirFilePath) { + if (StrUtil.isBlank(dirFilePath)) + return null; + return getAllFile(new File(dirFilePath)); + } + + + /** + * 获取指定文件夹下所有文件,不含文件夹里的文件 + * + * @param dirFile 文件夹 + * @return + */ + public static List getAllFile(File dirFile) { + // 如果文件夹不存在或着不是文件夹,则返回 null + if (Objects.isNull(dirFile) || !dirFile.exists() || dirFile.isFile()) + return null; + + File[] childrenFiles = dirFile.listFiles(); + if (Objects.isNull(childrenFiles) || childrenFiles.length == 0) + return null; + + List files = new ArrayList<>(); + for (File childFile : childrenFiles) { + // 如果是文件,直接添加到结果集合 + if (childFile.isFile()) { + files.add(childFile); + } + //以下几行代码取消注释后可以将所有子文件夹里的文件也获取到列表里 +// else { +// // 如果是文件夹,则将其内部文件添加进结果集合 +// List cFiles = getAllFile(childFile); +// if (Objects.isNull(cFiles) || cFiles.isEmpty()) continue; +// files.addAll(cFiles); +// } + } + return files; + } + + + } diff --git a/src/test/java/org/springblade/test/BladeTest.java b/src/test/java/org/springblade/test/BladeTest.java deleted file mode 100644 index 600b816..0000000 --- a/src/test/java/org/springblade/test/BladeTest.java +++ /dev/null @@ -1,46 +0,0 @@ -package org.springblade.test; - -import com.baomidou.mybatisplus.core.toolkit.Wrappers; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.springblade.core.test.BladeBootTest; -import org.springblade.core.test.BladeSpringExtension; -import org.springblade.core.tool.utils.StringUtil; -import org.springblade.flow.engine.entity.FlowModel; -import org.springblade.flow.engine.service.FlowEngineService; -import org.springframework.beans.factory.annotation.Autowired; - -import java.util.List; - -/** - * Blade单元测试 - * - * @author Chill - */ -@ExtendWith(BladeSpringExtension.class) -@BladeBootTest(appName = "blade-runner", enableLoader = true) -public class BladeTest { - - @Autowired - private FlowEngineService service; - - @Test - public void contextLoads() { - System.out.println("=====数据迁移启动====="); - - // 获取 ACT_DE_MODEL 表需要转换的数据 - List list = service.list(); - // 循环转换 - list.forEach(flowModel -> { - if (StringUtil.isBlank(flowModel.getModelEditorXml())) { - service.update(Wrappers.lambdaUpdate() - .set(FlowModel::getModelEditorXml, new String(service.getModelEditorXML(flowModel))) - .ge(FlowModel::getId, flowModel.getId()) - ); - } - }); - - System.out.println("=====数据迁移完毕====="); - } - -} diff --git a/src/test/java/org/springblade/test/TestMethod.java b/src/test/java/org/springblade/test/TestMethod.java new file mode 100644 index 0000000..a04cc3f --- /dev/null +++ b/src/test/java/org/springblade/test/TestMethod.java @@ -0,0 +1,49 @@ +package org.springblade.test; + + +import lombok.AllArgsConstructor; +import org.junit.jupiter.api.Test; +import org.springblade.core.tool.utils.DateUtil; +import org.springblade.ftpdemo.controller.SSHRemoteCall; +import org.springblade.ftpdemo.service.Method; +import org.springblade.ftpdemo.service.impl.MethodImpl; +import org.springblade.ftpdemo.util.Constant; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.Date; +import java.util.Vector; + +/** + * @author laifeng + * @since 2022/11/30 + */ + +@SpringBootTest +public class TestMethod { + + + @Test + void test1() { + String operatorTime = "20221201000030"; + + Date date = DateUtil.parse(operatorTime, DateUtil.PATTERN_DATETIME_MINI); + String format = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss"); + System.out.println(format); + + + } + + @Test + void test2() throws Exception { + + // 1、首先远程连接ssh + SSHRemoteCall.getInstance().sshRemoteCallLogin(Constant.host, Constant.user,Constant.password1); + Vector files = SSHRemoteCall.getInstance().listFiles("/opt/2022-11-30"); + for (Object file : files) { + System.out.println(file.toString()); + } + + + } +} diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index 43a9a94..d83f83e 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -107,12 +107,12 @@ swagger: url: https://gitee.com/smallc #flowable配置 -flowable: - activity-font-name: \u5B8B\u4F53 - label-font-name: \u5B8B\u4F53 - annotation-font-name: \u5B8B\u4F53 - check-process-definitions: false - database-schema-update: false +#flowable: +# activity-font-name: \u5B8B\u4F53 +# label-font-name: \u5B8B\u4F53 +# annotation-font-name: \u5B8B\u4F53 +# check-process-definitions: false +# database-schema-update: false #报表配置 report: