Java URL Create createURL(String src, File basedir)

Here you can find the source of createURL(String src, File basedir)

Description

Converts a string to a URL.

License

LGPL

Parameter

Parameter Description
src a URL or a file path
basedir base directory for relative file paths

Exception

Parameter Description
MalformedURLException an exception

Return

URL-equivalent of src

Declaration

public static URL createURL(String src, File basedir) throws MalformedURLException 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    /**//from  w w  w.j  a v  a 2s  . c  o  m
     * Converts a string to a URL. If it doesn't form a valid URL as is, it will
     * be treated as a file and converted to a file: URL. Relative file paths
     * will be interpreted, relative to basedir, and converted to absolute form.
     * 
     * @param src a URL or a file path
     * @param basedir base directory for relative file paths
     * @return URL-equivalent of src
     * @throws MalformedURLException
     */
    public static URL createURL(String src, File basedir) throws MalformedURLException {
        URL srcURL;
        try {
            srcURL = new URL(src);
        } catch (MalformedURLException e) {
            File srcFile = new File(src);
            if (!srcFile.isAbsolute()) {
                srcFile = new File(basedir, src);
            }
            srcURL = srcFile.toURI().toURL();
        }
        return srcURL;
    }
}

Related

  1. createUrl(final String spec)
  2. createURL(final String url)
  3. createURL(String fileName)
  4. createUrl(String spec)
  5. createURL(String spec, URLStreamHandlerFactory urlHandlerFactory)
  6. createURL(String str)
  7. createURL(String url)
  8. createURL(String url)
  9. createURL(String url)