mirror of
https://gitee.com/dromara/hutool.git
synced 2025-06-28 13:34:09 +08:00
Merge remote-tracking branch 'origin/feature/ftp-rename' into v5-dev
# Conflicts: # hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java # hutool-extra/src/test/java/cn/hutool/extra/ssh/SftpTest.java
This commit is contained in:
commit
e458bca2a0
@ -737,6 +737,25 @@ public class Ftp extends AbstractFtp {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重命名文件/目录
|
||||
*
|
||||
* @param from 原路径
|
||||
* @param to 目标路径
|
||||
*
|
||||
* @throws FtpException FTP异常
|
||||
*/
|
||||
@Override
|
||||
public void rename(String from, String to) throws FtpException {
|
||||
try {
|
||||
if (!client.rename(from, to)) {
|
||||
throw new FtpException("rename [{}] to [{}] fail", from, to);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new FtpException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取FTPClient客户端对象
|
||||
*
|
||||
@ -756,4 +775,5 @@ public class Ftp extends AbstractFtp {
|
||||
this.client = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -656,6 +656,23 @@ public class Sftp extends AbstractFtp {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重命名文件/目录
|
||||
*
|
||||
* @param from 原路径
|
||||
* @param to 目标路径
|
||||
*
|
||||
* @throws JschRuntimeException Jsch异常
|
||||
*/
|
||||
@Override
|
||||
public void rename(String from, String to) throws JschRuntimeException {
|
||||
try {
|
||||
getClient().rename(from, to);
|
||||
} catch (SftpException e) {
|
||||
throw new JschRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取远程文件
|
||||
*
|
||||
|
@ -90,7 +90,7 @@ public class SshjSftp extends AbstractFtp {
|
||||
* @param config FTP配置
|
||||
* @since 5.3.3
|
||||
*/
|
||||
protected SshjSftp(FtpConfig config) {
|
||||
public SshjSftp(FtpConfig config) {
|
||||
super(config);
|
||||
init();
|
||||
}
|
||||
@ -261,6 +261,23 @@ public class SshjSftp extends AbstractFtp {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重命名文件/目录
|
||||
*
|
||||
* @param from 原路径
|
||||
* @param to 目标路径
|
||||
*
|
||||
* @throws FtpException FTP异常
|
||||
*/
|
||||
@Override
|
||||
public void rename(String from, String to) throws FtpException {
|
||||
try {
|
||||
sftp.rename(from, to);
|
||||
} catch (IOException e) {
|
||||
throw new FtpException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
IoUtil.close(this.session);
|
||||
|
@ -13,6 +13,8 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class FtpTest {
|
||||
|
||||
@Test
|
||||
@ -171,4 +173,14 @@ public class FtpTest {
|
||||
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"));
|
||||
}
|
||||
}
|
||||
|
@ -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", 22, "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::print);
|
||||
}
|
||||
}
|
||||
|
||||
@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