Java URI.resolve

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?

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"

URI (Java Platform SE 7 ), resolve(u.relativize(v)).equals(v) . This operation is often useful when constructing a document containing URIs that must be made relative to the� Java URI.resolve. Ask Question Asked 10 years, 3 months ago. Active 7 months ago. Viewed 18k times 19. 4. I'm trying to resolve two URIs, but it's not as

URI should contain the final separator('/') as well to resolve the way you want:

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

java.net.URI.resolve java code examples, @param relative * the given string to create the new URI instance which has to * be resolved later on. * @return the created and resolved URI. */ public URI� Java . This code will resolve the relative base uri e.g. if current base uri is /path1/path2 then ../path3 will be resolved as /path1/path3.

Ok, appears from URL deffinition scheme://domain:port/path?query_string#fragment_id there should be 3 slashes before path (two after scheme and one directly before path) 2 situation can occure:

    there are 3 slashes in your URI => everything is OK
    there are less then 3 slashes in your URI => you need to add slash at the end of URI

there is my snappet of code:

String url = "http://www.foo.com";
String endSlash="";
int indexOfSlash = 0;
for(int i = 0;i<3;i++){
   int nextIndex = url.indexOf('/',indexOfSlash);
   if(!(nextIndex>0)){
      if(i>1){
         endSlash="/";
      }else{
         throw new MalformedURLException("Bad given url format, mising :// after schema");
      }
   }else{
      indexOfSlash = ++nextIndex;
   }
}
URL rightUrl = new URL(url+endSlash);

Java Code Examples for java.net.URI#resolve, In the following code shows how to use URI.resolve(String str) method. import java.net.URI; import java.net.URISyntaxException; public class Main { public static� The following are Jave code examples for showing how to use resolve() of the java.net.URI class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.

URI.resolve behaves like if you are on a HTML page like http://example.org/path/to/menu.html and click a link with href="page1.html": It cuts off the last segment (here menu.html) and puts page1.html in its place.

(http://example.org/path/to/menu.html, page1.html) → http://example.org/path/to/page1.html

This works also, if the object you call resolve on is a directory, denoted by ending in a slash:

(http://example.org/path/to/, page1.html) → http://example.org/path/to/page1.html

If it does not end in a slash, the outcome is not what you might expect:

(http://example.org/path/to, page1.html) → http://example.org/path/page1.html (missing "to")

If you know that the first argument of the URIs to concatenate is a directory, but you don’t know in which format you get it (with or without trailing slash), this might help you:

static URI asDirectory(URI uri) {
    String uriString = uri.toString();
    return !uriString.endsWith("/") ? URI.create(uriString.concat("/")) : uri;
}

Java API Tutorial - Java URI.resolve(String str), Syntax : public URI resolve(URI uri) Parameters : uri : URI to be resolved. Another overloaded method which takes string as argument and is equivalent to calling� This convenience method works as if invoking it were equivalent to evaluating the expression {@link #resolve(java.net.URI) resolve}(URI.{@link #create(String) create}(str)). Parameters: str - The string to be parsed into a URI

Java.net.URI class in Java, Java. This code will resolve the relative base uri e.g. if current base uri is /path1/ path2 then ../path3 will be resolved as /path1/path3. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

How to resolve relative URI in Java?, Creates a new URI instance by parsing the given string relative and resolves the created URI against the URI represented by this instance. Resolve(URI) Resolve( � I can observe the issue on jdk1.7.0_71, jdk1.8.0_211, but 9, 10, 11, 12, 13 and mainline behave properly.

URI.Resolve Method (Java.Net), URI. However, the standard Java class has a bug. The Java URI class does It depends on the protocol (scheme) used which root is used to resolve the path. FULL PRODUCT VERSION : java version "1.5.0" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64) Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode) ADDITIONAL OS VERSION INFORMATION : Microsoft Windows XP [Version 5.1.2600] A DESCRIPTION OF THE PROBLEM : An exception is thrown where one should not be.

Comments
  • What was the output you were expecting?
  • "foo.com/bar.html" would have been fine.
  • Why are you using two URIs? There must be a reason but it's unclear.