Convert an URL which represents a local file or a resource into a File.

@param url is the URL to convert.
@return the file.
@throws IllegalArgumentException is the URL was malformed.
972428
@Pure
	@SuppressWarnings("checkstyle:npathcomplexity")
	public static File convertURLToFile(URL url) {
		URL theUrl = url;
		if (theUrl == null) {
			return null;
		}
		if (URISchemeType.RESOURCE.isURL(theUrl)) {
			theUrl = Resources.getResource(decodeHTMLEntities(theUrl.getFile()));
			if (theUrl == null) {
				theUrl = url;
			}
		}
		URI uri;
		try {
			// this is the step that can fail, and so
			// it should be this step that should be fixed
			uri = theUrl.toURI();
		} catch (URISyntaxException e) {
			// OK if we are here, then obviously the URL did
			// not comply with RFC 2396. This can only
			// happen if we have illegal unescaped characters.
			// If we have one unescaped character, then
			// the only automated fix we can apply, is to assume
			// all characters are unescaped.
			// If we want to construct a URI from unescaped
			// characters, then we have to use the component
			// constructors:
			try {
				uri = new URI(theUrl.getProtocol(), theUrl.getUserInfo(),
						theUrl.getHost(), theUrl.getPort(),
						decodeHTMLEntities(theUrl.getPath()),
						decodeHTMLEntities(theUrl.getQuery()),
						theUrl.getRef());
			} catch (URISyntaxException e1) {
				// The URL is broken beyond automatic repair
				throw new IllegalArgumentException(Locale.getString("E1", theUrl)); //$NON-NLS-1$
			}

		}
		if (uri != null && URISchemeType.FILE.isURI(uri)) {
			final String auth = uri.getAuthority();
			String path = uri.getPath();
			if (path == null) {
				path = uri.getRawPath();
			}
			if (path == null) {
				path = uri.getSchemeSpecificPart();
			}
			if (path == null) {
				path = uri.getRawSchemeSpecificPart();
			}
			if (path != null) {
				if (auth == null || "".equals(auth)) { //$NON-NLS-1$
					// absolute filename in URI
					path = decodeHTMLEntities(path);
				} else {
					// relative filename in URI, extract it directly
					path = decodeHTMLEntities(auth + path);
				}
				if (Pattern.matches("^" + Pattern.quote(URL_PATH_SEPARATOR) //$NON-NLS-1$
						+ "[a-zA-Z][:|].*$", path)) { //$NON-NLS-1$
					path = path.substring(URL_PATH_SEPARATOR.length());
				}
				return new File(path);
			}
		}
		throw new IllegalArgumentException(Locale.getString("E2", theUrl)); //$NON-NLS-1$
	}
61966
public static String getURL(final String fileName) {

        if (fileName.startsWith("file:/")) {
            return fileName;
        } else {
            final File file = new File(fileName);
            return file.toURI().toString();
        }

    }
739378
public static URL toURL(String value) throws MalformedURLException {
        try {
            URL url = new URL(value);
            return url;
        } catch (MalformedURLException e) {
            try {
                return new File(value).toURI().toURL();
            } catch (MalformedURLException e2) {
                // throw the original
                throw e;
            }
        }
    }
582311
public static File toFile(final URI filename) {
        if (filename == null) {
            return null;
        }
        final URI f = stripFragment(filename);
        if ("file".equals(f.getScheme()) && f.getPath() != null && f.isAbsolute()) {
            return new File(f);
        } else {
            return toFile(f.toString());
        }
    }
1341264
public static void downloadImageToFile(String url, Path file) throws IOException {


        if (Files.exists(file.getParent())) {
            URL urlObject = new URL(url);
            URLConnection connection = urlObject.openConnection();
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
            connection.setRequestProperty("Referer", "http://www.google.com");


            FileUtils.copyInputStreamToFile(connection.getInputStream(), file.toFile());
        }
    }
