From e8c1f7ad7b40f029b2dfafd92e3bf1426989881f Mon Sep 17 00:00:00 2001 From: Looly Date: Sat, 29 Jul 2023 12:15:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9A=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E4=B8=8BSftp=E4=B8=ADChannel=E5=85=B3=E9=97=AD=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../main/java/cn/hutool/extra/ssh/Sftp.java | 27 +++++++++++-------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68f3719705..31e482d9f3 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ * 【json 】 修复JSONBeanParser在遇到List时没有被正确递归问题(issue#I7M2GZ@Gitee) * 【core 】 修复VersionComparator对1.0.3及1.0.2a比较有误的问题(pr#1043@Gitee) * 【core 】 修复IOS系统下,chrome 浏览器的解析规则有误(pr#1044@Gitee) +* 【extra 】 修复多线程下Sftp中Channel关闭的问题(issue#I7OHIB@Gitee) ------------------------------------------------------------------------------------------------------------- # 5.8.20(2023-06-16) 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 d29b6c1bd1..bbca18dd0b 100755 --- a/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java @@ -233,6 +233,9 @@ public Sftp reconnectIfTimeout() { * @since 4.1.14 */ public ChannelSftp getClient() { + if(false == this.channel.isConnected()){ + init(); + } return this.channel; } @@ -244,7 +247,7 @@ public ChannelSftp getClient() { @Override public String pwd() { try { - return channel.pwd(); + return getClient().pwd(); } catch (SftpException e) { throw new JschRuntimeException(e); } @@ -258,7 +261,7 @@ public String pwd() { */ public String home() { try { - return channel.getHome(); + return getClient().getHome(); } catch (SftpException e) { throw new JschRuntimeException(e); } @@ -339,7 +342,7 @@ public List lsEntries(String path) { public List lsEntries(String path, Filter filter) { final List entryList = new ArrayList<>(); try { - channel.ls(path, entry -> { + getClient().ls(path, entry -> { final String fileName = entry.getFilename(); if (false == StrUtil.equals(".", fileName) && false == StrUtil.equals("..", fileName)) { if (null == filter || filter.accept(entry)) { @@ -364,7 +367,7 @@ public boolean mkdir(String dir) { return true; } try { - this.channel.mkdir(dir); + getClient().mkdir(dir); return true; } catch (SftpException e) { throw new JschRuntimeException(e); @@ -375,7 +378,7 @@ public boolean mkdir(String dir) { public boolean isDir(String dir) { final SftpATTRS sftpATTRS; try { - sftpATTRS = this.channel.stat(dir); + sftpATTRS = getClient().stat(dir); } catch (SftpException e) { final String msg = e.getMessage(); // issue#I4P9ED@Gitee @@ -403,7 +406,7 @@ synchronized public boolean cd(String directory) throws FtpException { return true; } try { - channel.cd(directory.replace('\\', '/')); + getClient().cd(directory.replace('\\', '/')); return true; } catch (SftpException e) { throw new FtpException(e); @@ -418,7 +421,7 @@ synchronized public boolean cd(String directory) throws FtpException { @Override public boolean delFile(String filePath) { try { - channel.rm(filePath); + getClient().rm(filePath); } catch (SftpException e) { throw new JschRuntimeException(e); } @@ -438,6 +441,8 @@ public boolean delDir(String dirPath) { return false; } + final ChannelSftp channel = getClient(); + Vector list; try { list = channel.ls(channel.pwd()); @@ -562,7 +567,7 @@ public Sftp put(String srcFilePath, String destPath, Mode mode) { */ public Sftp put(String srcFilePath, String destPath, SftpProgressMonitor monitor, Mode mode) { try { - channel.put(srcFilePath, destPath, monitor, mode.ordinal()); + getClient().put(srcFilePath, destPath, monitor, mode.ordinal()); } catch (SftpException e) { throw new JschRuntimeException(e); } @@ -581,7 +586,7 @@ public Sftp put(String srcFilePath, String destPath, SftpProgressMonitor monitor */ public Sftp put(InputStream srcStream, String destPath, SftpProgressMonitor monitor, Mode mode) { try { - channel.put(srcStream, destPath, monitor, mode.ordinal()); + getClient().put(srcStream, destPath, monitor, mode.ordinal()); } catch (SftpException e) { throw new JschRuntimeException(e); } @@ -644,7 +649,7 @@ public void recursiveDownloadFolder(String sourcePath, File destDir) throws Jsch */ public Sftp get(String src, String dest) { try { - channel.get(src, dest); + getClient().get(src, dest); } catch (SftpException e) { throw new JschRuntimeException(e); } @@ -661,7 +666,7 @@ public Sftp get(String src, String dest) { */ public Sftp get(String src, OutputStream out) { try { - channel.get(src, out); + getClient().get(src, out); } catch (SftpException e) { throw new JschRuntimeException(e); }