Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

parent location of a url  RSS feed

 
ramesh agarwal
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to retrive the path of a URL without the ending resource name.

For example, given, http://www.abcd.com/x/y/mnop.txt, I need to extract http://www.abcd.com/x/y.

How is this possible? I browsed the methods in java.net.URL but could not find any direct API.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because technically, a URL doesn't have any "parent URL". It's not strictly hierarchical like a folder on your file system. Usually it exists out of folders but it doesn't have to.

However, you can trick URL into thinking it does. If we think of a URL as a folder, then the current folder is http://www.abcd.com/x/y/. The short hand for the current folder is a single dot (.). By building a relative URL you can get what you want:
url2's String format will end in a / though. If you don't want it you can look for the last occurrence of /, then strip it and everything after it off using String.substring, and create a new URL from it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!