aspose file tools*
The moose likes Beginning Java and the fly likes how to constract absolute URL from relative URL string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to constract absolute URL from relative URL string" Watch "how to constract absolute URL from relative URL string" New topic
Author

how to constract absolute URL from relative URL string

Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
Hello all
how can i get absolute URL from relative URL string like "../foo.html"
like like "http://www.mysite.com/stuff/foo.html"

thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

You will need the base URL as well, like "http://www.mysite.com/stuff/subfolder/". Then just use one of the URL constructors that takes a URL object as the first parameter:

Note that the base URL needs a trailing / if it is a folder, or the "subfolder" part will be considered as a file, and "stuff" will be considered as the current folder.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
but what if i have only the site host url like
"www.mysite.com"
and then i have in the links only the relative URl like :
..\..\foo.html
can i somehow construct full uri ?
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

This code should work

[ September 06, 2008: Message edited by: Rene Larsen ]

Regards, Rene Larsen
Dropbox Invite
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

If you don't know the current URL, you can't of course determine the absolute URL of any relative URL. For instance, ../foo.html will have a different absolute URL if found on www.mysite.com (www.mysite.com/foo.html), www.mysite.com/mypath (www.mysite.com/foo.html) or www.mysite.com/mypath/mysubpath (www.mysite.com/mypath/foo.html).

So your first step is: determine the full current URL, or you can't do a single thing. If you don't know the domain, you can actually add any domain and later cut it off again. "localhost" tends to be a good one
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to constract absolute URL from relative URL string