Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java URI.resolve

Tags:

I'm trying to resolve two URIs, but it's not as straightforward as I'd like it to be.

URI a = new URI("http://www.foo.com"); URI b = new URI("bar.html"); 

The trouble is that a.resolve(b).toString() is now "http://www.foo.combar.html". How can I get away with that?

like image 458
Cpa Avatar asked Mar 28 '10 19:03

Cpa


People also ask

What does URI resolve do?

resolve. Resolves the given URI against this URI. If the given URI is already absolute, or if this URI is opaque, then the given URI is returned.

What is URI in Java example?

A URI is a uniform resource identifier while a URL is a uniform resource locator. Hence every URL is a URI, abstractly speaking, but not every URI is a URL.

What is URI in spring?

Uniform Resource Identifier (URI) − a sequence of characters that allows the complete identification of any abstract or physical resource.


1 Answers

Sounds like you probably want to use URL rather than URI (which is more general and needs to deal with a less strict syntax.)

URI a = new URI("http://www.foo.com"); URI b = new URI("bar.html"); URI c = a.resolve(b); c.toString()     -> "http://www.foo.combar.html" c.getAuthority() -> "www.foo.com" c.getPath()      -> "bar.html" 

URI's toString() doesn't behave as you might expect, but given its general nature it may be that it should be forgiven.

Sadly URI's toURL() method doesn't behave quite as I would have hoped to give you what you want.

URL u = c.toURL(); u.toString()     -> "http://www.foo.combar.html" u.getAuthority() -> "www.foo.combar.html"  --- Oh dear :( 

So best just to start straight out with a URL to get what you want:

URL x = new URL("http://www.foo.com"); URL y = new URL(x, "bar.html"); y.toString() -> "http://www.foo.com/bar.html" 
like image 166
Mike Tunnicliffe Avatar answered Nov 10 '22 08:11

Mike Tunnicliffe