org.apache.commons.io.FileUtils.decodeUrl()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(6.8k)|赞(0)|评价(0)|浏览(178)

本文整理了Java中org.apache.commons.io.FileUtils.decodeUrl()方法的一些代码示例,展示了FileUtils.decodeUrl()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileUtils.decodeUrl()方法的具体详情如下:
包路径:org.apache.commons.io.FileUtils
类名称:FileUtils
方法名:decodeUrl

FileUtils.decodeUrl介绍

[英]Decodes the specified URL as per RFC 3986, i.e. transforms percent-encoded octets to characters by decoding with the UTF-8 character set. This function is primarily intended for usage with java.net.URL which unfortunately does not enforce proper URLs. As such, this method will leniently accept invalid characters or malformed percent-encoded octets and simply pass them literally through to the result string. Except for rare edge cases, this will make unencoded URLs pass through unaltered.
[中]按照RFC 3986解码指定的URL,即通过UTF-8字符集解码将百分比编码的八位字节转换为字符。此函数主要用于java。网不幸的是,该URL没有强制执行正确的URL。因此,此方法将宽容地接受无效字符或格式不正确的百分比编码八位字节,并将它们直接传递给结果字符串。除了极少数边缘情况外,这将使未编码的URL不受更改地通过。

代码示例

代码示例来源:origin: commons-io/commons-io

/**
 * 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);
  }
}

代码示例来源:origin: commons-io/commons-io

@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"));
}

代码示例来源:origin: commons-io/commons-io

@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"));
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testDecodeUrlNullSafe() {
  assertNull(FileUtils.decodeUrl(null));
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testDecodeUrlEncodingUtf8() {
  assertEquals("\u00E4\u00F6\u00FC\u00DF", FileUtils.decodeUrl("%C3%A4%C3%B6%C3%BC%C3%9F"));
}

代码示例来源:origin: io.github.stephenc.docker/docker-client-shaded

/**
 * 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);
  }
}

代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded

/**
 * 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);
  }
}

代码示例来源:origin: Nextdoor/bender

/**
 * 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);
  }
}

代码示例来源:origin: org.onosproject/onlab-thirdparty

/**
 * 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);
  }
}

相关文章

微信公众号

最新文章

更多

FileUtils类方法