Skip to content

Commit e8c1f7a

Browse files
committed
修复多线程下Sftp中Channel关闭的问题
1 parent 68daef7 commit e8c1f7a

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* 【json 】 修复JSONBeanParser在遇到List时没有被正确递归问题(issue#I7M2GZ@Gitee)
3434
* 【core 】 修复VersionComparator对1.0.3及1.0.2a比较有误的问题(pr#1043@Gitee)
3535
* 【core 】 修复IOS系统下,chrome 浏览器的解析规则有误(pr#1044@Gitee)
36+
* 【extra 】 修复多线程下Sftp中Channel关闭的问题(issue#I7OHIB@Gitee)
3637

3738
-------------------------------------------------------------------------------------------------------------
3839
# 5.8.20(2023-06-16)

hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ public Sftp reconnectIfTimeout() {
233233
* @since 4.1.14
234234
*/
235235
public ChannelSftp getClient() {
236+
if(false == this.channel.isConnected()){
237+
init();
238+
}
236239
return this.channel;
237240
}
238241

@@ -244,7 +247,7 @@ public ChannelSftp getClient() {
244247
@Override
245248
public String pwd() {
246249
try {
247-
return channel.pwd();
250+
return getClient().pwd();
248251
} catch (SftpException e) {
249252
throw new JschRuntimeException(e);
250253
}
@@ -258,7 +261,7 @@ public String pwd() {
258261
*/
259262
public String home() {
260263
try {
261-
return channel.getHome();
264+
return getClient().getHome();
262265
} catch (SftpException e) {
263266
throw new JschRuntimeException(e);
264267
}
@@ -339,7 +342,7 @@ public List<LsEntry> lsEntries(String path) {
339342
public List<LsEntry> lsEntries(String path, Filter<LsEntry> filter) {
340343
final List<LsEntry> entryList = new ArrayList<>();
341344
try {
342-
channel.ls(path, entry -> {
345+
getClient().ls(path, entry -> {
343346
final String fileName = entry.getFilename();
344347
if (false == StrUtil.equals(".", fileName) && false == StrUtil.equals("..", fileName)) {
345348
if (null == filter || filter.accept(entry)) {
@@ -364,7 +367,7 @@ public boolean mkdir(String dir) {
364367
return true;
365368
}
366369
try {
367-
this.channel.mkdir(dir);
370+
getClient().mkdir(dir);
368371
return true;
369372
} catch (SftpException e) {
370373
throw new JschRuntimeException(e);
@@ -375,7 +378,7 @@ public boolean mkdir(String dir) {
375378
public boolean isDir(String dir) {
376379
final SftpATTRS sftpATTRS;
377380
try {
378-
sftpATTRS = this.channel.stat(dir);
381+
sftpATTRS = getClient().stat(dir);
379382
} catch (SftpException e) {
380383
final String msg = e.getMessage();
381384
// issue#I4P9ED@Gitee
@@ -403,7 +406,7 @@ synchronized public boolean cd(String directory) throws FtpException {
403406
return true;
404407
}
405408
try {
406-
channel.cd(directory.replace('\\', '/'));
409+
getClient().cd(directory.replace('\\', '/'));
407410
return true;
408411
} catch (SftpException e) {
409412
throw new FtpException(e);
@@ -418,7 +421,7 @@ synchronized public boolean cd(String directory) throws FtpException {
418421
@Override
419422
public boolean delFile(String filePath) {
420423
try {
421-
channel.rm(filePath);
424+
getClient().rm(filePath);
422425
} catch (SftpException e) {
423426
throw new JschRuntimeException(e);
424427
}
@@ -438,6 +441,8 @@ public boolean delDir(String dirPath) {
438441
return false;
439442
}
440443

444+
final ChannelSftp channel = getClient();
445+
441446
Vector<LsEntry> list;
442447
try {
443448
list = channel.ls(channel.pwd());
@@ -562,7 +567,7 @@ public Sftp put(String srcFilePath, String destPath, Mode mode) {
562567
*/
563568
public Sftp put(String srcFilePath, String destPath, SftpProgressMonitor monitor, Mode mode) {
564569
try {
565-
channel.put(srcFilePath, destPath, monitor, mode.ordinal());
570+
getClient().put(srcFilePath, destPath, monitor, mode.ordinal());
566571
} catch (SftpException e) {
567572
throw new JschRuntimeException(e);
568573
}
@@ -581,7 +586,7 @@ public Sftp put(String srcFilePath, String destPath, SftpProgressMonitor monitor
581586
*/
582587
public Sftp put(InputStream srcStream, String destPath, SftpProgressMonitor monitor, Mode mode) {
583588
try {
584-
channel.put(srcStream, destPath, monitor, mode.ordinal());
589+
getClient().put(srcStream, destPath, monitor, mode.ordinal());
585590
} catch (SftpException e) {
586591
throw new JschRuntimeException(e);
587592
}
@@ -644,7 +649,7 @@ public void recursiveDownloadFolder(String sourcePath, File destDir) throws Jsch
644649
*/
645650
public Sftp get(String src, String dest) {
646651
try {
647-
channel.get(src, dest);
652+
getClient().get(src, dest);
648653
} catch (SftpException e) {
649654
throw new JschRuntimeException(e);
650655
}
@@ -661,7 +666,7 @@ public Sftp get(String src, String dest) {
661666
*/
662667
public Sftp get(String src, OutputStream out) {
663668
try {
664-
channel.get(src, out);
669+
getClient().get(src, out);
665670
} catch (SftpException e) {
666671
throw new JschRuntimeException(e);
667672
}

0 commit comments

Comments
 (0)