Java:url 编码离开“允许”性格完整

发布于 2024-10-17 17:03:42 字数 246 浏览 2 评论 0原文

来自 Java 新手的简单问题。 我想对 url 进行编码,以便将非标准字符转换为其十六进制值(即 %XX),而预期在 url 中看到的字符(字母、数字、正斜杠、问号等)将保持不变。

例如,编码

"hi/hello?who=moris\\boris"

应该有

"hi/hello?who=moris%5cboris"

想法吗?

Simple question from a Java novice.
I want to encode a url so that nonstandard characters will be transformed to their hex value (that is %XX) while characters one expects to see in a url - letter, digits, forward slashes, question marks and whatever, will be left intact.

For example, encoding

"hi/hello?who=moris\\boris"

should result with

"hi/hello?who=moris%5cboris"

ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

衣神在巴黎 2024-10-24 17:03:42

这实际上是一个相当棘手的问题。它之所以棘手是因为 URL 的不同部分需要以不同的方式处理(编码)。

根据我的经验,最好的方法是使用 URL 或 URI 类从其组件中组装 url,让它们负责正确编码组件。


事实上,现在我想起来了,你必须在组装组件之前对其进行编码。一旦部件组装完毕,不可能判断是否(例如)“?”用于查询分隔符(不要转义它)或路径名组件中的字符(转义它)。

This is actually, a rather tricky problem. And the reason that it is tricky is that the different parts of a URL need to be handled (encoded) differently.

In my experience, the best way to do this is to assemble the url from its components using the URL or URI class, letting the them take care of the encoding the components correctly.


In fact, now that I think about it, you have to encode the components before they get assembled. Once the parts are assembled it is impossible to tell whether (for example) a "?" is intended to the query separator (don't escape it) or a character in a pathname component (escape it).

天荒地未老 2024-10-24 17:03:42

您可以使用下面的方法来转义 URL 中的特殊字符。但是您只需要传递值而不是整个 url

public static String escapeSpecialCharacters(String input) {
        StringBuilder resultStr = new StringBuilder();
        for (char ch : input.toCharArray()) {
            if (isSafe(ch)) {
                resultStr.append(ch);
            } else{
                resultStr.append('%');
                resultStr.append(toHex(ch / 16));
                resultStr.append(toHex(ch % 16));                   
            }
        }

        return resultStr.toString();
    }

    private static char toHex(int ch) {
        return (char) (ch < 10 ? '0' + ch : 'A' + ch - 10);
    }

    private static boolean isSafe(char ch) {
    return ((ch>='A' && ch<='Z') || (ch>='a' && ch<='z') || (ch>='0' && ch<='9') || "-_.~".indexOf(ch)>=0); 
}

You can use below to escape special chars in URLs. However you need to pass the value only not the whole url

public static String escapeSpecialCharacters(String input) {
        StringBuilder resultStr = new StringBuilder();
        for (char ch : input.toCharArray()) {
            if (isSafe(ch)) {
                resultStr.append(ch);
            } else{
                resultStr.append('%');
                resultStr.append(toHex(ch / 16));
                resultStr.append(toHex(ch % 16));                   
            }
        }

        return resultStr.toString();
    }

    private static char toHex(int ch) {
        return (char) (ch < 10 ? '0' + ch : 'A' + ch - 10);
    }

    private static boolean isSafe(char ch) {
    return ((ch>='A' && ch<='Z') || (ch>='a' && ch<='z') || (ch>='0' && ch<='9') || "-_.~".indexOf(ch)>=0); 
}
韶华倾负 2024-10-24 17:03:42

org.apache.commons.codec.net.URLCodec 将对特殊字符进行编码(例如您指出的 \ )。但是,您可能需要分解 url,因为您不希望对路径中的字符进行编码。此外,您将需要拆分参数名称和值,因为 ? &和 = 需要保持完整以单独传递参数,而不是作为一个巨大的参数名称。

org.apache.commons.codec.net.URLCodec will encode special characters (e.g. the \ as you indicated). However, you will likely need to break up the url as you don't want characters in the path encoded. Additionally, you will need to split up the parameter names and values since ? & and = need to remain intact to pass the parameters individually and not as one huge parameter name.

灰色世界里的红玫瑰 2024-10-24 17:03:42

您可以尝试 spring UriUtils。这似乎可以正确处理 URL 相应部分的 URL 编码/解码。

http://docs.spring .io/spring/docs/current/javadoc-api/org/springframework/web/util/UriUtils.html

You can try spring UriUtils.This seems to be handling the URL encoding/decoding correctly for the appropriate parts of the URL.

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/util/UriUtils.html

蓝色星空 2024-10-24 17:03:42

使用 URLEncoder.encode(url, "UTF-8"),请参阅 Javadoc

Use URLEncoder.encode(url, "UTF-8"), see the Javadoc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文