From 9a088883a1f604845c88adb4078bd336fad9c4fc Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 15 Sep 2023 11:59:03 +0800 Subject: [PATCH] fix bug --- hutool-extra/pom.xml | 6 ++ .../client/engine/jdk/JdkClientEngine.java | 43 ++++---------- .../client/engine/jdk/JdkHttpConnection.java | 58 +++++++++---------- .../client/engine/jdk/JdkHttpResponse.java | 2 +- .../org/dromara/hutool/poi/word/WordUtil.java | 3 + 5 files changed, 51 insertions(+), 61 deletions(-) diff --git a/hutool-extra/pom.xml b/hutool-extra/pom.xml index 9898e74e5..368f4a059 100755 --- a/hutool-extra/pom.xml +++ b/hutool-extra/pom.xml @@ -215,6 +215,12 @@ compile true + + org.apache.sshd + sshd-core + 2.10.0 + + com.google.zxing diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkClientEngine.java b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkClientEngine.java index cec31d24d..8ce58689b 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkClientEngine.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkClientEngine.java @@ -42,7 +42,6 @@ import java.util.List; public class JdkClientEngine implements ClientEngine { private ClientConfig config; - private JdkHttpConnection conn; /** * 重定向次数计数器,内部使用 */ @@ -56,10 +55,6 @@ public class JdkClientEngine implements ClientEngine { @Override public JdkClientEngine init(final ClientConfig config) { this.config = config; - if(null != this.conn){ - this.conn.disconnectQuietly(); - this.conn = null; - } return this; } @@ -76,16 +71,16 @@ public class JdkClientEngine implements ClientEngine { * @return {@link Response} */ public JdkHttpResponse send(final Request message, final boolean isAsync) { - initConn(message); + final JdkHttpConnection conn = buildConn(message); try { - doSend(message); + doSend(conn, message); } catch (final IOException e) { // 出错后关闭连接 - IoUtil.closeQuietly(this); + IoUtil.closeQuietly(conn); throw new IORuntimeException(e); } - return sendRedirectIfPossible(message, isAsync); + return sendRedirectIfPossible(conn, message, isAsync); } @Override @@ -95,9 +90,7 @@ public class JdkClientEngine implements ClientEngine { @Override public void close() { - if (null != conn) { - conn.disconnectQuietly(); - } + // do nothing } /** @@ -106,28 +99,16 @@ public class JdkClientEngine implements ClientEngine { * @param message 请求消息 * @throws IOException IO异常 */ - private void doSend(final Request message) throws IOException { + private void doSend(final JdkHttpConnection conn, final Request message) throws IOException { final HttpBody body = message.body(); if (null != body) { // 带有消息体,一律按照Rest方式发送 - body.writeClose(this.conn.getOutputStream()); + body.writeClose(conn.getOutputStream()); return; } // 非Rest简单GET请求 - this.conn.connect(); - } - - /** - * 初始化连接对象 - * - * @param message 请求消息 - */ - private void initConn(final Request message) { - // 执行下次请求时自动关闭上次请求(常用于转发) - IoUtil.closeQuietly(this); - - this.conn = buildConn(message); + conn.connect(); } /** @@ -163,11 +144,11 @@ public class JdkClientEngine implements ClientEngine { /** * 调用转发,如果需要转发返回转发结果,否则返回{@code null} * + * @param conn {@link JdkHttpConnection}} * @param isAsync 最终请求是否异步 * @return {@link JdkHttpResponse},无转发返回 {@code null} */ - private JdkHttpResponse sendRedirectIfPossible(final Request message, final boolean isAsync) { - final JdkHttpConnection conn = this.conn; + private JdkHttpResponse sendRedirectIfPossible(JdkHttpConnection conn, final Request message, final boolean isAsync) { // 手动实现重定向 if (message.maxRedirectCount() > 0) { final int code; @@ -175,7 +156,7 @@ public class JdkClientEngine implements ClientEngine { code = conn.getCode(); } catch (final IOException e) { // 错误时静默关闭连接 - conn.disconnectQuietly(); + conn.closeQuietly(); throw new HttpException(e); } @@ -191,7 +172,7 @@ public class JdkClientEngine implements ClientEngine { } // 最终页面 - return new JdkHttpResponse(this.conn, true, message.charset(), isAsync, + return new JdkHttpResponse(conn, true, message.charset(), isAsync, isIgnoreResponseBody(message.method())); } diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkHttpConnection.java b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkHttpConnection.java index b556e53b0..4766b1280 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkHttpConnection.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkHttpConnection.java @@ -22,6 +22,7 @@ import org.dromara.hutool.http.meta.Method; import org.dromara.hutool.http.ssl.SSLInfo; import javax.net.ssl.HttpsURLConnection; +import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -37,7 +38,7 @@ import java.util.Map; * * @author Looly */ -public class JdkHttpConnection implements HeaderOperation { +public class JdkHttpConnection implements HeaderOperation, Closeable { private final URL url; private final Proxy proxy; @@ -315,34 +316,6 @@ public class JdkHttpConnection implements HeaderOperation { return this; } - /** - * 静默断开连接。不抛出异常 - * - * @return this - * @since 4.6.0 - */ - public JdkHttpConnection disconnectQuietly() { - try { - disconnect(); - } catch (final Throwable e) { - // ignore - } - - return this; - } - - /** - * 断开连接 - * - * @return this - */ - public JdkHttpConnection disconnect() { - if (null != this.conn) { - this.conn.disconnect(); - } - return this; - } - /** * 获得输入流对象
* 输入流对象用于读取数据 @@ -419,4 +392,31 @@ public class JdkHttpConnection implements HeaderOperation { return sb.toString(); } + /** + * 静默断开连接。不抛出异常 + * + * @return this + * @since 4.6.0 + */ + public JdkHttpConnection closeQuietly() { + try { + close(); + } catch (final Throwable e) { + // ignore + } + + return this; + } + + /** + * 断开连接 + * + */ + @Override + public void close() { + if (null != this.conn) { + this.conn.disconnect(); + } + } + } diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkHttpResponse.java b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkHttpResponse.java index 5823bda86..c48a0c9bd 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkHttpResponse.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkHttpResponse.java @@ -217,7 +217,7 @@ public class JdkHttpResponse implements Response, Closeable { // 关闭流 IoUtil.closeQuietly(this.body); // 关闭连接 - this.httpConnection.disconnectQuietly(); + this.httpConnection.closeQuietly(); } @Override diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/word/WordUtil.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/word/WordUtil.java index ca5029d97..8f8ad182e 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/word/WordUtil.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/word/WordUtil.java @@ -21,6 +21,9 @@ import java.io.File; * @since 4.5.16 */ public class WordUtil { + + + /** * 创建Word 07格式的生成器 *