-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathURL2Path.java
41 lines (30 loc) · 853 Bytes
/
URL2Path.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package de.bolben.utils;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
*@Author Benjamin Bolgrin
*
*This class resolves the host name of a URL and returns a Path representation.
*For Example:
*URL: http://www.bolben.de -> Path: de/bolben/www
*/
public class URL2Path {
private final URL url;
public URL2Path(URL url){
this.url = url;
}
public Path getPath(){
String hostName = url.getHost();
List<String> hostNameParts = new ArrayList<>(Arrays.asList(hostName.split("\\.")));
Collections.reverse(hostNameParts);
String pathName = "";
for(String s : hostNameParts){
pathName = pathName + "/" + s;
}
return Path.of(pathName);
}
}