Java URL is Absolute getAbsoluteURL(String baseURLString, String relURlString)

Here you can find the source of getAbsoluteURL(String baseURLString, String relURlString)

Description

get Absolute URL

License

Open Source License

Declaration

public static final String getAbsoluteURL(String baseURLString, String relURlString) 

Method Source Code


//package com.java2s;
import java.io.IOException;

import java.net.MalformedURLException;

import java.net.URL;

public class Main {
    /** *///  w w  w .  j  a v  a  2 s.c  o  m
    public static final String getAbsoluteURL(String baseURLString, String relURlString) {
        try {
            URL baseURL = new URL(baseURLString);
            String url = baseURL.getProtocol() + "://" + baseURL.getHost() + ":" + baseURL.getPort()
                    + toRelativeURL(relURlString);
            return url;
        } catch (IOException e) {
            return "";
        }
    }

    /** */
    public static final String toRelativeURL(String urlString, boolean withParam) {
        String uri = null;
        try {
            URL url = new URL(urlString);
            uri = url.getPath();
            if (withParam == true) {
                String queryString = url.getQuery();
                if (!queryString.equals("")) {
                    uri += ("?" + queryString);
                }
            }
            if (uri.endsWith("/")) {
                uri = uri.substring(0, uri.length() - 1);
            }
        } catch (MalformedURLException e) {
            if (urlString == null) {
                uri = "";
            } else if (0 < urlString.length() && urlString.charAt(0) != '/') {
                uri = "/" + urlString;
            } else {
                uri = urlString;
            }
        }
        return uri;
    }

    /** */
    public static final String toRelativeURL(String urlString) {
        return toRelativeURL(urlString, true);
    }
}

Related

  1. getAbsoluteUrl(String base_url, String rel_url)
  2. getAbsoluteURL(String baseUrl, String url)
  3. getAbsoluteURL(String relativeURL, String baseURL)
  4. getAbsoluteURL(String url, URL base)
  5. getAbsoluteUrl(URL baseUrl, String relativeUrl)
  6. getAbsoluteUrlFromFile(final String file)