Skip to content

Commit

Permalink
修复多线程下Sftp中Channel关闭的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Jul 29, 2023
1 parent 68daef7 commit e8c1f7a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 16 additions & 11 deletions hutool-extra/src/main/java/cn/hutool/extra/ssh/Sftp.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ public Sftp reconnectIfTimeout() {
* @since 4.1.14
*/
public ChannelSftp getClient() {
if(false == this.channel.isConnected()){
init();
}
return this.channel;
}

Expand All @@ -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);
}
Expand All @@ -258,7 +261,7 @@ public String pwd() {
*/
public String home() {
try {
return channel.getHome();
return getClient().getHome();
} catch (SftpException e) {
throw new JschRuntimeException(e);
}
Expand Down Expand Up @@ -339,7 +342,7 @@ public List<LsEntry> lsEntries(String path) {
public List<LsEntry> lsEntries(String path, Filter<LsEntry> filter) {
final List<LsEntry> 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)) {
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand All @@ -438,6 +441,8 @@ public boolean delDir(String dirPath) {
return false;
}

final ChannelSftp channel = getClient();

Vector<LsEntry> list;
try {
list = channel.ls(channel.pwd());
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down

0 comments on commit e8c1f7a

Please sign in to comment.