From 88cf47b5bbb8367d67a25d4cedbfb3a616f263d8 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 25 Apr 2024 09:34:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DHttpDownloader=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E8=B6=85=E6=97=B6=E6=97=A0=E6=95=88=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 ++- .../java/cn/hutool/http/HttpDownloader.java | 31 ++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fce51e6fc..83ecc11a5 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ # 🚀Changelog ------------------------------------------------------------------------------------------------------------- -# 5.8.28(2024-04-24) +# 5.8.28(2024-04-25) ### 🐣新特性 * 【core 】 修正XmlUtil的omitXmlDeclaration描述注释(issue#I9CPC7@Gitee) @@ -13,6 +13,7 @@ * 【cache 】 CacheUtil.newTimedCache增加有schedulePruneDelay参数的重载方法(issue#I9HO25@Gitee) * 【core 】 NumberChineseFormatter提供阿拉伯转中文支持多位小数的方法(pr#3552@Github) * 【captcha】 Captcha.setBackground为null时背景透明(issue#3558@Github) +* 【captcha】 HttpDownloader.downloadBytes增加超时参数重载(issue#3556@Github) ### 🐞Bug修复 * 【http 】 修复HttpUtil.urlWithFormUrlEncoded方法重复编码问题(issue#3536@Github) @@ -25,6 +26,7 @@ * 【core 】 修复TemporalAccessorConverter自定义格式转换问题(issue#I9HQQE@Gitee) * 【cron 】 修复CronPattern.nextMatchAfter匹配初始值问题(issue#I9FQUA@Gitee) * 【core 】 修复FileUtil.copyFile没有创建父目录导致的问题(issue#3557@Github) +* 【http 】 修复HttpDownloader全局超时无效问题(issue#3556@Github) ------------------------------------------------------------------------------------------------------------- # 5.8.27(2024-03-29) diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpDownloader.java b/hutool-http/src/main/java/cn/hutool/http/HttpDownloader.java index 4c98f4f00..cf1a6f15b 100644 --- a/hutool-http/src/main/java/cn/hutool/http/HttpDownloader.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpDownloader.java @@ -37,7 +37,18 @@ public class HttpDownloader { * @return 文件数据 */ public static byte[] downloadBytes(String url) { - return requestDownload(url, -1).bodyBytes(); + return downloadBytes(url, 0); + } + + /** + * 下载远程文件数据,支持30x跳转 + * + * @param url 请求的url + * @param timeout 超时毫秒数 + * @return 文件数据 + */ + public static byte[] downloadBytes(String url, int timeout) { + return requestDownload(url, timeout).bodyBytes(); } /** @@ -50,6 +61,8 @@ public class HttpDownloader { * @return 文件大小 */ public static long downloadFile(String url, File targetFileOrDir, int timeout, StreamProgress streamProgress) { + Assert.notNull(targetFileOrDir, "[targetFileOrDir] is null !"); + return requestDownload(url, timeout).writeBody(targetFileOrDir, streamProgress); } @@ -67,6 +80,8 @@ public class HttpDownloader { * @since 5.7.12 */ public static long downloadFile(String url, File targetFileOrDir, String tempFileSuffix, int timeout, StreamProgress streamProgress) { + Assert.notNull(targetFileOrDir, "[targetFileOrDir] is null !"); + return requestDownload(url, timeout).writeBody(targetFileOrDir, tempFileSuffix, streamProgress); } @@ -80,6 +95,9 @@ public class HttpDownloader { * @return 文件 */ public static File downloadForFile(String url, File targetFileOrDir, int timeout, StreamProgress streamProgress) { + Assert.notNull(targetFileOrDir, "[targetFileOrDir] is null !"); + + // writeBody后会自动关闭网络流 return requestDownload(url, timeout).writeBodyForFile(targetFileOrDir, streamProgress); } @@ -95,6 +113,7 @@ public class HttpDownloader { public static long download(String url, OutputStream out, boolean isCloseOut, StreamProgress streamProgress) { Assert.notNull(out, "[out] is null !"); + // writeBody后会自动关闭网络流 return requestDownload(url, -1).writeBody(out, isCloseOut, streamProgress); } @@ -109,10 +128,14 @@ public class HttpDownloader { private static HttpResponse requestDownload(String url, int timeout) { Assert.notBlank(url, "[url] is blank !"); - final HttpResponse response = HttpUtil.createGet(url, true) - .timeout(timeout) - .executeAsync(); + final HttpRequest request = HttpUtil.createGet(url, true); + if (timeout > 0) { + // 只有用户自定义了超时时长才有效,否则使用全局默认的超时时长。 + request.timeout(timeout); + } + + final HttpResponse response = request.executeAsync(); if (response.isOk()) { return response; }