6

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.

Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180
  • possible duplicate of [Building an absolute URL from a relative URL in Java](http://stackoverflow.com/questions/1389184/building-an-absolute-url-from-a-relative-url-in-java) – CoderNeji Jun 15 '15 at 11:29
  • Not a duplicate, they want the opposite thing there – Kaloyan Roussev Jun 15 '15 at 11:30
  • Sorry my mistake... Find your answer here http://www.java2s.com/Code/Java/Network-Protocol/ResolvearelativeURLstringagainstanabsoluteURLstring.htm – CoderNeji Jun 15 '15 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? – Francisco Romero Jun 15 '15 at 11:36

7 Answers7

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"
Community
  • 1
  • 1
Chop
  • 4,267
  • 5
  • 26
  • 58
2

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/
Nagarz
  • 178
  • 2
  • 13
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.

  • 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. :) – Chop Jun 15 '15 at 11:49
1

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
Hiren
  • 1,427
  • 1
  • 18
  • 35
1

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 '/'
sam_haz
  • 189
  • 3
  • 15
0

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();
Anton Krosnev
  • 3,964
  • 1
  • 21
  • 37
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

Fulin Xu
  • 1
  • 2