简体   繁体   English

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

[英]Java - how to encode URL path for non Latin characters

Currently there is final URL url = new URL(urlString); 目前有final URL url = new URL(urlString); but I run into server not supporting non-ASCII in path. 但我遇到服务器不支持路径中的非ASCII。

Using Java (Android) I need to encode URL from 使用Java(Android)我需要编码来自的URL

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

to

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

just like browsers do. 就像浏览器一样。

I checked URLEncoder.encode(s, "UTF-8"); 我检查了URLEncoder.encode(s, "UTF-8"); but it also encodes / slashes 但它也编码/斜杠

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

Is there way to do it simply without parsing string that the method gets? 有没有方法可以简单地解析方法得到的字符串?

from http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars 来自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 (ie, defined with %URI; in the DTD). B.2.1 URI属性值中的非ASCII字符尽管URI不包含非ASCII值(参见[URI],第2.1节),但作者有时会在期望URI的属性值中指定它们(即,使用%URI定义;在DTD中) 。 For instance, the following href value is illegal: 例如,以下href值是非法的:

<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: 我们建议用户代理在这种情况下采用以下约定来处理非ASCII字符:

  1. Represent each character in UTF-8 (see [RFC2279]) as one or more bytes. 将UTF-8中的每个字符(参见[RFC2279])表示为一个或多个字节。
  2. Escape these bytes with the URI escaping mechanism (ie, by converting each byte to %HH, where HH is the hexadecimal notation of the byte value). 使用URI转义机制转义这些字节(即,通过将每个字节转换为%HH,其中HH是字节值的十六进制表示法)。

You should just encode the special characters and the parse them together. 您应该只编码特殊字符并将它们解析在一起。 If you tried to encode the entire URI then you'd run into problems. 如果您尝试对整个URI进行编码,那么您就会遇到问题。

Stick with: 坚持:

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

Check out this great guide on URL encoding. 查看这个关于URL编码的精彩指南

That being said, a little bit of searching suggests that there may be other ways to do what you want: 话虽如此,一点点搜索表明可能还有其他方法可以做你想做的事情:

Give this a try: 尝试一下:

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();

(You will need to have those spaces encoded so you can use it for a request.) (您需要对这些空间进行编码,以便将其用于请求。)

This takes advantage of a couple features available to you in Android classes. 这利用了Android类中可用的一些功能。 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. 首先,URL类可以将url分解为其正确的组件,因此您无需进行任何字符串搜索/替换工作。 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. 其次,当您通过组件而不是单个字符串构造URI时,此方法利用了正确转义组件的URI类功能。

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. 这种方法的优点在于,您可以使用任何有效的URL字符串并使其工作,而无需您自己了解任何特殊知识。

I did it as below, which is cumbersome 我这样做了,这很麻烦

        //was: final URL url = new URL(urlString);
        String asciiString;
        try {
            asciiString = new URL(urlString).toURI().toASCIIString();
        } catch (URISyntaxException e1) {
            Log.e(TAG, "Error new URL(urlString).toURI().toASCIIString() " + urlString + " : " + e1);
            return null;
        }
        Log.v(TAG, urlString+" -> "+ asciiString );
        final URL url = new URL(asciiString);

url is later used in url后来被用于

        connection = (HttpURLConnection) url.openConnection();
final URL url = new URL( new URI(urlString).toASCIIString() );

为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM