使用 maven导入 jar包
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
使用 这个包可以对接ssh 等常用的通信
创建对应的 Config信息映射
package com.lab.common.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "linux")
public class LinuxConfig {
private static String host;
private static int port;
private static String username;
private static String password;
private static String backupPath;
private static String upgradingPath;
public static String getHost() {
return host;
}
public void setHost(String host) {
LinuxConfig.host = host;
}
public static int getPort() {
return port;
}
public void setPort(int port) {
LinuxConfig.port = port;
}
public static String getUsername() {
return username;
}
public void setUsername(String username) {
LinuxConfig.username = username;
}
public static String getPassword() {
return password;
}
public void setPassword(String password) {
LinuxConfig.password = password;
}
public static String getBackupPath() {
return backupPath;
}
public void setBackupPath(String backupPath) {
LinuxConfig.backupPath = backupPath;
}
public static String getUpgradingPath() {
return upgradingPath;
}
public void setUpgradingPath(String upgradingPath) {
LinuxConfig.upgradingPath = upgradingPath;
}
}
在对应的 yml文件中添加
linux:
host: 192.168.1.101
port: 22
username: test
password: password
backupPath: /home/test/test
upgradingPath: /home/test/test1
创建实体Utils 类
package com.lab.common.utils.sftp;
import com.jcraft.jsch.*;
import java.io.*;
import java.util.List;
import java.util.Properties;
public class SFTPUtil {
/**
* 向Linux服务器通过SFTP上传文件
*
* @param host 服务器IP地址
* @param port SFTP服务端口(通常是22)
* @param username 登录用户名
* @param password 登录密码
* @param pathList 服务器上的目标路径(包括文件名)
* @param localFile 本地待上传文件的绝对路径
* @throws Exception 如果上传过程中发生错误
*/
public static void uploadFileToServer(String host, int port, String username, String password,
List<String> pathList, String localFile) throws Exception {
JSch jsch = new JSch();
Session session = null;
ChannelSftp channelSftp = null;
try {
// 创建Session并设置认证信息
session = jsch.getSession(username, host, port);
session.setPassword(password);
// 设置SSH连接配置,如跳过主机密钥检查
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
// 建立连接
session.connect();
// 打开SFTP通道
Channel channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
for (String path: pathList) {
sftpSyncFile(localFile, path, channelSftp);
}
System.out.println("File uploaded successfully");
} catch (Exception e) {
throw e;
} finally {
if (channelSftp != null) {
channelSftp.disconnect();
}
if (session != null) {
session.disconnect();
}
}
}
/**
* 判断目录是否存在
* @param directory
* @return
*/
public static boolean isDirExist(String directory, ChannelSftp sftp)
{
boolean isDirExistFlag = false;
try
{
SftpATTRS sftpATTRS = sftp.lstat(directory);
isDirExistFlag = true;
return sftpATTRS.isDir();
}
catch (Exception e)
{
if (e.getMessage().toLowerCase().equals("no such file"))
{
isDirExistFlag = false;
}
}
return isDirExistFlag;
}
public static void createMkdir(String directory, ChannelSftp sftp) throws SftpException {
String[] path = directory.split("/");
String sd = "";
for (int i = 1; i < path.length; i++) {
sd += "/"+path[i];
if(isDirExist(sd, sftp)){
sftp.cd(sd);
} else {
sftp.mkdir(sd);
sftp.cd(sd);
}
}
}
public static void sftpSyncFile(String localFile, String path, ChannelSftp channelSftp) throws SftpException, IOException {
// 读取本地文件作为输入流
File localFileObj = new File(localFile);
FileInputStream fileInputStream = new FileInputStream(localFileObj);
String cdPath = path.substring(0, path.lastIndexOf('/'));
if (isDirExist(cdPath,channelSftp)) {
// 上传文件到服务器指定路径
channelSftp.cd(cdPath); // 切换到目标目录
} else {
createMkdir(cdPath, channelSftp);
// 进入并设置为当前目录
channelSftp.cd(cdPath); // 切换到目标目录
}
channelSftp.put(fileInputStream, path.substring(path.lastIndexOf('/') + 1)); // 上传文件
fileInputStream.close();
}
}