mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复HttpDownloader全局超时无效问题
This commit is contained in:
parent
7b1d72c029
commit
88cf47b5bb
@ -2,7 +2,7 @@
|
|||||||
# 🚀Changelog
|
# 🚀Changelog
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.8.28(2024-04-24)
|
# 5.8.28(2024-04-25)
|
||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
* 【core 】 修正XmlUtil的omitXmlDeclaration描述注释(issue#I9CPC7@Gitee)
|
* 【core 】 修正XmlUtil的omitXmlDeclaration描述注释(issue#I9CPC7@Gitee)
|
||||||
@ -13,6 +13,7 @@
|
|||||||
* 【cache 】 CacheUtil.newTimedCache增加有schedulePruneDelay参数的重载方法(issue#I9HO25@Gitee)
|
* 【cache 】 CacheUtil.newTimedCache增加有schedulePruneDelay参数的重载方法(issue#I9HO25@Gitee)
|
||||||
* 【core 】 NumberChineseFormatter提供阿拉伯转中文支持多位小数的方法(pr#3552@Github)
|
* 【core 】 NumberChineseFormatter提供阿拉伯转中文支持多位小数的方法(pr#3552@Github)
|
||||||
* 【captcha】 Captcha.setBackground为null时背景透明(issue#3558@Github)
|
* 【captcha】 Captcha.setBackground为null时背景透明(issue#3558@Github)
|
||||||
|
* 【captcha】 HttpDownloader.downloadBytes增加超时参数重载(issue#3556@Github)
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【http 】 修复HttpUtil.urlWithFormUrlEncoded方法重复编码问题(issue#3536@Github)
|
* 【http 】 修复HttpUtil.urlWithFormUrlEncoded方法重复编码问题(issue#3536@Github)
|
||||||
@ -25,6 +26,7 @@
|
|||||||
* 【core 】 修复TemporalAccessorConverter自定义格式转换问题(issue#I9HQQE@Gitee)
|
* 【core 】 修复TemporalAccessorConverter自定义格式转换问题(issue#I9HQQE@Gitee)
|
||||||
* 【cron 】 修复CronPattern.nextMatchAfter匹配初始值问题(issue#I9FQUA@Gitee)
|
* 【cron 】 修复CronPattern.nextMatchAfter匹配初始值问题(issue#I9FQUA@Gitee)
|
||||||
* 【core 】 修复FileUtil.copyFile没有创建父目录导致的问题(issue#3557@Github)
|
* 【core 】 修复FileUtil.copyFile没有创建父目录导致的问题(issue#3557@Github)
|
||||||
|
* 【http 】 修复HttpDownloader全局超时无效问题(issue#3556@Github)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.8.27(2024-03-29)
|
# 5.8.27(2024-03-29)
|
||||||
|
@ -37,7 +37,18 @@ public class HttpDownloader {
|
|||||||
* @return 文件数据
|
* @return 文件数据
|
||||||
*/
|
*/
|
||||||
public static byte[] downloadBytes(String url) {
|
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 文件大小
|
* @return 文件大小
|
||||||
*/
|
*/
|
||||||
public static long downloadFile(String url, File targetFileOrDir, int timeout, StreamProgress streamProgress) {
|
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);
|
return requestDownload(url, timeout).writeBody(targetFileOrDir, streamProgress);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,6 +80,8 @@ public class HttpDownloader {
|
|||||||
* @since 5.7.12
|
* @since 5.7.12
|
||||||
*/
|
*/
|
||||||
public static long downloadFile(String url, File targetFileOrDir, String tempFileSuffix, int timeout, StreamProgress streamProgress) {
|
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);
|
return requestDownload(url, timeout).writeBody(targetFileOrDir, tempFileSuffix, streamProgress);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,6 +95,9 @@ public class HttpDownloader {
|
|||||||
* @return 文件
|
* @return 文件
|
||||||
*/
|
*/
|
||||||
public static File downloadForFile(String url, File targetFileOrDir, int timeout, StreamProgress streamProgress) {
|
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);
|
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) {
|
public static long download(String url, OutputStream out, boolean isCloseOut, StreamProgress streamProgress) {
|
||||||
Assert.notNull(out, "[out] is null !");
|
Assert.notNull(out, "[out] is null !");
|
||||||
|
|
||||||
|
// writeBody后会自动关闭网络流
|
||||||
return requestDownload(url, -1).writeBody(out, isCloseOut, streamProgress);
|
return requestDownload(url, -1).writeBody(out, isCloseOut, streamProgress);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,10 +128,14 @@ public class HttpDownloader {
|
|||||||
private static HttpResponse requestDownload(String url, int timeout) {
|
private static HttpResponse requestDownload(String url, int timeout) {
|
||||||
Assert.notBlank(url, "[url] is blank !");
|
Assert.notBlank(url, "[url] is blank !");
|
||||||
|
|
||||||
final HttpResponse response = HttpUtil.createGet(url, true)
|
final HttpRequest request = HttpUtil.createGet(url, true);
|
||||||
.timeout(timeout)
|
if (timeout > 0) {
|
||||||
.executeAsync();
|
// 只有用户自定义了超时时长才有效,否则使用全局默认的超时时长。
|
||||||
|
request.timeout(timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
final HttpResponse response = request.executeAsync();
|
||||||
if (response.isOk()) {
|
if (response.isOk()) {
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user