feat: 增加sftp循环创建目录和上传文件功能

haikang
huangfeng 2 months ago
parent a33d677671
commit 52681d2edc

@ -1,6 +1,7 @@
package com.xydl.cac.util;
import com.jcraft.jsch.*;
import org.apache.commons.lang3.StringUtils;
import java.io.*;
import java.util.Properties;
@ -70,4 +71,38 @@ public class SFTPTool {
return sftp.ls(remotePath);
}
public void cdmkdir(String remotePath) throws SftpException {
if (StringUtils.isNotBlank(remotePath)) {
if (remotePath.startsWith("/")) {
sftp.cd("/");
}
String[] dirs = remotePath.split("/");
for (String dir : dirs) {
if (StringUtils.isNotBlank(dir)) {
try {
sftp.cd(dir);
} catch (SftpException e) {
sftp.mkdir(dir);
sftp.cd(dir);
}
}
}
}
}
public static void main(String[] args) {
SFTPTool sftpTool = new SFTPTool();
try {
String path = "abc/aaa";
sftpTool.connect("192.168.1.190", 22, "root", "123456");
sftpTool.cdmkdir(path);
sftpTool.upload("C:\\Code\\cac\\局放谱图\\50100055_5__001_01_20250409175800.dat",
"50100055_5__001_01_20250409175800.dat");
} catch (Exception e) {
e.printStackTrace();
} finally {
sftpTool.disconnect();
}
}
}

Loading…
Cancel
Save