From 81159f75bb7dc7821decf83a9daa68f3b4da1246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8E=89=E5=86=9B?= Date: Wed, 30 Apr 2025 15:20:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3pwd=E3=80=81cd=E8=B0=83?= =?UTF-8?q?=E7=94=A8command=E5=AF=BC=E8=87=B4=E4=BB=85SftpSubsystem?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E6=97=B6=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/extra/ssh/SshjSftp.java | 78 ++++++++++++++----- 1 file changed, 60 insertions(+), 18 deletions(-) diff --git a/hutool-extra/src/main/java/cn/hutool/extra/ssh/SshjSftp.java b/hutool-extra/src/main/java/cn/hutool/extra/ssh/SshjSftp.java index 9f6c47e3b..e3217d11a 100644 --- a/hutool-extra/src/main/java/cn/hutool/extra/ssh/SshjSftp.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/ssh/SshjSftp.java @@ -1,6 +1,7 @@ package cn.hutool.extra.ssh; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.StrUtil; @@ -36,6 +37,7 @@ public class SshjSftp extends AbstractFtp { private SSHClient ssh; private SFTPClient sftp; private Session session; + private String workingDir; /** * 构造,使用默认端口 @@ -126,34 +128,65 @@ public class SshjSftp extends AbstractFtp { return this; } + private String getPath(String path) { + if (StrUtil.isBlank(this.workingDir)) { + try { + this.workingDir = sftp.canonicalize(""); + } catch (IOException e) { + throw new FtpException(e); + } + } + + if (StrUtil.isBlank(path)) { + return this.workingDir; + } + + // 如果是绝对路径,则返回 + if (StrUtil.startWith(path, StrUtil.SLASH)) { + return path; + } else { + String tmp = StrUtil.removeSuffix(this.workingDir, StrUtil.SLASH); + return StrUtil.format("{}/{}", tmp, path); + } + } + + /** + * 改变目录,注意目前不支持.. + * @param directory directory + * @return true:成功 + */ @Override public boolean cd(String directory) { - String exec = String.format("cd %s", directory); - command(exec); - String pwd = pwd(); - return pwd.equals(directory); + String newPath = getPath(directory); + try { + sftp.ls(newPath); + this.workingDir = newPath; + return true; + } catch (IOException e) { + throw new FtpException(e); + } } @Override public String pwd() { - return command("pwd"); + return getPath(null); } @Override public boolean mkdir(String dir) { try { - sftp.mkdir(dir); + sftp.mkdir(getPath(dir)); } catch (IOException e) { throw new FtpException(e); } - return containsFile(dir); + return containsFile(getPath(dir)); } @Override public List ls(String path) { List infoList; try { - infoList = sftp.ls(path); + infoList = sftp.ls(getPath(path)); } catch (IOException e) { throw new FtpException(e); } @@ -166,8 +199,8 @@ public class SshjSftp extends AbstractFtp { @Override public boolean delFile(String path) { try { - sftp.rm(path); - return !containsFile(path); + sftp.rm(getPath(path)); + return !containsFile(getPath(path)); } catch (IOException e) { throw new FtpException(e); } @@ -176,8 +209,8 @@ public class SshjSftp extends AbstractFtp { @Override public boolean delDir(String dirPath) { try { - sftp.rmdir(dirPath); - return !containsFile(dirPath); + sftp.rmdir(getPath(dirPath)); + return !containsFile(getPath(dirPath)); } catch (IOException e) { throw new FtpException(e); } @@ -186,8 +219,11 @@ public class SshjSftp extends AbstractFtp { @Override public boolean upload(String destPath, File file) { try { - sftp.put(new FileSystemFile(file), destPath); - return containsFile(destPath); + if (StrUtil.endWith(destPath, StrUtil.SLASH)) { + destPath += file.getName(); + } + sftp.put(new FileSystemFile(file), getPath(destPath)); + return containsFile(getPath(destPath)); } catch (IOException e) { throw new FtpException(e); } @@ -196,7 +232,7 @@ public class SshjSftp extends AbstractFtp { @Override public void download(String path, File outFile) { try { - sftp.get(path, new FileSystemFile(outFile)); + sftp.get(getPath(path), new FileSystemFile(outFile)); } catch (IOException e) { throw new FtpException(e); } @@ -204,9 +240,15 @@ public class SshjSftp extends AbstractFtp { @Override public void recursiveDownloadFolder(String sourcePath, File destDir) { - List files = ls(sourcePath); + if (!destDir.exists() || !destDir.isDirectory()) { + if (!destDir.mkdirs()) { + throw new FtpException("创建目录" + destDir.getAbsolutePath() + "失败"); + } + } + + List files = ls(getPath(sourcePath)); if (files != null && !files.isEmpty()) { - files.forEach(path -> download(sourcePath + "/" + path, destDir)); + files.forEach(file -> download(sourcePath + "/" + file, FileUtil.file(destDir, file))); } } @@ -236,7 +278,7 @@ public class SshjSftp extends AbstractFtp { */ public boolean containsFile(String fileDir) { try { - sftp.lstat(fileDir); + sftp.lstat(getPath(fileDir)); return true; } catch (IOException e) { return false;