From 921d5141c00524498f255e0eb852b37ea45df18b Mon Sep 17 00:00:00 2001 From: niumoo Date: Mon, 3 Aug 2020 14:35:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=B9=E6=B3=95=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/wdbyte/downbit/util/HttpUtls.java | 39 ++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/com/wdbyte/downbit/util/HttpUtls.java b/src/com/wdbyte/downbit/util/HttpUtls.java index 9a6c416..7fc0df5 100644 --- a/src/com/wdbyte/downbit/util/HttpUtls.java +++ b/src/com/wdbyte/downbit/util/HttpUtls.java @@ -15,13 +15,29 @@ */ public class HttpUtls { + /** + * 获取 HTTP 链接 + * + * @param url + * @return + * @throws IOException + */ public static HttpURLConnection getHttpUrlConnection(String url) throws IOException { URL httpUrl = new URL(url); - HttpURLConnection httpConnection = (HttpURLConnection)httpUrl.openConnection(); + HttpURLConnection httpConnection = (HttpURLConnection) httpUrl.openConnection(); httpConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"); return httpConnection; } + /** + * 获取 HTTP 链接 + * + * @param url + * @param start + * @param end + * @return + * @throws IOException + */ public static HttpURLConnection getHttpUrlConnection(String url, long start, Long end) throws IOException { HttpURLConnection httpUrlConnection = getHttpUrlConnection(url); if (end != null) { @@ -32,6 +48,13 @@ public static HttpURLConnection getHttpUrlConnection(String url, long start, Lon return httpUrlConnection; } + /** + * 获取网络文件大小 bytes + * + * @param url + * @return + * @throws IOException + */ public static long getHttpFileContentLength(String url) throws IOException { HttpURLConnection httpUrlConnection = getHttpUrlConnection(url); int contentLength = httpUrlConnection.getContentLength(); @@ -39,13 +62,27 @@ public static long getHttpFileContentLength(String url) throws IOException { return contentLength; } + /** + * 获取网络文件 Etag + * + * @param url + * @return + * @throws IOException + */ public static String getHttpFileEtag(String url) throws IOException { HttpURLConnection httpUrlConnection = getHttpUrlConnection(url); Map> headerFields = httpUrlConnection.getHeaderFields(); List eTagList = headerFields.get("ETag"); + httpUrlConnection.disconnect(); return eTagList.get(0); } + /** + * 获取网络文件名 + * + * @param url + * @return + */ public static String getHttpFileName(String url) { int indexOf = url.lastIndexOf("/"); return url.substring(indexOf + 1);