Java URL: Получить корень объекта URL (удалить путь) - PullRequest
0 голосов
/ 11 июля 2019

Есть ли способ получить только схему + хост + порт от заданного URL.

Итак, я хочу удалить сегмент URL пути.

URL aURL = new URL("http://example.com:80/docs/books/tutorial"
                       + "/index.html?name=networking#DOWNLOADING");

Я играл с этим:

System.out.println("protocol = " + aURL.getProtocol());
System.out.println("authority = " + aURL.getAuthority());
System.out.println("host = " + aURL.getHost());
System.out.println("port = " + aURL.getPort());
System.out.println("path = " + aURL.getPath());
System.out.println("query = " + aURL.getQuery());
System.out.println("filename = " + aURL.getFile());
System.out.println("ref = " + aURL.getRef());

Вывод:

protocol = http
authority = example.com:80
host = example.com
port = 80
path = /docs/books/tutorial/index.html
query = name=networking
filename = /docs/books/tutorial/index.html?name=networking
ref = DOWNLOADING

я хочу получить новый URL объект для http://example.com:8080

Есть идеи?

1 Ответ

0 голосов
/ 11 июля 2019

Просто используйте конструктор URL для создания нового URL.

URL newUrl = new URL(aURL.getProtocol(), aURL.getHost(), 8080, "/");
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...