Java 和 RFC 3986 URI 编码

发布于 2024-11-04 20:35:08 字数 199 浏览 5 评论 0原文

是否有一个类可以按照 RFC 3986 规范对通用 String 进行编码?

即: "hello world" => “hello%20world” 不是(RFC 1738)“hello+world”

谢谢

is there a class to encode a generic String following the RFC 3986 specification?

That is: "hello world" => "hello%20world" Not (RFC 1738): "hello+world"

Thanks

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

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

发布评论

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

评论(5

最终幸福 2024-11-11 20:35:08

如果是 url,则使用 URI

URI uri = new URI("http", "//hello world", null);
String urlString = uri.toASCIIString();
System.out.println(urlString);

If it's a url, use URI

URI uri = new URI("http", "//hello world", null);
String urlString = uri.toASCIIString();
System.out.println(urlString);
七七 2024-11-11 20:35:08

来源:推特
符合 RFC3986 的编码功能。

此方法获取字符串并将其转换为 RFC3986 特定的编码字符串。

/** The encoding used to represent characters as bytes. */
public static final String ENCODING = "UTF-8";

public static String percentEncode(String s) {
    if (s == null) {
        return "";
    }
    try {
        return URLEncoder.encode(s, ENCODING)
                // OAuth encodes some characters differently:
                .replace("+", "%20").replace("*", "%2A")
                .replace("%7E", "~");
        // This could be done faster with more hand-crafted code.
    } catch (UnsupportedEncodingException wow) {
        throw new RuntimeException(wow.getMessage(), wow);
    }
}

Source : Twitter
RFC3986 compliant encoding functions.

This method takes string and converts it to RFC3986 specific encoded string.

/** The encoding used to represent characters as bytes. */
public static final String ENCODING = "UTF-8";

public static String percentEncode(String s) {
    if (s == null) {
        return "";
    }
    try {
        return URLEncoder.encode(s, ENCODING)
                // OAuth encodes some characters differently:
                .replace("+", "%20").replace("*", "%2A")
                .replace("%7E", "~");
        // This could be done faster with more hand-crafted code.
    } catch (UnsupportedEncodingException wow) {
        throw new RuntimeException(wow.getMessage(), wow);
    }
}
dawn曙光 2024-11-11 20:35:08

不知道有没有。有一个类提供编码,但它将“”更改为“+”。但是你可以使用String类中的replaceAll方法将“+”转换为你想要的。

str.repaceAll("+","%20")

In don't know if there is one. There is a class that provides encoding but it changes " " into "+". But you can use the replaceAll method in String class to convert the "+" into what you want.

str.repaceAll("+","%20")

栀子花开つ 2024-11-11 20:35:08

对于 Spring Web 应用程序,我可以使用以下内容:

http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/util/UriComponentsBuilder.html

UriComponentsBuilder.newInstance()
  .queryParam("KEY1", "Wally's crazy empôrium=")
  .queryParam("KEY2", "Horibble % sign in value")
  .build().encode("UTF-8") // or .encode() defaults to UTF-8

返回字符串

?KEY1=Wally's%20crazy%20emp%C3%B4rium%3D&KEY2=Horibble%20%25%20sign%20in%20value

对我最喜欢的网站之一进行交叉检查显示了相同的结果,“URI 的编码百分比”。我觉得不错。 http://rishida.net/tools/conversion/

In the case of Spring Web applications, I was able to use this:

http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/util/UriComponentsBuilder.html

UriComponentsBuilder.newInstance()
  .queryParam("KEY1", "Wally's crazy empôrium=")
  .queryParam("KEY2", "Horibble % sign in value")
  .build().encode("UTF-8") // or .encode() defaults to UTF-8

returns the String

?KEY1=Wally's%20crazy%20emp%C3%B4rium%3D&KEY2=Horibble%20%25%20sign%20in%20value

A cross check on one of my favorite sites shows the same result, "Percent encoding for URIs". Looks good to me. http://rishida.net/tools/conversion/

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