質問

ジェネリックをエンコードするクラスはありますか String RFC 3986仕様に従いますか?

あれは: "hello world" => "hello%20world" NOT(RFC 1738): "hello+world"

ありがとう

役に立ちましたか?

他のヒント

URLの場合は、URIを使用してください

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

出典:Twitter 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);
    }
}

あるかどうかわからない。エンコーディングを提供するクラスがありますが、「 "に「+」に変わります。ただし、文字列クラスの交換メソッドを使用して、「+」を必要なものに変換できます。

str.repaceall( "+"、 "%20")

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

私のお気に入りのサイトの1つのクロスチェックは、同じ結果「URISのエンコードパーセント」を示しています。は、私にはよく見えますよ。 http://rishida.net/tools/conversion/

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top