Java URL is Absolute getAbsoluteURL(String url, URL base)

Here you can find the source of getAbsoluteURL(String url, URL base)

Description

interprets url w.r.t base and returns the corresponding absolute URL (only if url is indeed a relative URL ; returns the original url otherwise)

License

Open Source License

Declaration

public static URL getAbsoluteURL(String url, URL base) 

Method Source Code

//package com.java2s;

import java.net.URL;
import java.net.MalformedURLException;

public class Main {
    /**//from  ww  w .ja v  a2  s .c o m
     *interprets url w.r.t base and returns the corresponding absolute URL (only if url is indeed a relative URL ; returns the original url otherwise)
     */
    public static URL getAbsoluteURL(String url, URL base) {
        URL res = null;
        if (url.startsWith("http:") || url.startsWith("file:") || url.startsWith("ftp:")) {
            //url seems to be an absolute URL
            try {//try to instantiate it, return null if it fails for some reason
                res = new URL(url);
                return res;
            } catch (MalformedURLException mue) {
                System.err.println("Error:Utils.getAbsoluteURL():malformed URL: " + url);
                mue.printStackTrace();
                return null;
            }
        } else {//url seems to be a relative URL
            try {//try to interpret it w.r.t base
                res = new URL(new URL(base.toString().substring(0, base.toString().lastIndexOf("/") + 1)), url);
                return res;
            } catch (MalformedURLException mue) {
                System.err.println("Error:Utils.getAbsoluteURL():malformed URL: " + url);
                mue.printStackTrace();
                return null;
            }
        }
    }
}

Related

  1. getAbsoluteUrl(String base_url, String rel_url)
  2. getAbsoluteURL(String baseUrl, String url)
  3. getAbsoluteURL(String baseURLString, String relURlString)
  4. getAbsoluteURL(String relativeURL, String baseURL)
  5. getAbsoluteUrl(URL baseUrl, String relativeUrl)
  6. getAbsoluteUrlFromFile(final String file)
  7. getAbsoluteUrlPathFromFile(String file)
  8. isAbsolute(String uri)