diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/math/NumberUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/math/NumberUtil.java index ec3881378..332d530b3 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/math/NumberUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/math/NumberUtil.java @@ -638,6 +638,20 @@ public class NumberUtil extends NumberValidator { format.setMaximumFractionDigits(scale); return format.format(number); } + + /** + * 格式化千分位表示方式,小数采用四舍五入方式 + * + * @param number 值 + * @param scale 保留小数位数 + * @return 千分位数字 + * @since 6.0.0 + */ + public static String formatThousands(final double number, final int scale) { + final NumberFormat format = NumberFormat.getNumberInstance(); + format.setMaximumFractionDigits(scale); + return format.format(number); + } // endregion // region ----- range diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/util/NumberUtilTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/util/NumberUtilTest.java index 044cfcedc..498e1f4c1 100644 --- a/hutool-core/src/test/java/org/dromara/hutool/core/util/NumberUtilTest.java +++ b/hutool-core/src/test/java/org/dromara/hutool/core/util/NumberUtilTest.java @@ -746,4 +746,12 @@ public class NumberUtilTest { void issueI6ZD1RTest() { Assertions.assertFalse(NumberUtil.isInteger("999999999999999")); } + + @Test + void formatThousands() { + // issue#I7OIA6 + Assertions.assertEquals( + "123,456,789.111111", + NumberUtil.formatThousands(123456789.111111D, 6)); + } } diff --git a/hutool-extra/src/main/java/org/dromara/hutool/extra/ssh/Sftp.java b/hutool-extra/src/main/java/org/dromara/hutool/extra/ssh/Sftp.java index 338954c45..6c6ae838e 100644 --- a/hutool-extra/src/main/java/org/dromara/hutool/extra/ssh/Sftp.java +++ b/hutool-extra/src/main/java/org/dromara/hutool/extra/ssh/Sftp.java @@ -245,6 +245,9 @@ public class Sftp extends AbstractFtp { * @since 4.1.14 */ public ChannelSftp getClient() { + if(false == this.channel.isConnected()){ + init(); + } return this.channel; } @@ -256,7 +259,7 @@ public class Sftp extends AbstractFtp { @Override public String pwd() { try { - return channel.pwd(); + return getClient().pwd(); } catch (final SftpException e) { throw new JschRuntimeException(e); } @@ -270,7 +273,7 @@ public class Sftp extends AbstractFtp { */ public String home() { try { - return channel.getHome(); + return getClient().getHome(); } catch (final SftpException e) { throw new JschRuntimeException(e); } @@ -351,7 +354,7 @@ public class Sftp extends AbstractFtp { public List lsEntries(final String path, final Predicate predicate) { final List entryList = new ArrayList<>(); try { - channel.ls(path, entry -> { + getClient().ls(path, entry -> { final String fileName = entry.getFilename(); if (!StrUtil.equals(".", fileName) && !StrUtil.equals("..", fileName)) { if (null == predicate || predicate.test(entry)) { @@ -376,7 +379,7 @@ public class Sftp extends AbstractFtp { return true; } try { - this.channel.mkdir(dir); + getClient().mkdir(dir); return true; } catch (final SftpException e) { throw new JschRuntimeException(e); @@ -387,7 +390,7 @@ public class Sftp extends AbstractFtp { public boolean isDir(final String dir) { final SftpATTRS sftpATTRS; try { - sftpATTRS = this.channel.stat(dir); + sftpATTRS = getClient().stat(dir); } catch (final SftpException e) { final String msg = e.getMessage(); // issue#I4P9ED@Gitee @@ -415,7 +418,7 @@ public class Sftp extends AbstractFtp { return true; } try { - channel.cd(directory.replace('\\', '/')); + getClient().cd(directory.replace('\\', '/')); return true; } catch (final SftpException e) { throw new FtpException(e); @@ -430,7 +433,7 @@ public class Sftp extends AbstractFtp { @Override public boolean delFile(final String filePath) { try { - channel.rm(filePath); + getClient().rm(filePath); } catch (final SftpException e) { throw new JschRuntimeException(e); } @@ -450,6 +453,8 @@ public class Sftp extends AbstractFtp { return false; } + final ChannelSftp channel = getClient(); + final Vector list; try { list = channel.ls(channel.pwd()); @@ -576,7 +581,7 @@ public class Sftp extends AbstractFtp { */ public Sftp put(final String srcFilePath, final String destPath, final SftpProgressMonitor monitor, final Mode mode) { try { - channel.put(srcFilePath, destPath, monitor, mode.ordinal()); + getClient().put(srcFilePath, destPath, monitor, mode.ordinal()); } catch (final SftpException e) { throw new JschRuntimeException(e); } @@ -595,7 +600,7 @@ public class Sftp extends AbstractFtp { */ public Sftp put(final InputStream srcStream, final String destPath, final SftpProgressMonitor monitor, final Mode mode) { try { - channel.put(srcStream, destPath, monitor, mode.ordinal()); + getClient().put(srcStream, destPath, monitor, mode.ordinal()); } catch (final SftpException e) { throw new JschRuntimeException(e); } @@ -658,7 +663,7 @@ public class Sftp extends AbstractFtp { */ public Sftp get(final String src, final String dest) { try { - channel.get(src, dest); + getClient().get(src, dest); } catch (final SftpException e) { throw new JschRuntimeException(e); } @@ -675,7 +680,7 @@ public class Sftp extends AbstractFtp { */ public Sftp get(final String src, final OutputStream out) { try { - channel.get(src, out); + getClient().get(src, out); } catch (final SftpException e) { throw new JschRuntimeException(e); }