解决pwd、cd调用command导致仅SftpSubsystem服务时无法正常使用的问题

This commit is contained in:
Looly
2025-05-12 12:08:30 +08:00
parent 255ff52071
commit 11fc243705
4 changed files with 90 additions and 38 deletions

View File

@@ -648,17 +648,17 @@ public class CommonsFtp extends AbstractFtp {
* 递归下载FTP服务器上文件到本地(文件目录和服务器同步)
*
* @param sourcePath ftp服务器目录
* @param destDir 本地目录
* @param targetDir 本地目录
*/
@Override
public void recursiveDownloadFolder(final String sourcePath, final File destDir) {
public void recursiveDownloadFolder(final String sourcePath, final File targetDir) {
String fileName;
String srcFile;
File destFile;
for (final FTPFile ftpFile : lsFiles(sourcePath, null)) {
fileName = ftpFile.getName();
srcFile = StrUtil.format("{}/{}", sourcePath, fileName);
destFile = FileUtil.file(destDir, fileName);
destFile = FileUtil.file(targetDir, fileName);
if (!ftpFile.isDirectory()) {
// 本地不存在文件或者ftp上文件有修改则下载

View File

@@ -174,10 +174,10 @@ public interface Ftp extends Closeable {
* 递归下载FTP服务器上文件到本地(文件目录和服务器同步), 服务器上有新文件会覆盖本地文件
*
* @param sourcePath ftp服务器目录
* @param destDir 本地目录
* @param targetDir 本地目录
* @since 5.3.5
*/
void recursiveDownloadFolder(String sourcePath, File destDir);
void recursiveDownloadFolder(String sourcePath, File targetDir);
/**
* 读取FTP服务器上的文件为输入流

View File

@@ -593,17 +593,17 @@ public class JschSftp extends AbstractFtp {
* 递归下载FTP服务器上文件到本地(文件目录和服务器同步)
*
* @param sourcePath ftp服务器目录必须为目录
* @param destDir 本地目录
* @param targetDir 本地目录
*/
@Override
public void recursiveDownloadFolder(final String sourcePath, final File destDir) throws SshException {
public void recursiveDownloadFolder(final String sourcePath, final File targetDir) throws SshException {
String fileName;
String srcFile;
File destFile;
for (final LsEntry item : lsEntries(sourcePath)) {
fileName = item.getFilename();
srcFile = StrUtil.format("{}/{}", sourcePath, fileName);
destFile = FileUtil.file(destDir, fileName);
destFile = FileUtil.file(targetDir, fileName);
if (!item.getAttrs().isDir()) {
// 本地不存在文件或者ftp上文件有修改则下载

View File

@@ -16,20 +16,21 @@
package cn.hutool.v7.extra.ssh.engine.sshj;
import cn.hutool.v7.core.collection.CollUtil;
import cn.hutool.v7.core.io.IORuntimeException;
import cn.hutool.v7.core.io.IoUtil;
import cn.hutool.v7.core.io.file.FileUtil;
import cn.hutool.v7.core.text.StrUtil;
import cn.hutool.v7.extra.ftp.AbstractFtp;
import cn.hutool.v7.extra.ftp.FtpConfig;
import cn.hutool.v7.extra.ftp.FtpException;
import cn.hutool.v7.extra.ssh.Connector;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.sftp.RemoteFile;
import net.schmizz.sshj.sftp.RemoteResourceInfo;
import net.schmizz.sshj.sftp.SFTPClient;
import net.schmizz.sshj.xfer.FileSystemFile;
import cn.hutool.v7.core.collection.CollUtil;
import cn.hutool.v7.core.io.IORuntimeException;
import cn.hutool.v7.core.io.IoUtil;
import cn.hutool.v7.core.text.StrUtil;
import cn.hutool.v7.extra.ftp.AbstractFtp;
import cn.hutool.v7.extra.ftp.FtpConfig;
import cn.hutool.v7.extra.ftp.FtpException;
import cn.hutool.v7.extra.ssh.Connector;
import java.io.File;
import java.io.IOException;
@@ -51,6 +52,7 @@ import java.util.List;
public class SshjSftp extends AbstractFtp {
// region ----- of
/**
* 构造
*
@@ -94,6 +96,7 @@ public class SshjSftp extends AbstractFtp {
private SSHClient ssh;
private SFTPClient sftp;
private Session session;
private String workingDir;
/**
* 构造
@@ -125,7 +128,7 @@ public class SshjSftp extends AbstractFtp {
* @since 5.7.18
*/
public void init() {
if(null == this.ssh){
if (null == this.ssh) {
this.ssh = SshjUtil.openClient(this.ftpConfig.getConnector());
}
@@ -153,32 +156,41 @@ public class SshjSftp extends AbstractFtp {
@Override
public boolean cd(final String directory) {
final String exec = String.format("cd %s", directory);
command(exec);
final String pwd = pwd();
return pwd.equals(directory);
// final String exec = String.format("cd %s", directory);
// command(exec);
// final 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 command("pwd");
return getPath(null);
}
@Override
public boolean mkdir(final String dir) {
try {
sftp.mkdir(dir);
sftp.mkdir(getPath(dir));
} catch (final IOException e) {
throw new FtpException(e);
}
return containsFile(dir);
return containsFile(getPath(dir));
}
@Override
public List<String> ls(final String path) {
final List<RemoteResourceInfo> infoList;
try {
infoList = sftp.ls(path);
infoList = sftp.ls(getPath(path));
} catch (final IOException e) {
throw new FtpException(e);
}
@@ -201,8 +213,8 @@ public class SshjSftp extends AbstractFtp {
@Override
public boolean delFile(final String path) {
try {
sftp.rm(path);
return !containsFile(path);
sftp.rm(getPath(path));
return !containsFile(getPath(path));
} catch (final IOException e) {
throw new FtpException(e);
}
@@ -211,18 +223,21 @@ public class SshjSftp extends AbstractFtp {
@Override
public boolean delDir(final String dirPath) {
try {
sftp.rmdir(dirPath);
return !containsFile(dirPath);
sftp.rmdir(getPath(dirPath));
return !containsFile(getPath(dirPath));
} catch (final IOException e) {
throw new FtpException(e);
}
}
@Override
public boolean uploadFile(final String destPath, final File file) {
public boolean uploadFile(String destPath, final 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 (final IOException e) {
throw new FtpException(e);
}
@@ -231,17 +246,25 @@ public class SshjSftp extends AbstractFtp {
@Override
public void download(final String path, final File outFile) {
try {
sftp.get(path, new FileSystemFile(outFile));
sftp.get(getPath(path), new FileSystemFile(outFile));
} catch (final IOException e) {
throw new FtpException(e);
}
}
@Override
public void recursiveDownloadFolder(final String sourcePath, final File destDir) {
final List<String> files = ls(sourcePath);
if (files != null && !files.isEmpty()) {
files.forEach(path -> download(sourcePath + "/" + path, destDir));
public void recursiveDownloadFolder(final String sourcePath, final File targetDir) {
if (!targetDir.exists()) {
if (!targetDir.mkdirs()) {
throw new FtpException("Dir {} create failed!", targetDir.getAbsolutePath());
}
}else if(!targetDir.isDirectory()){
throw new FtpException("Target is not a directory!");
}
List<String> files = ls(getPath(sourcePath));
if (CollUtil.isNotEmpty(files)) {
files.forEach(file -> download(sourcePath + StrUtil.SLASH + file, FileUtil.file(targetDir, file)));
}
}
@@ -280,7 +303,7 @@ public class SshjSftp extends AbstractFtp {
*/
public boolean containsFile(final String fileDir) {
try {
sftp.lstat(fileDir);
sftp.lstat(getPath(fileDir));
return true;
} catch (final IOException e) {
return false;
@@ -329,4 +352,33 @@ public class SshjSftp extends AbstractFtp {
}
return session;
}
/**
* 获取对应路径的绝对路径<br>
* 如果提供的为绝对路径,直接返回,否则拼接当前路径
*
* @param path 路径
* @return 绝对路径
*/
private String getPath(final String path) {
if (StrUtil.isBlank(this.workingDir)) {
try {
this.workingDir = sftp.canonicalize(StrUtil.EMPTY);
} catch (IOException e) {
throw new FtpException(e);
}
}
if (StrUtil.isBlank(path)) {
return this.workingDir;
}
// 如果是绝对路径,则返回
if (StrUtil.startWith(path, StrUtil.SLASH)) {
return path;
}
final String tmp = StrUtil.removeSuffix(this.workingDir, StrUtil.SLASH);
return StrUtil.format("{}/{}", tmp, path);
}
}