Java URL to File Name getFile(URL aURL)

Here you can find the source of getFile(URL aURL)

Description

Returns the URL as a file.

License

Open Source License

Declaration

public static File getFile(URL aURL) 

Method Source Code


//package com.java2s;
import java.io.*;
import java.net.*;

public class Main {
    /**/*from  w  w w . j av  a  2 s.  c om*/
     * Returns the URL as a file.
     */
    public static File getFile(URL aURL) {
        // If "jar:" url, recast as inner URL
        if (aURL.getProtocol().equals("jar")) {
            String urls = aURL.toExternalForm();
            int sep = urls.indexOf('!');
            urls = urls.substring(4, sep);
            try {
                aURL = new URL(urls);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        // Return file for URL
        String path = aURL.getPath();
        try {
            path = URLDecoder.decode(path, "UTF-8");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return isLocal(aURL) ? new File(path) : null;
    }

    /**
     * Returns whether a URL is local.
     */
    public static boolean isLocal(URL aURL) {
        String url = aURL.toExternalForm().toLowerCase();
        return url.startsWith("file:") || url.startsWith("jar:file:");
    }
}

Related

  1. getFile(URL resourceUrl, String description)
  2. getFile(URL resourceUrl, String description)
  3. getFile(URL resourceUrl, String description)
  4. getFile(URL resourceUrl, String description)