controller层
package top.aaaaqqq.controller;
import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.aaaaqqq.pojo.Add;
import top.aaaaqqq.pojo.IP_address;
import top.aaaaqqq.service.IPService;
import java.util.List;
import java.util.Vector;
@Slf4j
@RestController
public class ZtController {
String firstFilePath="/home/zt/test2/";//本地路径第一部分
@Autowired
private IPService ipService;
@RequestMapping("/copy")
public String copy(@RequestParam("remoteFilePath")String remoteFilePath,
@RequestParam("fileEnd") String fileEnd){
// 通过远程目录/home/ubuntu/projects/FreewayEvent_v1/Release0/sample截取
// 本地路径第三部分:/Release0/sample
int index = remoteFilePath.lastIndexOf("/");//从后往前根据“/”截
int count = 0;
while (index >= 0 && count < 2 ) {
index = remoteFilePath.lastIndexOf("/", index - 1);
count++;
}
String lastFilePath = remoteFilePath.substring(index + 1);
// System.out.println(lastFilePath); /Release0/sample
// 查询数据库获取 IP 地址列表
List<IP_address> ipAddressList = ipService.selectAll();
//循环取,调用remoteCopy方法
for (IP_address ipAddress : ipAddressList) {
String ip = ipAddress.getIp();
String username = ipAddress.getUsername();
String password = ipAddress.getPassword();
int port = ipAddress.getPort();
// System.out.println(ip+username+password+port);
// 调用远程拷贝方法,localFilePath拼接成/home/zt/test2/902001/Release0/sample
remoteCopy(ip, username, password, port, remoteFilePath, firstFilePath+ipAddress.getId()+"/"+lastFilePath, fileEnd);
}
return "成功";
}
private void remoteCopy(String ip, String username, String password, int port, String remoteFilePath, String localFilePath, String fileEnd) {
// System.out.println(ip+username+password+port+remoteFilePath+localFilePath+fileEnd);
JSch jSch = new JSch();
Session session = null;
ChannelSftp channelSftp = null;
try {
// 创建 SSH Session
session = jSch.getSession(username, ip, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
// 创建 SFTP Channel
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
// 列出远程服务器上的文件列表
@SuppressWarnings("unchecked")
Vector<ChannelSftp.LsEntry> entries = channelSftp.ls(remoteFilePath+"/");
for (ChannelSftp.LsEntry entry : entries) {
// 判断是否是文件
if (!entry.getAttrs().isDir()) {
// 获取文件名
String fileName = entry.getFilename();
// System.out.println(fileName);
//截取后缀
String[] strings = fileName.split("\\.", 2);
String fileName_end = strings[1];//jpg
// System.out.println(fileName_end);
if (fileName_end.equals(fileEnd)) {
// 拼接远程服务器文件的完整路径
String remoteFile = remoteFilePath + "/" + fileName;
// 拼接本地保存文件的完整路径
String localFile = localFilePath + "/" + fileName;
// 下载文件
channelSftp.get(remoteFile, localFile);
System.out.println("成功拷贝"+fileName+"从" + remoteFilePath + " 到 " + localFilePath);
}else {
System.out.println(fileName+"不是"+fileEnd+"文件");
}
}
}
} catch (SftpException | JSchException e) {
log.error("远程拷贝文件异常",e);
} finally {
// 关闭 SFTP Channel 和 SSH Session
if (channelSftp != null) {
channelSftp.disconnect();
}
if (session != null) {
session.disconnect();
}
}
}
}