Java URL to File Name getFileFromUrl(String urlPath)

Here you can find the source of getFileFromUrl(String urlPath)

Description

get File From Url

License

Open Source License

Declaration

public static File getFileFromUrl(String urlPath) 

Method Source Code


//package com.java2s;
/*/*  w w w .j  av  a  2  s.c o m*/
 * Sonatype Nexus (TM) Open Source Version
 * Copyright (c) 2007-2012 Sonatype, Inc.
 * All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
 *
 * This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
 * which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
 *
 * Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
 * of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
 * Eclipse Foundation. All other trademarks are the property of their respective owners.
 */

import java.io.File;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;

public class Main {
    private static Set<File> roots = null;

    public static File getFileFromUrl(String urlPath) {
        if (validFileUrl(urlPath)) {
            try {
                URL url = new URL(urlPath);
                try {
                    return new File(url.toURI());
                } catch (Exception t) {
                    return new File(url.getPath());
                }
            } catch (MalformedURLException e) {
                // Try just a regular file
                return new File(urlPath);
            }
        }

        return null;
    }

    public static boolean validFileUrl(String url) {
        boolean result = true;

        if (!validFile(new File(url))) {
            // Failed w/ straight file, now time to try URL
            try {
                if (!validFile(new File(new URL(url).getFile()))) {
                    result = false;
                }
            } catch (MalformedURLException e) {
                result = false;
            }
        }

        return result;
    }

    public static boolean validFile(File file) {
        if (roots == null) {
            roots = new HashSet<File>();

            File[] listedRoots = File.listRoots();

            for (int i = 0; i < listedRoots.length; i++) {
                roots.add(listedRoots[i]);
            }

            // Allow UNC based paths on windows
            // i.e. \\someserver\repository\central\blah
            if (isWindows()) {
                roots.add(new File("\\\\"));
            }
        }

        File root = file;

        while (root.getParentFile() != null) {
            root = root.getParentFile();
        }

        return roots.contains(root);
    }

    public static boolean isWindows() {
        return System.getProperty("os.name").indexOf("Windows") != -1;
    }
}

Related

  1. getFileFor(URL url1)
  2. getFileForURL(String url)
  3. getFileForUrl(URL url)
  4. getFileFrom(final URL url)
  5. getFileFromUrl(String url, File base)
  6. getFileFromURL(URL url)
  7. getFileFromURL(URL url)
  8. getFileFromUrl(URL url)
  9. getFileFromURL(URL urlToDecode)