From bef38c365b7adcf3ef328be0864468413e682729 Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 26 Sep 2022 22:18:45 +0800 Subject: [PATCH] =?UTF-8?q?hutool-extra=20ftp=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E6=88=96=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/collection/CollUtil.java | 8 +-- .../hutool/core/collection/CollUtilTest.java | 2 + .../main/java/cn/hutool/extra/ftp/Ftp.java | 72 ++++++++----------- .../java/cn/hutool/extra/ftp/FtpTest.java | 37 +++++----- 4 files changed, 55 insertions(+), 64 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java index ba1b99b23..cd8dddc7b 100755 --- a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java @@ -208,7 +208,7 @@ public class CollUtil { @SafeVarargs public static List unionAll(Collection coll1, Collection coll2, Collection... 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 otherColl : otherColls) { + for (final Collection otherColl : otherColls) { totalSize += size(otherColl); } } // 根据size创建,防止多次扩容 - List res = new ArrayList<>(totalSize); + final List res = new ArrayList<>(totalSize); if (coll1 != null) { res.addAll(coll1); } @@ -233,7 +233,7 @@ public class CollUtil { return res; } - for (Collection otherColl : otherColls) { + for (final Collection otherColl : otherColls) { if (otherColl != null) { res.addAll(otherColl); } diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java index 6ff5c4339..b9b9d4775 100755 --- a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java @@ -941,6 +941,7 @@ public class CollUtilTest { final List list = CollUtil.unionAll(list1, list2, list3); Assert.assertNotNull(list); + @SuppressWarnings("ConfusingArgumentToVarargsMethod") final List resList2 = CollUtil.unionAll(null, null, null); Assert.assertNotNull(resList2); } @@ -972,6 +973,7 @@ public class CollUtilTest { public void unionAllOtherIsNullTest() { final List list1 = CollectionUtil.newArrayList(1, 2, 2, 3, 3); final List list2 = CollectionUtil.newArrayList(1, 2, 3); + @SuppressWarnings("ConfusingArgumentToVarargsMethod") final List list = CollUtil.unionAll(list1, list2, null); Assert.assertNotNull(list); Assert.assertArrayEquals( diff --git a/hutool-extra/src/main/java/cn/hutool/extra/ftp/Ftp.java b/hutool-extra/src/main/java/cn/hutool/extra/ftp/Ftp.java index c7ef76170..06ecf7681 100755 --- a/hutool-extra/src/main/java/cn/hutool/extra/ftp/Ftp.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/ftp/Ftp.java @@ -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客户端封装
@@ -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 { } /** - * 上传文件或目录(包含当前及子孙目录的所有文件) + * 递归上传文件(支持目录)
+ * 上传时,如果uploadFile为目录,只复制目录下所有目录和文件到目标路径下,并不会复制目录本身
+ * 上传时,自动创建父级目录 * - * @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 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); - } - } - } } diff --git a/hutool-extra/src/test/java/cn/hutool/extra/ftp/FtpTest.java b/hutool-extra/src/test/java/cn/hutool/extra/ftp/FtpTest.java index 70f20b801..e581c8fd3 100644 --- a/hutool-extra/src/test/java/cn/hutool/extra/ftp/FtpTest.java +++ b/hutool-extra/src/test/java/cn/hutool/extra/ftp/FtpTest.java @@ -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 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 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(""));