Here you can find the source of fileToURL(File file)
Parameter | Description |
---|---|
file | a parameter |
public static URL fileToURL(File file)
//package com.java2s; /*//w ww .j a va 2 s. c om * GeoTools - The Open Source Java GIS Toolkit * http://geotools.org * * (C) 2012, Open Source Geospatial Foundation (OSGeo) * * This library 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; * version 2.1 of the License. * * This library 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. */ import java.io.File; import java.net.MalformedURLException; import java.net.URL; public class Main { /** * A replacement for File.toURI().toURL(). * <p> * The handling of file.toURL() is broken; the handling of file.toURI().toURL() is known to be * broken on a few platforms like mac. We have the urlToFile( URL ) method that is able to * untangle both these problems and we use it in the geotools library. * <p> * However occasionally we need to pick up a file and hand it to a third party library like EMF; * this method performs a couple of sanity checks which we can use to prepare a good URL * reference to a file in these situtations. * * @param file * @return URL */ public static URL fileToURL(File file) { try { URL url = file.toURI().toURL(); String string = url.toExternalForm(); if (string.contains("+")) { // this represents an invalid URL created using either // file.toURL(); or // file.toURI().toURL() on a specific version of Java 5 on Mac string = string.replace("+", "%2B"); } if (string.contains(" ")) { // this represents an invalid URL created using either // file.toURL(); or // file.toURI().toURL() on a specific version of Java 5 on Mac string = string.replace(" ", "%20"); } return new URL(string); } catch (MalformedURLException e) { return null; } } }