mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
解决pwd、cd调用command导致仅SftpSubsystem服务时无法正常使用的问题
This commit is contained in:
@@ -648,17 +648,17 @@ public class CommonsFtp extends AbstractFtp {
|
|||||||
* 递归下载FTP服务器上文件到本地(文件目录和服务器同步)
|
* 递归下载FTP服务器上文件到本地(文件目录和服务器同步)
|
||||||
*
|
*
|
||||||
* @param sourcePath ftp服务器目录
|
* @param sourcePath ftp服务器目录
|
||||||
* @param destDir 本地目录
|
* @param targetDir 本地目录
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void recursiveDownloadFolder(final String sourcePath, final File destDir) {
|
public void recursiveDownloadFolder(final String sourcePath, final File targetDir) {
|
||||||
String fileName;
|
String fileName;
|
||||||
String srcFile;
|
String srcFile;
|
||||||
File destFile;
|
File destFile;
|
||||||
for (final FTPFile ftpFile : lsFiles(sourcePath, null)) {
|
for (final FTPFile ftpFile : lsFiles(sourcePath, null)) {
|
||||||
fileName = ftpFile.getName();
|
fileName = ftpFile.getName();
|
||||||
srcFile = StrUtil.format("{}/{}", sourcePath, fileName);
|
srcFile = StrUtil.format("{}/{}", sourcePath, fileName);
|
||||||
destFile = FileUtil.file(destDir, fileName);
|
destFile = FileUtil.file(targetDir, fileName);
|
||||||
|
|
||||||
if (!ftpFile.isDirectory()) {
|
if (!ftpFile.isDirectory()) {
|
||||||
// 本地不存在文件或者ftp上文件有修改则下载
|
// 本地不存在文件或者ftp上文件有修改则下载
|
||||||
|
@@ -174,10 +174,10 @@ public interface Ftp extends Closeable {
|
|||||||
* 递归下载FTP服务器上文件到本地(文件目录和服务器同步), 服务器上有新文件会覆盖本地文件
|
* 递归下载FTP服务器上文件到本地(文件目录和服务器同步), 服务器上有新文件会覆盖本地文件
|
||||||
*
|
*
|
||||||
* @param sourcePath ftp服务器目录
|
* @param sourcePath ftp服务器目录
|
||||||
* @param destDir 本地目录
|
* @param targetDir 本地目录
|
||||||
* @since 5.3.5
|
* @since 5.3.5
|
||||||
*/
|
*/
|
||||||
void recursiveDownloadFolder(String sourcePath, File destDir);
|
void recursiveDownloadFolder(String sourcePath, File targetDir);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 读取FTP服务器上的文件为输入流
|
* 读取FTP服务器上的文件为输入流
|
||||||
|
@@ -593,17 +593,17 @@ public class JschSftp extends AbstractFtp {
|
|||||||
* 递归下载FTP服务器上文件到本地(文件目录和服务器同步)
|
* 递归下载FTP服务器上文件到本地(文件目录和服务器同步)
|
||||||
*
|
*
|
||||||
* @param sourcePath ftp服务器目录,必须为目录
|
* @param sourcePath ftp服务器目录,必须为目录
|
||||||
* @param destDir 本地目录
|
* @param targetDir 本地目录
|
||||||
*/
|
*/
|
||||||
@Override
|
@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 fileName;
|
||||||
String srcFile;
|
String srcFile;
|
||||||
File destFile;
|
File destFile;
|
||||||
for (final LsEntry item : lsEntries(sourcePath)) {
|
for (final LsEntry item : lsEntries(sourcePath)) {
|
||||||
fileName = item.getFilename();
|
fileName = item.getFilename();
|
||||||
srcFile = StrUtil.format("{}/{}", sourcePath, fileName);
|
srcFile = StrUtil.format("{}/{}", sourcePath, fileName);
|
||||||
destFile = FileUtil.file(destDir, fileName);
|
destFile = FileUtil.file(targetDir, fileName);
|
||||||
|
|
||||||
if (!item.getAttrs().isDir()) {
|
if (!item.getAttrs().isDir()) {
|
||||||
// 本地不存在文件或者ftp上文件有修改则下载
|
// 本地不存在文件或者ftp上文件有修改则下载
|
||||||
|
@@ -16,20 +16,21 @@
|
|||||||
|
|
||||||
package cn.hutool.v7.extra.ssh.engine.sshj;
|
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.SSHClient;
|
||||||
import net.schmizz.sshj.connection.channel.direct.Session;
|
import net.schmizz.sshj.connection.channel.direct.Session;
|
||||||
import net.schmizz.sshj.sftp.RemoteFile;
|
import net.schmizz.sshj.sftp.RemoteFile;
|
||||||
import net.schmizz.sshj.sftp.RemoteResourceInfo;
|
import net.schmizz.sshj.sftp.RemoteResourceInfo;
|
||||||
import net.schmizz.sshj.sftp.SFTPClient;
|
import net.schmizz.sshj.sftp.SFTPClient;
|
||||||
import net.schmizz.sshj.xfer.FileSystemFile;
|
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.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -51,6 +52,7 @@ import java.util.List;
|
|||||||
public class SshjSftp extends AbstractFtp {
|
public class SshjSftp extends AbstractFtp {
|
||||||
|
|
||||||
// region ----- of
|
// region ----- of
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
*
|
*
|
||||||
@@ -94,6 +96,7 @@ public class SshjSftp extends AbstractFtp {
|
|||||||
private SSHClient ssh;
|
private SSHClient ssh;
|
||||||
private SFTPClient sftp;
|
private SFTPClient sftp;
|
||||||
private Session session;
|
private Session session;
|
||||||
|
private String workingDir;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
@@ -125,7 +128,7 @@ public class SshjSftp extends AbstractFtp {
|
|||||||
* @since 5.7.18
|
* @since 5.7.18
|
||||||
*/
|
*/
|
||||||
public void init() {
|
public void init() {
|
||||||
if(null == this.ssh){
|
if (null == this.ssh) {
|
||||||
this.ssh = SshjUtil.openClient(this.ftpConfig.getConnector());
|
this.ssh = SshjUtil.openClient(this.ftpConfig.getConnector());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,32 +156,41 @@ public class SshjSftp extends AbstractFtp {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean cd(final String directory) {
|
public boolean cd(final String directory) {
|
||||||
final String exec = String.format("cd %s", directory);
|
// final String exec = String.format("cd %s", directory);
|
||||||
command(exec);
|
// command(exec);
|
||||||
final String pwd = pwd();
|
// final String pwd = pwd();
|
||||||
return pwd.equals(directory);
|
// 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
|
@Override
|
||||||
public String pwd() {
|
public String pwd() {
|
||||||
return command("pwd");
|
// return command("pwd");
|
||||||
|
return getPath(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean mkdir(final String dir) {
|
public boolean mkdir(final String dir) {
|
||||||
try {
|
try {
|
||||||
sftp.mkdir(dir);
|
sftp.mkdir(getPath(dir));
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
throw new FtpException(e);
|
throw new FtpException(e);
|
||||||
}
|
}
|
||||||
return containsFile(dir);
|
return containsFile(getPath(dir));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> ls(final String path) {
|
public List<String> ls(final String path) {
|
||||||
final List<RemoteResourceInfo> infoList;
|
final List<RemoteResourceInfo> infoList;
|
||||||
try {
|
try {
|
||||||
infoList = sftp.ls(path);
|
infoList = sftp.ls(getPath(path));
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
throw new FtpException(e);
|
throw new FtpException(e);
|
||||||
}
|
}
|
||||||
@@ -201,8 +213,8 @@ public class SshjSftp extends AbstractFtp {
|
|||||||
@Override
|
@Override
|
||||||
public boolean delFile(final String path) {
|
public boolean delFile(final String path) {
|
||||||
try {
|
try {
|
||||||
sftp.rm(path);
|
sftp.rm(getPath(path));
|
||||||
return !containsFile(path);
|
return !containsFile(getPath(path));
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
throw new FtpException(e);
|
throw new FtpException(e);
|
||||||
}
|
}
|
||||||
@@ -211,18 +223,21 @@ public class SshjSftp extends AbstractFtp {
|
|||||||
@Override
|
@Override
|
||||||
public boolean delDir(final String dirPath) {
|
public boolean delDir(final String dirPath) {
|
||||||
try {
|
try {
|
||||||
sftp.rmdir(dirPath);
|
sftp.rmdir(getPath(dirPath));
|
||||||
return !containsFile(dirPath);
|
return !containsFile(getPath(dirPath));
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
throw new FtpException(e);
|
throw new FtpException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean uploadFile(final String destPath, final File file) {
|
public boolean uploadFile(String destPath, final File file) {
|
||||||
try {
|
try {
|
||||||
sftp.put(new FileSystemFile(file), destPath);
|
if (StrUtil.endWith(destPath, StrUtil.SLASH)) {
|
||||||
return containsFile(destPath);
|
destPath += file.getName();
|
||||||
|
}
|
||||||
|
sftp.put(new FileSystemFile(file), getPath(destPath));
|
||||||
|
return containsFile(getPath(destPath));
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
throw new FtpException(e);
|
throw new FtpException(e);
|
||||||
}
|
}
|
||||||
@@ -231,17 +246,25 @@ public class SshjSftp extends AbstractFtp {
|
|||||||
@Override
|
@Override
|
||||||
public void download(final String path, final File outFile) {
|
public void download(final String path, final File outFile) {
|
||||||
try {
|
try {
|
||||||
sftp.get(path, new FileSystemFile(outFile));
|
sftp.get(getPath(path), new FileSystemFile(outFile));
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
throw new FtpException(e);
|
throw new FtpException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void recursiveDownloadFolder(final String sourcePath, final File destDir) {
|
public void recursiveDownloadFolder(final String sourcePath, final File targetDir) {
|
||||||
final List<String> files = ls(sourcePath);
|
if (!targetDir.exists()) {
|
||||||
if (files != null && !files.isEmpty()) {
|
if (!targetDir.mkdirs()) {
|
||||||
files.forEach(path -> download(sourcePath + "/" + path, destDir));
|
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) {
|
public boolean containsFile(final String fileDir) {
|
||||||
try {
|
try {
|
||||||
sftp.lstat(fileDir);
|
sftp.lstat(getPath(fileDir));
|
||||||
return true;
|
return true;
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
return false;
|
return false;
|
||||||
@@ -329,4 +352,33 @@ public class SshjSftp extends AbstractFtp {
|
|||||||
}
|
}
|
||||||
return session;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user