331356
private static File getFile(URL resourceUrl) {
    try {
      return new File(new URI(resourceUrl.toString()).getPath());
    } catch (URISyntaxException e) {
      throw new RuntimeException(e);
    } catch (NullPointerException npe) {
      LOG.error("NullPointerException is thrown. Resource url: {} ", resourceUrl);
      throw new RuntimeException(npe);
    }
  }
347179
public static InputStream getStream(URL url) {
		Assert.notNull(url);
		try {
			return url.openStream();
		} catch (IOException e) {
			throw new IORuntimeException(e);
		}
	}
1266808
public static boolean isJarFileURL(URL url) {
        return (URL_PROTOCOL_FILE.equals(url.getProtocol()) &&
                url.getPath().toLowerCase().endsWith(JAR_FILE_EXTENSION));
    }
425932
public static InputStream fromURL(URL url) throws IOException {
		if(url != null) {
			return url.openStream();
		}
		return null;
	}
364507
public byte[] simpleGetBytes(String url) throws IOException {
		final AtomicReference<byte[]> bytes = new AtomicReference<>();
		simpleGetInternal(url, inputStream -> {
            try {
                bytes.set(IOUtils.toByteArray(inputStream));
            } catch (IOException e) {
                throw new IllegalStateException(e);
            }
        }, null);

		return bytes.get();
	}
297203
public static URI urlToURI(URL url) {
    try {
      return url.toURI();
    }
    catch (URISyntaxException e) {
      throw LOG.cannotConvertUrlToUri(url, e);
    }
  }
1323344
private URL extractActualUrl(URL jarUrl) throws MalformedURLException {
        String urlFile = jarUrl.getFile();
        int separatorIndex = urlFile.indexOf("!/");
        if (separatorIndex != -1) {
            String jarFile = urlFile.substring(0, separatorIndex);

            try {
                return new URL(jarFile);
            } catch (MalformedURLException var5) {
                if (!jarFile.startsWith("/")) {
                    jarFile = "/" + jarFile;
                }

                return new URL("file:" + jarFile);
            }
        } else {
            return jarUrl;
        }
    }
562110
public static URL url(final String url) {
        if (url == null) {
            return null;
        }
        try {
            if (url.startsWith("classpath:")) {
                return new URL(null, url, new ClasspathURLStreamHandler());
            }
            return new URL(url);
        } catch (final MalformedURLException ex) {
            throw new IllegalArgumentException("Invalid URL: " + url, ex);
        }
    }
228074
public static Resource newResource(URL url)
        throws IOException
    {
        if (url==null)
            return null;

        String urls=url.toExternalForm();
        if( urls.startsWith( "file:"))
        {
            try
            {
                FileResource fileResource= new FileResource(url);
                return fileResource;
            }
            catch(Exception e)
            {
                log.debug(LogSupport.EXCEPTION,e);
                return new BadResource(url,e.toString());
            }
        }
        else if( urls.startsWith( "jar:file:"))
        {
            return new JarFileResource(url);
        }
        else if( urls.startsWith( "jar:"))
        {
            return new JarResource(url);
        }

        return new URLResource(url,null);
    }
661696
public static File toFile(final URL aURL) throws MalformedURLException {
        if (aURL.getProtocol().equals(FILE_TYPE)) {
            return new File(aURL.toString().replace("file:", ""));
        }

        throw new MalformedURLException(LOGGER.getI18n(MessageCodes.UTIL_036, aURL));
    }
653596
public static URL asUrlOrResource(String s) {
        if (Strings.isNullOrEmpty(s)) {
            return null;
        }

        try {
            return new URL(s);
        } catch (MalformedURLException e) {
            //If its not a valid URL try to treat it as a local resource.
            return findConfigResource(s);
        }
    }
984144
public static boolean isFileURL(URL url) {
        String protocol = url.getProtocol();
        return (URL_PROTOCOL_FILE.equals(protocol) || protocol.startsWith(URL_PROTOCOL_VFS));
    }