Skip to content

Commit

Permalink
增加方法注释
Browse files Browse the repository at this point in the history
  • Loading branch information
niumoo committed Aug 3, 2020
1 parent 0ecfb9b commit 921d514
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/com/wdbyte/downbit/util/HttpUtls.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -32,20 +48,41 @@ 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();
httpUrlConnection.disconnect();
return contentLength;
}

/**
* 获取网络文件 Etag
*
* @param url
* @return
* @throws IOException
*/
public static String getHttpFileEtag(String url) throws IOException {
HttpURLConnection httpUrlConnection = getHttpUrlConnection(url);
Map<String, List<String>> headerFields = httpUrlConnection.getHeaderFields();
List<String> 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);
Expand Down

0 comments on commit 921d514

Please sign in to comment.