Skip to content

Instantly share code, notes, and snippets.

@brunoborges
Created December 21, 2012 20:43
Show Gist options
  • Select an option

  • Save brunoborges/4355670 to your computer and use it in GitHub Desktop.

Select an option

Save brunoborges/4355670 to your computer and use it in GitHub Desktop.
Proposal for getBasePath() on java.net.URL
public static URL getBasePath(URL url) throws MalformedURLException {
int lastSlash = url.getFile().lastIndexOf('/');
if (lastSlash == -1) {
return url;
}
String path = url.getFile().substring(0, lastSlash);
URL basePath = new URL(url.getProtocol(), url.getHost(), url.getPort(), path);
return basePath;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment