From 0c730e249cef0415da56838196d3b8e0bb36986f Mon Sep 17 00:00:00 2001 From: bwcx_jzy Date: Mon, 19 Jul 2021 17:27:51 +0800 Subject: [PATCH 1/4] =?UTF-8?q?sftp=20=E6=96=B0=E5=A2=9E=E5=B0=86=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E6=96=87=E4=BB=B6=E6=88=96=E8=80=85=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=B9=E5=90=8C=E6=AD=A5=E4=B8=8A=E4=BC=A0=E5=88=B0=E8=BF=9C?= =?UTF-8?q?=E7=A8=8B=E7=9B=AE=E5=BD=95=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hutool/extra/ssh/Sftp.java | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java b/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java index 239c7caa9..a6812d8bf 100644 --- a/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java @@ -17,6 +17,7 @@ import com.jcraft.jsch.SftpException; import com.jcraft.jsch.SftpProgressMonitor; import java.io.File; +import java.io.FileInputStream; import java.io.OutputStream; import java.nio.charset.Charset; import java.util.ArrayList; @@ -319,7 +320,7 @@ public class Sftp extends AbstractFtp { @Override public boolean mkdir(String dir) { - if(isDir(dir)){ + if (isDir(dir)) { // 目录已经存在,创建直接返回 return true; } @@ -332,7 +333,7 @@ public class Sftp extends AbstractFtp { } @Override - public boolean isDir(String dir){ + public boolean isDir(String dir) { final SftpATTRS sftpATTRS; try { sftpATTRS = this.channel.stat(dir); @@ -423,6 +424,35 @@ public class Sftp extends AbstractFtp { } } + /** + * 将本地文件或者文件夹同步(覆盖)上传到远程路径 + * + * @param file 文件或者文件夹 + * @param remotePath 远程路径 + */ + public void syncPath(File file, String remotePath) { + if (!FileUtil.exist(file)) { + return; + } + if (file.isDirectory()) { + File[] files = file.listFiles(); + if (files == null) { + return; + } + for (File fileItem : files) { + if (fileItem.isDirectory()) { + String mkdir = FileUtil.normalize(remotePath + "/" + fileItem.getName()); + this.syncPath(fileItem, mkdir); + } else { + this.syncPath(fileItem, remotePath); + } + } + } else { + this.mkDirs(remotePath); + this.upload(remotePath, file); + } + } + @Override public boolean upload(String destPath, File file) { put(FileUtil.getAbsolutePath(file), destPath); From 3f84506b23e7a94ad87dd77df4812554111360c5 Mon Sep 17 00:00:00 2001 From: bwcx_jzy Date: Mon, 19 Jul 2021 17:31:11 +0800 Subject: [PATCH 2/4] =?UTF-8?q?sftp=20=E6=96=B0=E5=A2=9E=E5=B0=86=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E6=96=87=E4=BB=B6=E6=88=96=E8=80=85=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=B9=E5=90=8C=E6=AD=A5=E4=B8=8A=E4=BC=A0=E5=88=B0=E8=BF=9C?= =?UTF-8?q?=E7=A8=8B=E7=9B=AE=E5=BD=95=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java b/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java index a6812d8bf..9ac37de75 100644 --- a/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java @@ -430,7 +430,7 @@ public class Sftp extends AbstractFtp { * @param file 文件或者文件夹 * @param remotePath 远程路径 */ - public void syncPath(File file, String remotePath) { + public void syncUpload(File file, String remotePath) { if (!FileUtil.exist(file)) { return; } @@ -442,9 +442,9 @@ public class Sftp extends AbstractFtp { for (File fileItem : files) { if (fileItem.isDirectory()) { String mkdir = FileUtil.normalize(remotePath + "/" + fileItem.getName()); - this.syncPath(fileItem, mkdir); + this.syncUpload(fileItem, mkdir); } else { - this.syncPath(fileItem, remotePath); + this.syncUpload(fileItem, remotePath); } } } else { From 175f7fe9f45a428d1210901b60962e47edf01c40 Mon Sep 17 00:00:00 2001 From: bwcx_jzy Date: Mon, 19 Jul 2021 17:33:59 +0800 Subject: [PATCH 3/4] =?UTF-8?q?cn.hutool.core.io.FileUtil.isModifed=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=90=8D=E6=8B=BC=E5=86=99=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/cn/hutool/core/io/FileUtil.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java index f507a94a7..1e76e6945 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java @@ -1370,7 +1370,21 @@ public class FileUtil extends PathUtil { * @param lastModifyTime 上次的改动时间 * @return 是否被改动 */ + @Deprecated public static boolean isModifed(File file, long lastModifyTime) { + return isModified(file,lastModifyTime); + } + + + /** + * 判断文件是否被改动
+ * 如果文件对象为 null 或者文件不存在,被视为改动 + * + * @param file 文件对象 + * @param lastModifyTime 上次的改动时间 + * @return 是否被改动 + */ + public static boolean isModified(File file, long lastModifyTime) { if (null == file || false == file.exists()) { return true; } From 77e81d3f2b975f9af659395bc18527a0f2d32d2e Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 21 Jul 2021 21:19:26 +0800 Subject: [PATCH 4/4] add method --- CHANGELOG.md | 3 ++- hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java | 1 + hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1ace38d3..43359b695 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,12 +3,13 @@ ------------------------------------------------------------------------------------------------------------- -# 5.7.6 (2021-07-19) +# 5.7.6 (2021-07-21) ### 🐣新特性 * 【core 】 增加FieldsComparator(pr#374@Gitee) * 【core 】 FileUtil.del采用Files.delete实现 * 【core 】 改进Base64.isBase64方法增加等号判断(issue#1710@Github) +* 【core 】 Sftp增加syncUpload方法(pr#375@Gitee) ### 🐞Bug修复 diff --git a/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java index 56cd780e7..3dd71a32c 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java @@ -1382,6 +1382,7 @@ public class FileUtil extends PathUtil { * @param file 文件对象 * @param lastModifyTime 上次的改动时间 * @return 是否被改动 + * @deprecated 拼写错误,请使用{@link #isModified(File, long)} */ @Deprecated public static boolean isModifed(File file, long lastModifyTime) { diff --git a/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java b/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java index 9ac37de75..2b630ab17 100644 --- a/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java @@ -17,7 +17,6 @@ import com.jcraft.jsch.SftpException; import com.jcraft.jsch.SftpProgressMonitor; import java.io.File; -import java.io.FileInputStream; import java.io.OutputStream; import java.nio.charset.Charset; import java.util.ArrayList; @@ -429,9 +428,10 @@ public class Sftp extends AbstractFtp { * * @param file 文件或者文件夹 * @param remotePath 远程路径 + * @since 5.7.6 */ public void syncUpload(File file, String remotePath) { - if (!FileUtil.exist(file)) { + if (false == FileUtil.exist(file)) { return; } if (file.isDirectory()) {