Java URL Create createURL(String url, String baseURI)

Here you can find the source of createURL(String url, String baseURI)

Description

Create URL using base URI.

License

Open Source License

Parameter

Parameter Description
url a URL string.
baseURI a base url string to assist in creating a URL.

Exception

Parameter Description
MalformedURLException if a malformed URL has occurred.

Return

newly created URL.

Declaration

public static URL createURL(String url, String baseURI) throws MalformedURLException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2002-2005 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/* w  w  w  . ja  v  a2  s  .  c o m*/
 *   IBM - Initial API and implementation
 *******************************************************************************/

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

public class Main {
    /**
     * Create URL using base URI.
     * 
     * @param url a URL string.
     * @param baseURI a base url string to assist in creating a URL.
     * @return newly created URL.
     * @throws MalformedURLException if a malformed URL has occurred.
     */
    public static URL createURL(String url, String baseURI) throws MalformedURLException {
        URL returnURL = null;
        URI uri = null;
        try {
            returnURL = new URL(url);
            uri = new URI(url);
            uri = uri.normalize();
            returnURL = new URL(uri.toString());
        }

        catch (Exception mue) {
            int i = baseURI.lastIndexOf('/');
            int j = baseURI.lastIndexOf('\\');
            if (j > i)
                i = j;
            try {
                uri = new URI(baseURI.substring(0, i + 1) + url);
                uri = uri.normalize();
                returnURL = uri.toURL();
            } catch (Exception e) {
                return new URL(baseURI.substring(0, i + 1) + url);
            }
        }
        return returnURL;
    }
}

Related

  1. createURL(String src, File basedir)
  2. createURL(String str)
  3. createURL(String url)
  4. createURL(String url)
  5. createURL(String url)
  6. createURL(String urlString)
  7. toUrl(String filename)
  8. toURL(String port, String address, String table)
  9. toURL(String protocol, String host, int port, String path)