From 145ea0820d109a326a1d9e5fd60b315ba6e0565e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=81=AByzs?= <835476090@qq.com> Date: Fri, 29 May 2020 13:18:08 +0800 Subject: [PATCH] =?UTF-8?q?HttpUtil=E6=B7=BB=E5=8A=A0downloadBytes?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E2=9C=92=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hutool/http/HttpUtil.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java b/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java index 99db09344..820e6e653 100644 --- a/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java @@ -356,6 +356,26 @@ public class HttpUtil { return response.writeBody(out, isCloseOut, streamProgress); } + /** + * 下载远程文件数据 + * + * @param url 请求的url + * @return 文件数据 + */ + public static byte[] downloadBytes(String url) { + if (StrUtil.isBlank(url)) { + throw new NullPointerException("[url] is null!"); + } + + HttpRequest request = new HttpRequest(url); + request.setFollowRedirects(true); + final HttpResponse response = request.executeAsync(); + if (false == response.isOk()) { + throw new HttpException("Server response error with status code: [{}]", response.getStatus()); + } + return response.bodyBytes(); + } + /** * 将Map形式的Form表单数据转换为Url参数形式,不做编码 *