修复HttpDownloader全局超时无效问题

This commit is contained in:
Looly 2024-04-25 09:34:36 +08:00
parent 7b1d72c029
commit 88cf47b5bb
2 changed files with 30 additions and 5 deletions

View File

@ -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)

View File

@ -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;
}