From f4fc97f9de647e8218d968c5fc9c785afedf7de9 Mon Sep 17 00:00:00 2001 From: Looly Date: Sun, 19 Apr 2020 09:38:17 +0800 Subject: [PATCH] add NetUtil.isopen --- CHANGELOG.md | 4 +++- .../main/java/cn/hutool/core/net/NetUtil.java | 21 +++++++++++++++++-- .../main/java/cn/hutool/db/PageResult.java | 6 +++--- .../java/cn/hutool/db/PageResultTest.java | 14 +++++++++++++ .../cn/hutool/http/server/DocServerTest.java | 17 +++++++++++++++ 5 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 hutool-db/src/test/java/cn/hutool/db/PageResultTest.java create mode 100644 hutool-http/src/test/java/cn/hutool/http/server/DocServerTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index b60d772a0..a4d3832cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,10 +3,12 @@ ------------------------------------------------------------------------------------------------------------- -## 5.3.2 (2020-04-17) +## 5.3.2 (2020-04-19) ### 新特性 +* 【core 】 增加NetUtil.isOpen方法 ### Bug修复 +* 【db 】 修复PageResult.isLast计算问题 ------------------------------------------------------------------------------------------------------------- ## 5.3.1 (2020-04-17) diff --git a/hutool-core/src/main/java/cn/hutool/core/net/NetUtil.java b/hutool-core/src/main/java/cn/hutool/core/net/NetUtil.java index 6709c719e..1c218677f 100644 --- a/hutool-core/src/main/java/cn/hutool/core/net/NetUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/net/NetUtil.java @@ -714,12 +714,29 @@ public class NetUtil { * @return cookie字符串 * @since 5.2.6 */ - public static List parseCookies(String cookieStr){ - if(StrUtil.isBlank(cookieStr)){ + public static List parseCookies(String cookieStr) { + if (StrUtil.isBlank(cookieStr)) { return Collections.emptyList(); } return HttpCookie.parse(cookieStr); } + + /** + * 检查远程端口是否开启 + * + * @param address 远程地址 + * @param timeout 检测超时 + * @return 远程端口是否开启 + * @since 5.3.2 + */ + public static boolean isOpen(InetSocketAddress address, int timeout) { + try (Socket sc = new Socket()){ + sc.connect(address, timeout); + return true; + } catch (Exception e) { + return false; + } + } // ----------------------------------------------------------------------------------------- Private method start /** diff --git a/hutool-db/src/main/java/cn/hutool/db/PageResult.java b/hutool-db/src/main/java/cn/hutool/db/PageResult.java index 1be7b2cff..ffbec81fc 100644 --- a/hutool-db/src/main/java/cn/hutool/db/PageResult.java +++ b/hutool-db/src/main/java/cn/hutool/db/PageResult.java @@ -1,9 +1,9 @@ package cn.hutool.db; -import java.util.ArrayList; - import cn.hutool.core.util.PageUtil; +import java.util.ArrayList; + /** * 分页数据结果集 * @@ -169,6 +169,6 @@ public class PageResult extends ArrayList { * @return 是否最后一页 */ public boolean isLast() { - return this.page >= this.totalPage; + return this.page >= (this.totalPage - 1); } } diff --git a/hutool-db/src/test/java/cn/hutool/db/PageResultTest.java b/hutool-db/src/test/java/cn/hutool/db/PageResultTest.java new file mode 100644 index 000000000..61f9a2876 --- /dev/null +++ b/hutool-db/src/test/java/cn/hutool/db/PageResultTest.java @@ -0,0 +1,14 @@ +package cn.hutool.db; + +import org.junit.Assert; +import org.junit.Test; + +public class PageResultTest { + + @Test + public void isLastTest(){ + // 每页2条,共10条,总共5页,第一页是0,最后一页应该是4 + final PageResult result = new PageResult<>(4, 2, 10); + Assert.assertTrue(result.isLast()); + } +} diff --git a/hutool-http/src/test/java/cn/hutool/http/server/DocServerTest.java b/hutool-http/src/test/java/cn/hutool/http/server/DocServerTest.java new file mode 100644 index 000000000..198af98c6 --- /dev/null +++ b/hutool-http/src/test/java/cn/hutool/http/server/DocServerTest.java @@ -0,0 +1,17 @@ +package cn.hutool.http.server; + +import cn.hutool.core.swing.DesktopUtil; +import cn.hutool.http.HttpUtil; + +public class DocServerTest { + + public static void main(String[] args) { + HttpUtil.createServer(80) + // 设置默认根目录, + .setRoot("D:\\workspace\\site\\hutool-site") + // 返回JSON数据测试 + .start(); + + DesktopUtil.browse("http://localhost/"); + } +}