From 6d83db671bf3275d0d3bef1d5470f00cf6647849 Mon Sep 17 00:00:00 2001 From: xhzou Date: Mon, 26 Sep 2022 15:25:13 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20hutool-extra=20ftp=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E6=88=96=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=EF=BC=88=E5=8C=85=E5=90=AB=E5=BD=93=E5=89=8D=E5=8F=8A?= =?UTF-8?q?=E5=AD=90=E5=AD=99=E7=9B=AE=E5=BD=95=E7=9A=84=E6=89=80=E6=9C=89?= =?UTF-8?q?=E6=96=87=E4=BB=B6=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/extra/ftp/AbstractFtp.java | 4 +- .../main/java/cn/hutool/extra/ftp/Ftp.java | 69 ++++++++++++++++--- .../java/cn/hutool/extra/ftp/FtpTest.java | 9 +++ 3 files changed, 70 insertions(+), 12 deletions(-) diff --git a/hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java b/hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java index ffc5bd1b8..48b37d2b2 100644 --- a/hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java @@ -163,13 +163,13 @@ public abstract class AbstractFtp implements Closeable { if (StrUtil.isNotEmpty(s)) { boolean exist = true; try { - if (false == cd(s)) { + if (!cd(s)) { exist = false; } } catch (FtpException e) { exist = false; } - if (false == exist) { + if (!exist) { //目录不存在时创建 mkdir(s); cd(s); 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 0045b451a..98568b493 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,6 +21,7 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; +import java.util.Objects; /** * FTP客户端封装
@@ -219,7 +220,7 @@ public class Ftp extends AbstractFtp { throw new IORuntimeException(e); } final int replyCode = client.getReplyCode(); // 是否成功登录服务器 - if (false == FTPReply.isPositiveCompletion(replyCode)) { + if (!FTPReply.isPositiveCompletion(replyCode)) { try { client.disconnect(); } catch (IOException e) { @@ -354,7 +355,7 @@ public class Ftp extends AbstractFtp { String fileName; for (FTPFile ftpFile : ftpFiles) { fileName = ftpFile.getName(); - if (false == StrUtil.equals(".", fileName) && false == StrUtil.equals("..", fileName)) { + if (!StrUtil.equals(".", fileName) && !StrUtil.equals("..", fileName)) { if (null == filter || filter.accept(ftpFile)) { result.add(ftpFile); } @@ -375,7 +376,7 @@ public class Ftp extends AbstractFtp { String pwd = null; if (StrUtil.isNotBlank(path)) { pwd = pwd(); - if (false == cd(path)) { + if (!cd(path)) { throw new FtpException("Change dir to [{}] error, maybe path not exist!", path); } } @@ -439,7 +440,7 @@ public class Ftp extends AbstractFtp { final String pwd = pwd(); final String fileName = FileUtil.getName(path); final String dir = StrUtil.removeSuffix(path, fileName); - if (false == cd(dir)) { + if (!cd(dir)) { throw new FtpException("Change dir to [{}] error, maybe dir not exist!", path); } @@ -470,7 +471,7 @@ public class Ftp extends AbstractFtp { childPath = StrUtil.format("{}/{}", dirPath, name); if (ftpFile.isDirectory()) { // 上级和本级目录除外 - if (false == ".".equals(name) && false == "..".equals(name)) { + if (!".".equals(name) && !"..".equals(name)) { delDir(childPath); } } else { @@ -557,7 +558,7 @@ public class Ftp extends AbstractFtp { if (StrUtil.isNotBlank(destPath)) { mkDirs(destPath); - if (false == cd(destPath)) { + if (!cd(destPath)) { throw new FtpException("Change dir to [{}] error, maybe dir not exist!", destPath); } } @@ -573,6 +574,54 @@ public class Ftp extends AbstractFtp { } } + /** + * 上传文件或目录(包含当前及子孙目录的所有文件) + * + * @param destPath 目标路径 + * @param uploadFile 上传文件或目录 + */ + public void uploadFileOrDirectory(String destPath, final File uploadFile) { + if (uploadFile.isFile()) { + this.upload(destPath, uploadFile); + return; + } + + this.mkDirs(destPath); + recursiveUpload(destPath, uploadFile); + } + + /** + * 递归上传文件(支持目录) + * + * @param destPath 目录路径 + * @param uploadFile 上传文件或目录 + */ + public void recursiveUpload(String destPath, final File uploadFile) { + if (uploadFile.isFile()) { + this.upload(destPath, uploadFile); + return; + } + File[] files = uploadFile.listFiles(); + if (Objects.isNull(files)) { + return; + } + + //第一次只处理文件,防止目录在前面导致先处理子孙目录,而引发文件所在目录不正确 + for (File f : files) { + if (f.isFile()) { + this.upload(destPath, f); + } + } + //第二次只处理目录 + for (File f : files) { + if (f.isDirectory()) { + destPath = destPath + File.separator + f.getName(); + this.mkDirs(destPath); + recursiveUpload(destPath, f); + } + } + } + /** * 下载文件 * @@ -602,9 +651,9 @@ public class Ftp extends AbstractFtp { srcFile = StrUtil.format("{}/{}", sourcePath, fileName); destFile = FileUtil.file(destDir, fileName); - if (false == ftpFile.isDirectory()) { + if (!ftpFile.isDirectory()) { // 本地不存在文件或者ftp上文件有修改则下载 - if (false == FileUtil.exist(destFile) + if (!FileUtil.exist(destFile) || (ftpFile.getTimestamp().getTimeInMillis() > destFile.lastModified())) { download(srcFile, destFile); } @@ -628,7 +677,7 @@ public class Ftp extends AbstractFtp { if (outFile.isDirectory()) { outFile = new File(outFile, fileName); } - if (false == outFile.exists()) { + if (!outFile.exists()) { FileUtil.touch(outFile); } try (OutputStream out = FileUtil.getOutputStream(outFile)) { @@ -665,7 +714,7 @@ public class Ftp extends AbstractFtp { pwd = pwd(); } - if (false == cd(path)) { + if (!cd(path)) { throw new FtpException("Change dir to [{}] error, maybe dir not exist!", path); } 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 6dc04d292..70f20b801 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 @@ -33,6 +33,15 @@ public class FtpTest { IoUtil.close(ftp); } + @Test + @Ignore + public void uploadDirectorTest() { + final Ftp ftp = new Ftp("localhost"); + + ftp.uploadFileOrDirectory("/temp", FileUtil.file("d:/test/")); + IoUtil.close(ftp); + } + @Test @Ignore public void reconnectIfTimeoutTest() throws InterruptedException { From 775caa9911f9a6ef87d830ead143211c849ddff2 Mon Sep 17 00:00:00 2001 From: xhzou Date: Mon, 26 Sep 2022 15:25:36 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20hutool-extra=20ftp=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E6=88=96=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=EF=BC=88=E5=8C=85=E5=90=AB=E5=BD=93=E5=89=8D=E5=8F=8A?= =?UTF-8?q?=E5=AD=90=E5=AD=99=E7=9B=AE=E5=BD=95=E7=9A=84=E6=89=80=E6=9C=89?= =?UTF-8?q?=E6=96=87=E4=BB=B6=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hutool-extra/src/main/java/cn/hutool/extra/ftp/Ftp.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 98568b493..d71130de7 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 @@ -31,7 +31,7 @@ import java.util.Objects; * 1、filezila server ;根目录一般都是空 * 2、linux vsftpd ; 使用的 系统用户的目录,这里往往都是不是根目录,如:/home/ftpuser/ftp * - * @author looly + * @author looly, xhzou * @since 4.1.8 */ public class Ftp extends AbstractFtp { From 8bbe6bbf713b50243dba38ca2f31e1227b7d67b4 Mon Sep 17 00:00:00 2001 From: xhzou Date: Mon, 26 Sep 2022 16:30:19 +0800 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20hutool-extra=20ftp=20=E4=BF=9D?= =?UTF-8?q?=E6=8C=81=20false=20=3D=3D=20variable=20=E7=9A=84=E5=86=99?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/extra/ftp/AbstractFtp.java | 4 ++-- .../main/java/cn/hutool/extra/ftp/Ftp.java | 20 +++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java b/hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java index 48b37d2b2..ffc5bd1b8 100644 --- a/hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java @@ -163,13 +163,13 @@ public abstract class AbstractFtp implements Closeable { if (StrUtil.isNotEmpty(s)) { boolean exist = true; try { - if (!cd(s)) { + if (false == cd(s)) { exist = false; } } catch (FtpException e) { exist = false; } - if (!exist) { + if (false == exist) { //目录不存在时创建 mkdir(s); cd(s); 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 d71130de7..ecee84b4c 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 @@ -220,7 +220,7 @@ public class Ftp extends AbstractFtp { throw new IORuntimeException(e); } final int replyCode = client.getReplyCode(); // 是否成功登录服务器 - if (!FTPReply.isPositiveCompletion(replyCode)) { + if (false == FTPReply.isPositiveCompletion(replyCode)) { try { client.disconnect(); } catch (IOException e) { @@ -355,7 +355,7 @@ public class Ftp extends AbstractFtp { String fileName; for (FTPFile ftpFile : ftpFiles) { fileName = ftpFile.getName(); - if (!StrUtil.equals(".", fileName) && !StrUtil.equals("..", fileName)) { + if (false == StrUtil.equals(".", fileName) && false == StrUtil.equals("..", fileName)) { if (null == filter || filter.accept(ftpFile)) { result.add(ftpFile); } @@ -376,7 +376,7 @@ public class Ftp extends AbstractFtp { String pwd = null; if (StrUtil.isNotBlank(path)) { pwd = pwd(); - if (!cd(path)) { + if (false == cd(path)) { throw new FtpException("Change dir to [{}] error, maybe path not exist!", path); } } @@ -440,7 +440,7 @@ public class Ftp extends AbstractFtp { final String pwd = pwd(); final String fileName = FileUtil.getName(path); final String dir = StrUtil.removeSuffix(path, fileName); - if (!cd(dir)) { + if (false == cd(dir)) { throw new FtpException("Change dir to [{}] error, maybe dir not exist!", path); } @@ -471,7 +471,7 @@ public class Ftp extends AbstractFtp { childPath = StrUtil.format("{}/{}", dirPath, name); if (ftpFile.isDirectory()) { // 上级和本级目录除外 - if (!".".equals(name) && !"..".equals(name)) { + if (false == ".".equals(name) && false == "..".equals(name)) { delDir(childPath); } } else { @@ -558,7 +558,7 @@ public class Ftp extends AbstractFtp { if (StrUtil.isNotBlank(destPath)) { mkDirs(destPath); - if (!cd(destPath)) { + if (false == cd(destPath)) { throw new FtpException("Change dir to [{}] error, maybe dir not exist!", destPath); } } @@ -651,9 +651,9 @@ public class Ftp extends AbstractFtp { srcFile = StrUtil.format("{}/{}", sourcePath, fileName); destFile = FileUtil.file(destDir, fileName); - if (!ftpFile.isDirectory()) { + if (false == ftpFile.isDirectory()) { // 本地不存在文件或者ftp上文件有修改则下载 - if (!FileUtil.exist(destFile) + if (false == FileUtil.exist(destFile) || (ftpFile.getTimestamp().getTimeInMillis() > destFile.lastModified())) { download(srcFile, destFile); } @@ -677,7 +677,7 @@ public class Ftp extends AbstractFtp { if (outFile.isDirectory()) { outFile = new File(outFile, fileName); } - if (!outFile.exists()) { + if (false == outFile.exists()) { FileUtil.touch(outFile); } try (OutputStream out = FileUtil.getOutputStream(outFile)) { @@ -714,7 +714,7 @@ public class Ftp extends AbstractFtp { pwd = pwd(); } - if (!cd(path)) { + if (false == cd(path)) { throw new FtpException("Change dir to [{}] error, maybe dir not exist!", path); }