Java - 如何编码非拉丁字符的 URL 路径

标签 java android url unicode

目前有final URL url = new URL(urlString);但我遇到了不支持路径中非 ASCII 的服务器。

使用 Java (Android) 我需要对 URL 进行编码

http://acmeserver.com/download/agc/fcms/儿子去哪儿/儿子去哪儿.png

http://acmeserver.com/download/agc/fcms/%E5%84%BF%E5%AD%90%E5%8E%BB%E5%93%AA%E5%84%BF/%E5%84%BF%E5%AD%90%E5%8E%BB%E5%93%AA%E5%84%BF.png

就像浏览器一样。

我检查了URLEncoder.encode(s, "UTF-8");但它也编码 /斜线

http%3A%2F%2acmeserver.com%2Fdownload%2Fagc%2Ffcms%2F%E5%84%BF%E5%AD%90%E5%8E%BB%E5%93%AA%E5%84%BF%2F%E5%84%BF%E5%AD%90%E5%8E%BB%E5%93%AA%E5%84%BF.png

有没有办法在不解析方法获取的字符串的情况下简单地做到这一点?

来自 http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars

B.2.1 Non-ASCII characters in URI attribute values Although URIs do not contain non-ASCII values (see [URI], section 2.1) authors sometimes specify them in attribute values expecting URIs (i.e., defined with %URI; in the DTD). For instance, the following href value is illegal:

<A href="http://foo.org/Håkon">...</A>

We recommend that user agents adopt the following convention for handling non-ASCII characters in such cases:

  1. Represent each character in UTF-8 (see [RFC2279]) as one or more bytes.
  2. Escape these bytes with the URI escaping mechanism (i.e., by converting each byte to %HH, where HH is the hexadecimal notation of the byte value).

最佳答案

您应该只对特殊字符进行编码并将它们一起解析。如果您尝试对整个 URI 进行编码,则会遇到问题。

坚持:

String query = URLEncoder.encode("apples oranges", "utf-8");
String url = "http://stackoverflow.com/search?q=" + query;

看看这个 great guide关于 URL 编码。

话虽如此,稍作搜索表明可能还有其他方法可以完成您想要的操作:

试一试:

String urlStr = "http://abc.dev.domain.com/0007AC/ads/800x480 15sec h.264.mp4";
URL url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();

(您需要对这些空格进行编码,以便将其用于请求。)

This takes advantage of a couple features available to you in Android classes. First, the URL class can break a url into its proper components so there is no need for you to do any string search/replace work. Secondly, this approach takes advantage of the URI class feature of properly escaping components when you construct a URI via components rather than from a single string.

The beauty of this approach is that you can take any valid url string and have it work without needing any special knowledge of it yourself.

关于Java - 如何编码非拉丁字符的 URL 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26254051/

相关文章:

java - 仅将字符串内容返回到特定索引

java - jsp 将 blob 图像保存在数据库 mysql 中 - 我的代码不起作用

android - Android 中的 NavigationView 项目填充

android - ValueFormatter 抛出 IndexOutOfBoundsException

Mysql - 如何将网址保存为唯一键

java - java oauth 2.0 插件编译失败的 Google+ 快速入门

java - 两个单例示例之间的差异

android - ListView 多选 - 默认透明颜色不能正常工作?

c# - 检索域 url

javascript - 如何根据 URL 显示和隐藏 DIV。在 JavaScript 中