Browse Source

第五次提交

master
李世鹏 3 years ago
parent
commit
9829ac67b3
  1. 42
      src/main/java/org/springblade/ftpdemo/controller/SSHRemoteCall.java
  2. 58
      src/main/java/org/springblade/ftpdemo/service/impl/MethodImpl.java
  3. 2
      src/main/java/org/springblade/ftpdemo/task/IdentificationUpTask.java
  4. 22
      src/main/java/org/springblade/ftpdemo/task/TestTask.java

42
src/main/java/org/springblade/ftpdemo/controller/SSHRemoteCall.java

@ -1,12 +1,10 @@
package org.springblade.ftpdemo.controller; package org.springblade.ftpdemo.controller;
import com.jcraft.jsch.*; import com.jcraft.jsch.*;
import org.springblade.ftpdemo.util.Constant;
import java.io.*; import java.io.*;
import java.util.HashMap; import java.util.*;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
public class SSHRemoteCall { public class SSHRemoteCall {
@ -84,7 +82,7 @@ public class SSHRemoteCall {
JSch jSch = new JSch(); JSch jSch = new JSch();
try { try {
// 获取到jSch的session, 根据用户名、主机ip、端口号获取一个Session对象 // 获取到jSch的session, 根据用户名、主机ip、端口号获取一个Session对象
session = jSch.getSession(userName, ipAddress, 22); //012 session = jSch.getSession(userName, ipAddress, 22102); //22102
// 设置密码 // 设置密码
session.setPassword(password); session.setPassword(password);
@ -201,7 +199,7 @@ public class SSHRemoteCall {
// 打开channelSftp // 打开channelSftp
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp"); ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
// 远程连接 // 远程连接
channelSftp.connect(); channelSftp.connect(6000);
// 创建一个文件名称问uploadFile的文件 // 创建一个文件名称问uploadFile的文件
File file = new File(uploadFile); File file = new File(uploadFile);
// 将文件进行上传(sftp协议) // 将文件进行上传(sftp协议)
@ -269,7 +267,7 @@ public class SSHRemoteCall {
try { try {
channelSftp = (ChannelSftp) session.openChannel("sftp"); channelSftp = (ChannelSftp) session.openChannel("sftp");
// 远程连接 // 远程连接
channelSftp.connect(); channelSftp.connect(6000);
// 切换到主目录 // 切换到主目录
// channelSftp.cd("/"); // channelSftp.cd("/");
} catch (JSchException e) { } catch (JSchException e) {
@ -289,12 +287,11 @@ public class SSHRemoteCall {
channelSftp.cd(folder); channelSftp.cd(folder);
} catch (SftpException ex) { } catch (SftpException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
} }
} }
} }
channelSftp.disconnect();
} }
@ -352,44 +349,37 @@ public class SSHRemoteCall {
* @throws SftpException * @throws SftpException
* @throws JSchException * @throws JSchException
*/ */
public void listFiles(String directory) throws JSchException, SftpException { public List<String> listFiles(String directory) throws JSchException, SftpException {
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp"); ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
// 远程连接 // 远程连接
channelSftp.connect(); channelSftp.connect();
// 显示目录信息 // 显示目录信息
Vector ls = channelSftp.ls(directory); Vector ls = channelSftp.ls(directory);
Iterator iterator = ls.iterator();
Map<String, File> fileMap = new HashMap<>(); Iterator iterator = ls.iterator();
try { try {
ArrayList<String> list = new ArrayList<>();
while (iterator.hasNext()) { while (iterator.hasNext()) {
ChannelSftp.LsEntry file = (ChannelSftp.LsEntry) iterator.next(); ChannelSftp.LsEntry file = (ChannelSftp.LsEntry) iterator.next();
//文件名称 //文件名称
String fileName = file.getFilename(); String fileName = file.getFilename();
System.out.println(fileName); //移除上级目录和根目录:"." ".."
//todo 这里可使用if或正则不下载一些文件 if (".".equals(fileName) || "..".equals(fileName)) {
// InputStream inputStream = channelSftp.get(directory + fileName); continue;
// if (inputStream != null) { }
// File newFile = new File(fileName); list.add(fileName);
// //将InputStream转File
//// FileUtils.copyToFile(inputStream, newFile);
// fileMap.put(fileName, newFile);
// }
} }
return list;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
// 切断连接 // 切断连接
channelSftp.exit(); channelSftp.exit();
} }
return null;
} }
} }

58
src/main/java/org/springblade/ftpdemo/service/impl/MethodImpl.java

@ -19,8 +19,7 @@ import org.springblade.ftpdemo.util.Encrypt;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.*; import java.io.*;
import java.util.Date; import java.util.*;
import java.util.List;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
/** /**
@ -67,15 +66,14 @@ public class MethodImpl implements Method {
// 远程连接服务器 // 远程连接服务器
SSHRemoteCall.getInstance().sshRemoteCallLogin(Constant.ipAddress, Constant.userName, Constant.password); SSHRemoteCall.getInstance().sshRemoteCallLogin(Constant.ipAddress, Constant.userName, Constant.password);
// 确保目录一定存在 // 确保目录一定存在
SSHRemoteCall.getInstance().directory(upSrc); SSHRemoteCall.getInstance().directory(upSrc);
// 上传文件到指定目录 // 上传文件到指定目录
// SSHRemoteCall.getInstance().uploadFile("/data1/qingniao/" + upSrc + "/" + fileName, src); // SSHRemoteCall.getInstance().uploadFile("/data1/qingniao/" + upSrc + "/" + fileName, src);
SSHRemoteCall.getInstance().uploadFile("/data1/qingniao/" + upSrc + "/" + fileName, src); SSHRemoteCall.getInstance().uploadFile("/data1/qingniao/" + upSrc + "/" + fileName, src);
// 关闭连接 // // 关闭连接
SSHRemoteCall.getInstance().closeSession(); // SSHRemoteCall.getInstance().closeSession();
} }
@ -280,15 +278,16 @@ public class MethodImpl implements Method {
// 连接远程服务器,日志存放地 // 连接远程服务器,日志存放地
Date date = DateUtil.minusDays(new Date(), 1); Date date = DateUtil.minusDays(new Date(), 1);
String yesterday = DateUtil.formatDate(date); String yesterday = DateUtil.formatDate(date);
List<File> allFile = Decrypt.getAllFile("/opt/" + yesterday); // SSHRemoteCall.getInstance().sshRemoteCallLogin(Constant.host, Constant.user,Constant.password1);
String directoryPath = "/opt/" + yesterday;
List<String> allFile = listFiles(directoryPath,sftp);
if (Func.isEmpty(allFile)){ if (Func.isEmpty(allFile)){
return; return;
} }
for (File file : allFile) { for (String filePath : allFile) {
System.out.println("成功进入");
String filePath = file.getName();
// 读取远程服务器上的日志文件 gz压缩格式 // 读取远程服务器上的日志文件 gz压缩格式
GZIPInputStream gzipInputStream = new GZIPInputStream(sftp.get(filePath)); GZIPInputStream gzipInputStream = new GZIPInputStream(sftp.get(directoryPath+"/"+filePath));
Reader decoder = new InputStreamReader(gzipInputStream, "UTF-8"); Reader decoder = new InputStreamReader(gzipInputStream, "UTF-8");
BufferedReader reader = new BufferedReader(decoder); BufferedReader reader = new BufferedReader(decoder);
// 创建输出流 // 创建输出流
@ -340,11 +339,10 @@ public class MethodImpl implements Method {
} }
System.out.println("w完成");
sftp.disconnect(); sftp.disconnect();
session.disconnect(); session.disconnect();
// SSHRemoteCall.getInstance().closeSession();
} }
@ -378,4 +376,38 @@ public class MethodImpl implements Method {
return null; return null;
} }
/**
* 列出目录下的文件
*
* @param directory 要列出的目录
* @return
* @throws SftpException
* @throws JSchException
*/
private List<String> listFiles(String directory,ChannelSftp channelSftp) throws JSchException, SftpException {
// 显示目录信息
Vector ls = channelSftp.ls(directory);
Iterator iterator = ls.iterator();
try {
ArrayList<String> list = new ArrayList<>();
while (iterator.hasNext()) {
ChannelSftp.LsEntry file = (ChannelSftp.LsEntry) iterator.next();
//文件名称
String fileName = file.getFilename();
//移除上级目录和根目录:"." ".."
if (".".equals(fileName) || "..".equals(fileName)) {
continue;
}
list.add(fileName);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
} }

2
src/main/java/org/springblade/ftpdemo/task/IdentificationUpTask.java

@ -24,7 +24,7 @@ public class IdentificationUpTask {
private final NbmsEntApplyServcie nbmsEntApplyServcie; private final NbmsEntApplyServcie nbmsEntApplyServcie;
private final Method method; private final Method method;
@Scheduled(cron = "0 1/10 * * * ?") @Scheduled(cron = "0/20 * * * * ? ")
public void refreshAliDeviceStatus() { public void refreshAliDeviceStatus() {
// 标识解析企业主体信息上报检测新增并上传 // 标识解析企业主体信息上报检测新增并上传
threePackMethod(0); threePackMethod(0);

22
src/main/java/org/springblade/ftpdemo/task/TestTask.java

@ -10,6 +10,7 @@ import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch; import org.springframework.util.StopWatch;
import java.util.List;
import java.util.Vector; import java.util.Vector;
/** /**
@ -22,21 +23,16 @@ import java.util.Vector;
public class TestTask { public class TestTask {
private final Method method; private final Method method;
@Scheduled(cron = "0 0/1 * * * ?") // @Scheduled(cron = "0/8 * * * * ? ")
public void refreshAliDeviceStatus() throws Exception { public void refreshAliDeviceStatus() throws Exception {
// StopWatch stopWatch = new StopWatch("读取表6日志,生成加密文件,上传" ); StopWatch stopWatch = new StopWatch("读取表6日志,生成加密文件,上传" );
// stopWatch.start(); stopWatch.start();
// String yyyyMMdd = DateUtil.format(DateUtil.now(), "yyyyMMdd"); String yyyyMMdd = DateUtil.format(DateUtil.now(), "yyyyMMdd");
//
// method.creatSixUpFileAndUp(yyyyMMdd, "1");
// stopWatch.stop();
// double seconds = stopWatch.getTotalTimeSeconds();
// System.out.println("完成,花费:"+seconds);
method.creatSixUpFileAndUp(yyyyMMdd, "1");
// 1、首先远程连接ssh stopWatch.stop();
SSHRemoteCall.getInstance().sshRemoteCallLogin(Constant.host, Constant.user,Constant.password1); double seconds = stopWatch.getTotalTimeSeconds();
SSHRemoteCall.getInstance().listFiles("/opt/2022-11-30"); System.out.println("完成,花费:"+seconds);
} }
} }

Loading…
Cancel
Save