hutool-extra ftp 支持上传文件或目录

This commit is contained in:
Looly 2022-09-26 22:18:45 +08:00
parent d30e8ab55b
commit bef38c365b
4 changed files with 55 additions and 64 deletions

View File

@ -208,7 +208,7 @@ public class CollUtil {
@SafeVarargs
public static <T> List<T> unionAll(Collection<T> coll1, Collection<T> coll2, Collection<T>... otherColls) {
if (CollUtil.isEmpty(coll1) && CollUtil.isEmpty(coll2) && ArrayUtil.isEmpty(otherColls)) {
return Collections.emptyList();
return new ArrayList<>(0);
}
// 计算元素总数
@ -216,13 +216,13 @@ public class CollUtil {
totalSize += size(coll1);
totalSize += size(coll2);
if (otherColls != null) {
for (Collection<T> otherColl : otherColls) {
for (final Collection<T> otherColl : otherColls) {
totalSize += size(otherColl);
}
}
// 根据size创建防止多次扩容
List<T> res = new ArrayList<>(totalSize);
final List<T> res = new ArrayList<>(totalSize);
if (coll1 != null) {
res.addAll(coll1);
}
@ -233,7 +233,7 @@ public class CollUtil {
return res;
}
for (Collection<T> otherColl : otherColls) {
for (final Collection<T> otherColl : otherColls) {
if (otherColl != null) {
res.addAll(otherColl);
}

View File

@ -941,6 +941,7 @@ public class CollUtilTest {
final List<String> list = CollUtil.unionAll(list1, list2, list3);
Assert.assertNotNull(list);
@SuppressWarnings("ConfusingArgumentToVarargsMethod")
final List<String> resList2 = CollUtil.unionAll(null, null, null);
Assert.assertNotNull(resList2);
}
@ -972,6 +973,7 @@ public class CollUtilTest {
public void unionAllOtherIsNullTest() {
final List<Integer> list1 = CollectionUtil.newArrayList(1, 2, 2, 3, 3);
final List<Integer> list2 = CollectionUtil.newArrayList(1, 2, 3);
@SuppressWarnings("ConfusingArgumentToVarargsMethod")
final List<Integer> list = CollUtil.unionAll(list1, list2, null);
Assert.assertNotNull(list);
Assert.assertArrayEquals(

View File

@ -21,7 +21,6 @@ import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* FTP客户端封装<br>
@ -522,9 +521,9 @@ public class Ftp extends AbstractFtp {
* @throws IORuntimeException IO异常
*/
public boolean upload(String destPath, String fileName, File file) throws IORuntimeException {
try (InputStream in = FileUtil.getInputStream(file)) {
try (final InputStream in = FileUtil.getInputStream(file)) {
return upload(destPath, fileName, in);
} catch (IOException e) {
} catch (final IOException e) {
throw new IORuntimeException(e);
}
}
@ -547,7 +546,7 @@ public class Ftp extends AbstractFtp {
public boolean upload(String destPath, String fileName, InputStream fileStream) throws IORuntimeException {
try {
client.setFileType(FTPClient.BINARY_FILE_TYPE);
} catch (IOException e) {
} catch (final IOException e) {
throw new IORuntimeException(e);
}
@ -575,19 +574,38 @@ public class Ftp extends AbstractFtp {
}
/**
* 上传文件或目录包含当前及子孙目录的所有文件
* 递归上传文件支持目录<br>
* 上传时如果uploadFile为目录只复制目录下所有目录和文件到目标路径下并不会复制目录本身<br>
* 上传时自动创建父级目录
*
* @param destPath 目标路径
* @param remotePath 目录路径
* @param uploadFile 上传文件或目录
*/
public void uploadFileOrDirectory(String destPath, final File uploadFile) {
if (uploadFile.isFile()) {
this.upload(destPath, uploadFile);
public void uploadFileOrDirectory(final String remotePath, final File uploadFile) {
if (false == FileUtil.isDirectory(uploadFile)) {
this.upload(remotePath, uploadFile);
return;
}
this.mkDirs(destPath);
recursiveUpload(destPath, uploadFile);
final File[] files = uploadFile.listFiles();
if (ArrayUtil.isEmpty(files)) {
return;
}
final List<File> dirs = new ArrayList<>(files.length);
//第一次只处理文件防止目录在前面导致先处理子目录而引发文件所在目录不正确
for (final File f : files) {
if (f.isDirectory()) {
dirs.add(f);
} else {
this.upload(remotePath, f);
}
}
//第二次只处理目录
for (final File f : dirs) {
final String dir = FileUtil.normalize(remotePath + "/" + f.getName());
upload(dir, f);
}
}
/**
@ -720,36 +738,4 @@ public class Ftp extends AbstractFtp {
this.client = null;
}
}
/**
* 递归上传文件支持目录
*
* @param destPath 目录路径
* @param uploadFile 上传文件或目录
*/
private void recursiveUpload(String destPath, final File uploadFile) {
if (uploadFile.isFile()) {
this.upload(destPath, uploadFile);
return;
}
final File[] files = uploadFile.listFiles();
if (Objects.isNull(files)) {
return;
}
//第一次只处理文件防止目录在前面导致先处理子孙目录而引发文件所在目录不正确
for (final File f : files) {
if (f.isFile()) {
this.upload(destPath, f);
}
}
//第二次只处理目录
for (final File f : files) {
if (f.isDirectory()) {
destPath = destPath + "/" + f.getName();
this.mkDirs(destPath);
recursiveUpload(destPath, f);
}
}
}
}

View File

@ -7,6 +7,7 @@ import cn.hutool.extra.ssh.Sftp;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
public class FtpTest {
@ -14,7 +15,7 @@ public class FtpTest {
@Test
@Ignore
public void cdTest() {
Ftp ftp = new Ftp("looly.centos");
final Ftp ftp = new Ftp("looly.centos");
ftp.cd("/file/aaa");
Console.log(ftp.pwd());
@ -25,9 +26,9 @@ public class FtpTest {
@Test
@Ignore
public void uploadTest() {
Ftp ftp = new Ftp("localhost");
final Ftp ftp = new Ftp("localhost");
boolean upload = ftp.upload("/temp", FileUtil.file("d:/test/test.zip"));
final boolean upload = ftp.upload("/temp", FileUtil.file("d:/test/test.zip"));
Console.log(upload);
IoUtil.close(ftp);
@ -45,7 +46,7 @@ public class FtpTest {
@Test
@Ignore
public void reconnectIfTimeoutTest() throws InterruptedException {
Ftp ftp = new Ftp("looly.centos");
final Ftp ftp = new Ftp("looly.centos");
Console.log("打印pwd: " + ftp.pwd());
@ -54,7 +55,7 @@ public class FtpTest {
try{
Console.log("打印pwd: " + ftp.pwd());
}catch (FtpException e) {
}catch (final FtpException e) {
e.printStackTrace();
}
@ -69,7 +70,7 @@ public class FtpTest {
@Test
@Ignore
public void recursiveDownloadFolder() {
Ftp ftp = new Ftp("looly.centos");
final Ftp ftp = new Ftp("looly.centos");
ftp.recursiveDownloadFolder("/",FileUtil.file("d:/test/download"));
IoUtil.close(ftp);
@ -78,7 +79,7 @@ public class FtpTest {
@Test
@Ignore
public void recursiveDownloadFolderSftp() {
Sftp ftp = new Sftp("127.0.0.1", 22, "test", "test");
final Sftp ftp = new Sftp("127.0.0.1", 22, "test", "test");
ftp.cd("/file/aaa");
Console.log(ftp.pwd());
@ -90,20 +91,22 @@ public class FtpTest {
@Test
@Ignore
public void downloadTest() {
Ftp ftp = new Ftp("localhost");
List<String> fileNames = ftp.ls("temp/");
for(String name: fileNames) {
ftp.download("",
name,
FileUtil.file("d:/test/download/" + name));
try(final Ftp ftp = new Ftp("localhost")){
final List<String> fileNames = ftp.ls("temp/");
for(final String name: fileNames) {
ftp.download("",
name,
FileUtil.file("d:/test/download/" + name));
}
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
@Test
@Ignore
public void isDirTest() throws Exception {
try (Ftp ftp = new Ftp("127.0.0.1", 21)) {
try (final Ftp ftp = new Ftp("127.0.0.1", 21)) {
Console.log(ftp.pwd());
ftp.isDir("/test");
Console.log(ftp.pwd());
@ -113,7 +116,7 @@ public class FtpTest {
@Test
@Ignore
public void existSftpTest() throws Exception {
try (Sftp ftp = new Sftp("127.0.0.1", 22, "test", "test")) {
try (final Sftp ftp = new Sftp("127.0.0.1", 22, "test", "test")) {
Console.log(ftp.pwd());
Console.log(ftp.exist(null));
Console.log(ftp.exist(""));
@ -136,7 +139,7 @@ public class FtpTest {
@Test
@Ignore
public void existFtpTest() throws Exception {
try (Ftp ftp = new Ftp("127.0.0.1", 21)) {
try (final Ftp ftp = new Ftp("127.0.0.1", 21)) {
Console.log(ftp.pwd());
Console.log(ftp.exist(null));
Console.log(ftp.exist(""));