diff --git a/src/main/java/com/xydl/cac/util/SFTPTool.java b/src/main/java/com/xydl/cac/util/SFTPTool.java index 3ea8a6e..459aa1e 100644 --- a/src/main/java/com/xydl/cac/util/SFTPTool.java +++ b/src/main/java/com/xydl/cac/util/SFTPTool.java @@ -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(); + } + } + }