远程拷贝多个服务器中的文件

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();
}
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值