mirror of
https://gitee.com/dromara/hutool.git
synced 2025-06-28 13:34:09 +08:00
!1338 解决pwd、cd调用command导致仅SftpSubsystem服务时无法正常使用的问题
Merge pull request !1338 from 厉军/v5-dev
This commit is contained in:
commit
25327585b1
@ -756,4 +756,5 @@ public class Ftp extends AbstractFtp {
|
|||||||
this.client = null;
|
this.client = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package cn.hutool.extra.ssh;
|
package cn.hutool.extra.ssh;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
import cn.hutool.core.io.IoUtil;
|
import cn.hutool.core.io.IoUtil;
|
||||||
import cn.hutool.core.util.CharsetUtil;
|
import cn.hutool.core.util.CharsetUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
@ -36,6 +37,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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造,使用默认端口
|
* 构造,使用默认端口
|
||||||
@ -88,7 +90,7 @@ public class SshjSftp extends AbstractFtp {
|
|||||||
* @param config FTP配置
|
* @param config FTP配置
|
||||||
* @since 5.3.3
|
* @since 5.3.3
|
||||||
*/
|
*/
|
||||||
protected SshjSftp(FtpConfig config) {
|
public SshjSftp(FtpConfig config) {
|
||||||
super(config);
|
super(config);
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
@ -126,34 +128,65 @@ public class SshjSftp extends AbstractFtp {
|
|||||||
return this;
|
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
|
@Override
|
||||||
public boolean cd(String directory) {
|
public boolean cd(String directory) {
|
||||||
String exec = String.format("cd %s", directory);
|
String newPath = getPath(directory);
|
||||||
command(exec);
|
try {
|
||||||
String pwd = pwd();
|
sftp.ls(newPath);
|
||||||
return pwd.equals(directory);
|
this.workingDir = newPath;
|
||||||
|
return true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new FtpException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String pwd() {
|
public String pwd() {
|
||||||
return command("pwd");
|
return getPath(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean mkdir(String dir) {
|
public boolean mkdir(String dir) {
|
||||||
try {
|
try {
|
||||||
sftp.mkdir(dir);
|
sftp.mkdir(getPath(dir));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new FtpException(e);
|
throw new FtpException(e);
|
||||||
}
|
}
|
||||||
return containsFile(dir);
|
return containsFile(getPath(dir));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> ls(String path) {
|
public List<String> ls(String path) {
|
||||||
List<RemoteResourceInfo> infoList;
|
List<RemoteResourceInfo> infoList;
|
||||||
try {
|
try {
|
||||||
infoList = sftp.ls(path);
|
infoList = sftp.ls(getPath(path));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new FtpException(e);
|
throw new FtpException(e);
|
||||||
}
|
}
|
||||||
@ -166,8 +199,8 @@ public class SshjSftp extends AbstractFtp {
|
|||||||
@Override
|
@Override
|
||||||
public boolean delFile(String path) {
|
public boolean delFile(String path) {
|
||||||
try {
|
try {
|
||||||
sftp.rm(path);
|
sftp.rm(getPath(path));
|
||||||
return !containsFile(path);
|
return !containsFile(getPath(path));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new FtpException(e);
|
throw new FtpException(e);
|
||||||
}
|
}
|
||||||
@ -176,8 +209,8 @@ public class SshjSftp extends AbstractFtp {
|
|||||||
@Override
|
@Override
|
||||||
public boolean delDir(String dirPath) {
|
public boolean delDir(String dirPath) {
|
||||||
try {
|
try {
|
||||||
sftp.rmdir(dirPath);
|
sftp.rmdir(getPath(dirPath));
|
||||||
return !containsFile(dirPath);
|
return !containsFile(getPath(dirPath));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new FtpException(e);
|
throw new FtpException(e);
|
||||||
}
|
}
|
||||||
@ -186,8 +219,11 @@ public class SshjSftp extends AbstractFtp {
|
|||||||
@Override
|
@Override
|
||||||
public boolean upload(String destPath, File file) {
|
public boolean upload(String destPath, 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 (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new FtpException(e);
|
throw new FtpException(e);
|
||||||
}
|
}
|
||||||
@ -196,7 +232,7 @@ public class SshjSftp extends AbstractFtp {
|
|||||||
@Override
|
@Override
|
||||||
public void download(String path, File outFile) {
|
public void download(String path, File outFile) {
|
||||||
try {
|
try {
|
||||||
sftp.get(path, new FileSystemFile(outFile));
|
sftp.get(getPath(path), new FileSystemFile(outFile));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new FtpException(e);
|
throw new FtpException(e);
|
||||||
}
|
}
|
||||||
@ -204,9 +240,15 @@ public class SshjSftp extends AbstractFtp {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void recursiveDownloadFolder(String sourcePath, File destDir) {
|
public void recursiveDownloadFolder(String sourcePath, File destDir) {
|
||||||
List<String> files = ls(sourcePath);
|
if (!destDir.exists() || !destDir.isDirectory()) {
|
||||||
|
if (!destDir.mkdirs()) {
|
||||||
|
throw new FtpException("创建目录" + destDir.getAbsolutePath() + "失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> files = ls(getPath(sourcePath));
|
||||||
if (files != null && !files.isEmpty()) {
|
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) {
|
public boolean containsFile(String fileDir) {
|
||||||
try {
|
try {
|
||||||
sftp.lstat(fileDir);
|
sftp.lstat(getPath(fileDir));
|
||||||
return true;
|
return true;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -13,6 +13,8 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
public class FtpTest {
|
public class FtpTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -171,4 +173,14 @@ public class FtpTest {
|
|||||||
Console.log(ftp.pwd());
|
Console.log(ftp.pwd());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void renameTest() {
|
||||||
|
final Ftp ftp = new Ftp("localhost", 21, "test", "test");
|
||||||
|
|
||||||
|
ftp.mkdir("/ftp-1");
|
||||||
|
assertTrue(ftp.exist("/ftp-1"));
|
||||||
|
ftp.rename("/ftp-1", "/ftp-2");
|
||||||
|
assertTrue(ftp.exist("/ftp-2"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,8 @@ package cn.hutool.extra.ssh;
|
|||||||
|
|
||||||
import cn.hutool.core.util.CharsetUtil;
|
import cn.hutool.core.util.CharsetUtil;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.jupiter.api.Disabled;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.Disabled;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -16,52 +16,66 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class SftpTest {
|
public class SftpTest {
|
||||||
|
|
||||||
private SshjSftp sshjSftp;
|
private Sftp sftp;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
@Disabled
|
@Disabled
|
||||||
public void init() {
|
public void init() {
|
||||||
sshjSftp = new SshjSftp("ip", 22, "test", "test", CharsetUtil.CHARSET_UTF_8);
|
sftp = new Sftp("127.0.0.1", 8022, "test", "test", CharsetUtil.CHARSET_UTF_8);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Disabled
|
@Disabled
|
||||||
public void lsTest() {
|
public void lsTest() {
|
||||||
List<String> files = sshjSftp.ls("/");
|
List<String> files = sftp.ls("/");
|
||||||
if (files != null && !files.isEmpty()) {
|
if (files != null && !files.isEmpty()) {
|
||||||
files.forEach(System.out::print);
|
files.forEach(System.out::println);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Disabled
|
@Disabled
|
||||||
public void downloadTest() {
|
public void downloadTest() {
|
||||||
sshjSftp.recursiveDownloadFolder("/home/test/temp", new File("C:\\Users\\akwangl\\Downloads\\temp"));
|
sftp.recursiveDownloadFolder("/temp/20250427/", new File("D:\\temp\\20250430\\20250427\\"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Disabled
|
@Disabled
|
||||||
public void uploadTest() {
|
public void uploadTest() {
|
||||||
sshjSftp.upload("/home/test/temp/", new File("C:\\Users\\akwangl\\Downloads\\temp\\辽宁_20190718_104324.CIME"));
|
sftp.upload("/ftp-2/20250430/", new File("D:\\temp\\20250430\\test.txt"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Disabled
|
@Disabled
|
||||||
public void mkDirTest() {
|
public void mkDirTest() {
|
||||||
boolean flag = sshjSftp.mkdir("/home/test/temp");
|
boolean flag = sftp.mkdir("/ftp-2/20250430-1");
|
||||||
System.out.println("是否创建成功: " + flag);
|
System.out.println("是否创建成功: " + flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
public void pwdTest() {
|
||||||
|
String pwd = sftp.pwd();
|
||||||
|
System.out.println("PWD: " + pwd);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Disabled
|
@Disabled
|
||||||
public void mkDirsTest() {
|
public void mkDirsTest() {
|
||||||
// 在当前目录下批量创建目录
|
// 在当前目录下批量创建目录
|
||||||
sshjSftp.mkDirs("/home/test/temp");
|
sftp.mkDirs("/ftp-2/20250430-2/t1/t2/");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Disabled
|
@Disabled
|
||||||
public void delDirTest() {
|
public void delDirTest() {
|
||||||
sshjSftp.delDir("/home/test/temp");
|
sftp.delDir("/ftp-2/20250430-2/t1/t2");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
public void cdTest() {
|
||||||
|
System.out.println(sftp.cd("/ftp-2"));
|
||||||
|
System.out.println(sftp.cd("/ftp-4"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,86 @@
|
|||||||
|
package cn.hutool.extra.ssh;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.CharsetUtil;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Disabled;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 基于sshj 框架SFTP 封装测试.
|
||||||
|
*
|
||||||
|
* @author youyongkun
|
||||||
|
* @since 5.7.18
|
||||||
|
*/
|
||||||
|
class SshjSftpTest {
|
||||||
|
|
||||||
|
private static SshjSftp sshjSftp;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
public static void init() {
|
||||||
|
sshjSftp = new SshjSftp("localhost", 8022, "test", "test", CharsetUtil.CHARSET_UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
public void lsTest() {
|
||||||
|
List<String> files = sshjSftp.ls("/");
|
||||||
|
if (files != null && !files.isEmpty()) {
|
||||||
|
files.forEach(System.out::println);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
public void downloadTest() {
|
||||||
|
sshjSftp.recursiveDownloadFolder("/home/test/temp", new File("C:\\Users\\akwangl\\Downloads\\temp"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
public void uploadTest() {
|
||||||
|
sshjSftp.upload("/home/test/temp/", new File("C:\\Users\\akwangl\\Downloads\\temp\\辽宁_20190718_104324.CIME"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
public void mkDirTest() {
|
||||||
|
boolean flag = sshjSftp.mkdir("/home/test/temp");
|
||||||
|
System.out.println("是否创建成功: " + flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
public void mkDirsTest() {
|
||||||
|
// 在当前目录下批量创建目录
|
||||||
|
sshjSftp.mkDirs("/home/test/temp");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
public void delDirTest() {
|
||||||
|
sshjSftp.delDir("/home/test/temp");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void pwdTest() {
|
||||||
|
// mkDirsTest();
|
||||||
|
sshjSftp.cd("/ftp");
|
||||||
|
String pwd = sshjSftp.pwd();
|
||||||
|
System.out.println("当前目录: " + pwd);
|
||||||
|
assertEquals("/ftp", pwd);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void renameTest() {
|
||||||
|
// sshjSftp.mkdir("/ftp-1");
|
||||||
|
assertTrue(sshjSftp.exist("/ftp-1"));
|
||||||
|
sshjSftp.rename("/ftp-1", "/ftp-2");
|
||||||
|
assertTrue(sshjSftp.exist("/ftp-2"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user