Skip to content

Commit

Permalink
- changing URL utils method interfaces from HTTPURLConnection to a mo…
Browse files Browse the repository at this point in the history
…re generic URLConnection
  • Loading branch information
emarx committed Apr 24, 2017
1 parent 96dedee commit 0a9e03a
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions kbox.core/src/main/java/org/aksw/kbox/utils/URLUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,24 @@ public static URL[] fileToURL(File... files) throws MalformedURLException {
}

public static long getContentLength(URL url) throws IOException {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
List<String> values = conn.getHeaderFields().get("content-length");
conn.disconnect();
if(values != null) {
return Long.valueOf(values.get(0));
URLConnection conn = url.openConnection();
Long contentLength = conn.getContentLengthLong();
if(contentLength != null && contentLength >= 0) {
return contentLength;
}
String contentLengthValue = conn.getHeaderField("content-length");
if(contentLengthValue != null) {
return Long.valueOf(contentLengthValue);
}
return -1;
}

public static URL getURLForward(URL url) throws Exception {
URLConnection con = url.openConnection();
String location = con.getHeaderField("Location");
if(location != null) {
return new URL(location);
}
return url;
}
}

0 comments on commit 0a9e03a

Please sign in to comment.