7

I have this website:

https://asd.com/somestuff/another.html

and I want to extract the relative part out of it:

somestuff/another.html

How do I do that?

EDIT: I was offered an answer to a question, but the problem there was to build the absolute url out of the relative which is not what I'm interested in.

4
  • possible duplicate of Building an absolute URL from a relative URL in Java Commented Jun 15, 2015 at 11:29
  • Not a duplicate, they want the opposite thing there Commented Jun 15, 2015 at 11:30
  • Sorry my mistake... Find your answer here java2s.com/Code/Java/Network-Protocol/… Commented Jun 15, 2015 at 11:32
  • @J.K. According to your new edit. Where are you going to trying to retrieve the absolute path if it is not from your relative website? Commented Jun 15, 2015 at 11:36

7 Answers 7

9

You could use the getPath() method of the URL object:

URL url = new URL("https://asd.com/somestuff/another.html");
System.out.println(url.getPath());  // prints "/somestuff/another.html"

Now, this only brings the actual path. If you need more information (the anchor or the parameters passed as get values), you need to call other accessors of the URL object:

URL url = new URL("https://asd.com/somestuff/another.html?param=value#anchor");
System.out.println(url.getPath());  // prints "/somestuff/another.html"
System.out.println(url.getQuery()); // prints "param=value"
System.out.println(url.getRef());   // prints "anchor"

A possible use to generate the relative URL without much code, based on Hiru's answer:

URL absolute = new URL(url, "/");
String relative = url.toString().substring(absolute.toString().length());
System.out.println(relative); // prints "somestuff/another.html?param=value#anchor"
Sign up to request clarification or add additional context in comments.

1 Comment

very brief description!! +1 @Chop
1

if you know that the domain will always be .com then you can try something like this:

String url = "https://asd.com/somestuff/another.html";
String[] parts = url.split(".com/");
//parts[1] is the string after the .com/

2 Comments

it would be .com specific.. what if its "asd.in/somestuff/another.html" or something else
@Hiru This response assumes that url contains "XXX.com".
1

The URL consists of the following elements (note that some optional elements are omitted): 1) scheme 2) hostname 3) [port] 4) path 5) query 6) fragment Using the Java URL API, you can do the following:

URL u = new URL("https://randomsite.org/another/randomPage.html");
System.out.println(u.getPath());

Edit#1 Seeing Chop's answer, in case you have query elements in your URL, such as

?name=foo&value=bar

Using the getQuery() method will not return the resource path, just the query part.

1 Comment

Yep, I was mistaken and made a test to make sure I was not giving bad advice. I also included more details about getQuery() and getRef() before seeing your answer. Great minds think alike. :)
1

My solution based on java.net.URI

URI _absoluteURL = new URI(absoluteUrl).normalize();
String root = _absoluteURL.getScheme() + "://" + _absoluteURL.getAuthority();
URI relative = new URI(root).relativize(_absoluteURL);

String result = relative.toString();

Comments

0

You can do this using below snippet.

 String str="https://asd.org/somestuff/another.html";
    if(str.contains("//")) //To remove any protocol specific header.
    {
        str=str.split("//")[1];
    }
    System.out.println(str.substring(str.indexOf("/")+1)); // taking the first '/'

Comments

0

Try This

Use it Globally not only for .com

    URL u=new URL("https://asd.in/somestuff/another.html");
    String u1=new URL(u, "/").toString();
    String u2=u.toString();
    String[] u3=u2.split(u1);
    System.out.println(u3[1]); //it prints:   somestuff/another.html

Comments

0

Consider using Apache Commons VFS...

import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.VFS;
import org.apache.commons.vfs2.impl.StandardFileSystemManager;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLStreamHandlerFactory;

public class StudyURI {
    public static void main(String[] args) throws URISyntaxException, FileSystemException {
        StandardFileSystemManager fileSystemManager = (StandardFileSystemManager) VFS.getManager();
        URLStreamHandlerFactory factory = fileSystemManager.getURLStreamHandlerFactory();
        URL.setURLStreamHandlerFactory(factory);

        URI baseURI = fileSystemManager.resolveFile("https://asd.com/").getURI();
        URI anotherURI =fileSystemManager.resolveFile("https://asd.com/somestuff/another.html").getURI();

        String result = baseURI.relativize(anotherURI).getPath();

        System.out.println(result);
    }
}

Maybe you need to add module to run the code:
https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.