/** * Convert from a <code>URL</code> to a <code>File</code>. * <p> * From version 1.1 this method will decode the URL. * Syntax such as <code>file:///my%20docs/file.txt</code> will be * correctly decoded to <code>/my docs/file.txt</code>. Starting with version * 1.5, this method uses UTF-8 to decode percent-encoded octets to characters. * Additionally, malformed percent-encoded octets are handled leniently by * passing them through literally. * * @param url the file URL to convert, {@code null} returns {@code null} * @return the equivalent <code>File</code> object, or {@code null} * if the URL's protocol is not <code>file</code> */ public static File toFile(final URL url) { if (url == null || !"file".equalsIgnoreCase(url.getProtocol())) { return null; } else { String filename = url.getFile().replace('/', File.separatorChar); filename = decodeUrl(filename); return new File(filename); } }
@Test public void testDecodeUrlLenient() { assertEquals(" ", FileUtils.decodeUrl(" ")); assertEquals("\u00E4\u00F6\u00FC\u00DF", FileUtils.decodeUrl("\u00E4\u00F6\u00FC\u00DF")); assertEquals("%", FileUtils.decodeUrl("%")); assertEquals("% ", FileUtils.decodeUrl("%%20")); assertEquals("%2", FileUtils.decodeUrl("%2")); assertEquals("%2G", FileUtils.decodeUrl("%2G")); }
@Test public void testDecodeUrl() { assertEquals("", FileUtils.decodeUrl("")); assertEquals("foo", FileUtils.decodeUrl("foo")); assertEquals("+", FileUtils.decodeUrl("+")); assertEquals("% ", FileUtils.decodeUrl("%25%20")); assertEquals("%20", FileUtils.decodeUrl("%2520")); assertEquals("jar:file:/C:/dir/sub dir/1.0/foo-1.0.jar!/org/Bar.class", FileUtils .decodeUrl("jar:file:/C:/dir/sub%20dir/1.0/foo-1.0.jar!/org/Bar.class")); }
@Test public void testDecodeUrlNullSafe() { assertNull(FileUtils.decodeUrl(null)); }
@Test public void testDecodeUrlEncodingUtf8() { assertEquals("\u00E4\u00F6\u00FC\u00DF", FileUtils.decodeUrl("%C3%A4%C3%B6%C3%BC%C3%9F")); }
/** * Convert from a <code>URL</code> to a <code>File</code>. * <p> * From version 1.1 this method will decode the URL. * Syntax such as <code>file:///my%20docs/file.txt</code> will be * correctly decoded to <code>/my docs/file.txt</code>. Starting with version * 1.5, this method uses UTF-8 to decode percent-encoded octets to characters. * Additionally, malformed percent-encoded octets are handled leniently by * passing them through literally. * * @param url the file URL to convert, {@code null} returns {@code null} * @return the equivalent <code>File</code> object, or {@code null} * if the URL's protocol is not <code>file</code> */ public static File toFile(final URL url) { if (url == null || !"file".equalsIgnoreCase(url.getProtocol())) { return null; } else { String filename = url.getFile().replace('/', File.separatorChar); filename = decodeUrl(filename); return new File(filename); } }
/** * Convert from a <code>URL</code> to a <code>File</code>. * <p> * From version 1.1 this method will decode the URL. * Syntax such as <code>file:///my%20docs/file.txt</code> will be * correctly decoded to <code>/my docs/file.txt</code>. Starting with version * 1.5, this method uses UTF-8 to decode percent-encoded octets to characters. * Additionally, malformed percent-encoded octets are handled leniently by * passing them through literally. * * @param url the file URL to convert, {@code null} returns {@code null} * @return the equivalent <code>File</code> object, or {@code null} * if the URL's protocol is not <code>file</code> */ public static File toFile(final URL url) { if (url == null || !"file".equalsIgnoreCase(url.getProtocol())) { return null; } else { String filename = url.getFile().replace('/', File.separatorChar); filename = decodeUrl(filename); return new File(filename); } }
/** * Convert from a <code>URL</code> to a <code>File</code>. * <p> * From version 1.1 this method will decode the URL. * Syntax such as <code>file:///my%20docs/file.txt</code> will be * correctly decoded to <code>/my docs/file.txt</code>. Starting with version * 1.5, this method uses UTF-8 to decode percent-encoded octets to characters. * Additionally, malformed percent-encoded octets are handled leniently by * passing them through literally. * * @param url the file URL to convert, <code>null</code> returns <code>null</code> * @return the equivalent <code>File</code> object, or <code>null</code> * if the URL's protocol is not <code>file</code> */ public static File toFile(URL url) { if (url == null || !"file".equalsIgnoreCase(url.getProtocol())) { return null; } else { String filename = url.getFile().replace('/', File.separatorChar); filename = decodeUrl(filename); return new File(filename); } }
/** * Convert from a <code>URL</code> to a <code>File</code>. * <p> * From version 1.1 this method will decode the URL. * Syntax such as <code>file:///my%20docs/file.txt</code> will be * correctly decoded to <code>/my docs/file.txt</code>. Starting with version * 1.5, this method uses UTF-8 to decode percent-encoded octets to characters. * Additionally, malformed percent-encoded octets are handled leniently by * passing them through literally. * * @param url the file URL to convert, {@code null} returns {@code null} * @return the equivalent <code>File</code> object, or {@code null} * if the URL's protocol is not <code>file</code> */ public static File toFile(URL url) { if (url == null || !"file".equalsIgnoreCase(url.getProtocol())) { return null; } else { String filename = url.getFile().replace('/', File.separatorChar); filename = decodeUrl(filename); return new File(filename); } }