Java URL to File Name getFileFromUrl(String url, File base)

Here you can find the source of getFileFromUrl(String url, File base)

Description

Create a file from a 'relative' file URL

License

Open Source License

Declaration

public static File getFileFromUrl(String url, File base) 

Method Source Code

//package com.java2s;
/**/*from w w w . j  a  va 2s.  c  o  m*/
 * AC - A source-code copy detector
 *
 *     For more information please visit:  http://github.com/manuel-freire/ac
 *
 * ****************************************************************************
 *
 * This file is part of AC, version 2.0
 *
 * AC is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * AC is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with AC.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;

public class Main {
    /**
     * Create a file from a 'relative' file URL 
     */
    public static File getFileFromUrl(String url, File base) {

        // check to see if absolute (no '.' as first char) - and ignore base if so
        try {
            if (!url.startsWith("file://.")) {
                java.net.URI fileURI = new java.net.URI(url);
                return new File(fileURI);
            }
        } catch (java.net.URISyntaxException urise) {
            throw new IllegalArgumentException("Bad URL, cannot process "
                    + url);
        }

        // return a file using the base
        return new File(base, url.substring("file://.".length()));
    }
}

Related

  1. getFileExtensionByURL(URL url)
  2. getFileFor(URL url1)
  3. getFileForURL(String url)
  4. getFileForUrl(URL url)
  5. getFileFrom(final URL url)
  6. getFileFromUrl(String urlPath)
  7. getFileFromURL(URL url)
  8. getFileFromURL(URL url)
  9. getFileFromUrl(URL url)