Java Utililty Methods URL Create

List of utility methods to do URL Create

Description

The list of methods to do URL Create are organized into topic(s).

Method

StringbuildUrl(String URL, Map params)
build Url
if (URL == null)
    return null;
String qs = buildQueryString(params);
return URL.indexOf("?") < 0 ? URL + "?" + qs : URL + "&" + qs;
URLbuildUrl(String urlPrefix, String urlSuffix)
build Url
String urlPrefixFixed = urlPrefix.trim();
while (urlPrefixFixed.endsWith("/")) { 
    urlPrefixFixed = urlPrefixFixed.substring(0, urlPrefixFixed.length() - 1);
URL url = new URL(urlPrefixFixed + "/" + urlSuffix);
return url;
StringbuildURL(URI base, Multimap params)
build URL
if (params.isEmpty()) {
    return base.toString();
} else {
    return base + "?" + buildQueryString(params);
StringbuildUrlPath(String baseUrl, String childUrl)
build Url Path
try {
    URI oldUri = new URI(baseUrl);
    URI resolved = oldUri.resolve(childUrl);
    return resolved.toString();
} catch (URISyntaxException e) {
    e.printStackTrace();
    return baseUrl;
ListbuildUrlsList(final String domain, final String... paths)
Builds a List of URL s given a domain and 1..N paths relative to the domain.
return Arrays.stream(paths).map(p -> buildUrl(domain, p)).collect(Collectors.toList());
StringbuildURLString(Iterable elements, String joiner)
build URL String
Iterator<String> iter = elements.iterator();
if (!iter.hasNext()) {
    return "";
String wholequery = iter.next();
try {
    while (iter.hasNext()) {
        wholequery += joiner + URLEncoder.encode(iter.next(), "UTF-8");
...
URIconcatenate(URL server, String address)
Builds a complete URI given a URL that specifies the server and a string that is the remainder of the query.
try {
    return new URL(server, Preconditions.checkNotNull(address)).toURI();
} catch (MalformedURLException | URISyntaxException e) {
    throw new IllegalArgumentException(e);
URLconcatenateURL(final URL url, final String query)
Concatenates the given java.net.URL and query.
try {
    if (url.getQuery() != null && url.getQuery().length() > 0) {
        return new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + "&" + query);
    } else {
        return new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + "?" + query);
} catch (final MalformedURLException ex) {
    throw new IllegalArgumentException("Could not concatenate given URL with GET arguments!", ex);
...
URLconcatenateURL(URL url, String query)
concatenate URL
try {
    return url.getQuery() != null && url.getQuery().length() > 0
            ? new URL(url.getProtocol(), url.getHost(), url.getFile() + "&" + query)
            : new URL(url.getProtocol(), url.getHost(), url.getFile() + "?" + query);
} catch (MalformedURLException e) {
    throw new IllegalArgumentException("Concatenated URL was malformed: " + url.toString() + ", " + query);
URLconcatUrl(final URL baseUrl, final String extraPath)
concat Url
try {
    URI uri = baseUrl.toURI();
    String newPath = uri.getPath() + (uri.getPath().endsWith("/") ? "" : "/") + extraPath;
    URI newUri = uri.resolve(newPath);
    return newUri.toURL();
} catch (Exception e) {
    throw new RuntimeException("Can't add extra path " + extraPath + " to url " + baseUrl, e);